From 395f8187bf82f6e7f38329b8510a47922ab31a94 Mon Sep 17 00:00:00 2001 From: James Tanner Date: Wed, 25 Jan 2023 12:52:20 -0500 Subject: [PATCH] Handle situations where the cwd does not exist. This is seen in some situations with dynaconf where the system under test starts from a src code directory that got moved around or deleted during operation. Signed-off-by: James Tanner --- src/dotenv/cli.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/dotenv/cli.py b/src/dotenv/cli.py index b490bfaf..65ead461 100644 --- a/src/dotenv/cli.py +++ b/src/dotenv/cli.py @@ -17,8 +17,22 @@ from .version import __version__ +def enumerate_env(): + """ + Return a path for the ${pwd}/.env file. + + If pwd does not exist, return None. + """ + try: + cwd = os.getcwd() + except FileNotFoundError: + return None + path = os.path.join(cwd, '.env') + return path + + @click.group() -@click.option('-f', '--file', default=os.path.join(os.getcwd(), '.env'), +@click.option('-f', '--file', default=enumerate_env(), type=click.Path(file_okay=True), help="Location of the .env file, defaults to .env file in current working directory.") @click.option('-q', '--quote', default='always',