From c88cb8ef9f8390fd7a437be8979193967302db85 Mon Sep 17 00:00:00 2001 From: Robert Craigie Date: Thu, 2 Mar 2023 21:05:25 +0000 Subject: [PATCH] fix(cli): ensure entrypoint only exists if npm install passes (#708) ## Change Summary closes #705 I haven't tested these changes yet and have not confirmed that this does achieve the desired behaviour. ## Checklist - [ ] Unit tests for the changes exist - [ ] Tests pass without significant drop in coverage - [ ] Documentation reflects changes where applicable - [ ] Test snapshots have been [updated](https://prisma-client-py.readthedocs.io/en/latest/contributing/contributing/#snapshot-tests) if applicable ## Agreement By submitting this pull request, I confirm that you can use, modify, copy and redistribute this contribution, under the terms of your choice. --- src/prisma/cli/prisma.py | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/prisma/cli/prisma.py b/src/prisma/cli/prisma.py index 79db43b85..621ae36f5 100644 --- a/src/prisma/cli/prisma.py +++ b/src/prisma/cli/prisma.py @@ -88,18 +88,30 @@ def ensure_cached() -> CLICache: if not entrypoint.exists(): click.echo('Installing Prisma CLI') - proc = npm.run( - 'install', - f'prisma@{config.prisma_version}', - cwd=config.binary_cache_dir, - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, - ) - if proc.returncode != 0: - click.echo( - f'An error ocurred while installing the Prisma CLI; npm install log: {proc.stdout.decode("utf-8")}' + + try: + proc = npm.run( + 'install', + f'prisma@{config.prisma_version}', + cwd=config.binary_cache_dir, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, ) - proc.check_returncode() + if proc.returncode != 0: + click.echo( + f'An error ocurred while installing the Prisma CLI; npm install log: {proc.stdout.decode("utf-8")}' + ) + proc.check_returncode() + except Exception: + # as we use the entrypoint existing to check whether or not we should run `npm install` + # we need to make sure it doesn't exist if running `npm install` fails as it will otherwise + # lead to a broken state, https://github.com/RobertCraigie/prisma-client-py/issues/705 + if entrypoint.exists(): + try: + entrypoint.unlink() + except Exception: + pass + raise if not entrypoint.exists(): raise PrismaError(