casey / just

🤖 Just a command runner
https://just.systems
Creative Commons Zero v1.0 Universal
20.05k stars 448 forks source link

dotenv-path unexpectedly loads .env from parent directory #2257

Open yossarian21 opened 1 month ago

yossarian21 commented 1 month ago

I'm using just@1.31.0 on Mac.

The documentation states:

If dotenv-path is set, just will look for a file at the given path, which may be absolute, or relative to the working directory. dotenv-filename and dotenv-path and similar, but dotenv-path is only checked relative to the working directory, whereas dotenv-filename is checked relative to the working directory and each of its ancestors.

I tried configuring my justfile as follows:

set dotenv-path := "."

_default:
    @just --list

There is no .env file in the current directory, but in the parent directory, I created a bogus .env file, e.g.

daofinasdfoiasdn

In the current directory with my justfile, I get this error:

just
error: Failed to load environment file: Error parsing line: 'daofinasdfoiasdn', error at line index: 16

Expectation: dotenv-path should only load a .env file from the specified path.

casey commented 1 month ago

This is an annoying consequence of the way that the various dotenv settings work.

Setting any dotenv- setting turns on dotenv loading, which uses both dotenv-filename and dotenv-path. dotenv-filename defaults to .env, so even if you set dotenv-path, just will still also try to use dotenv-filename to load an environment file.

So, even though you set dotenv-path := ".", dotenv-filename still has the default value of .env, and the environment file in the parent directory is loaded.

This is definitely not ideal behavior, and if I could go back in time, I would definitely make dotenv-filenmae and dotenv-path not have default values, which would enable you to get the behavior you want.

Part of the reason that these settings are weird and continue to be weird is that someone could be relying on the existing behavior, and so changing them to be less weird might break someone's setup.

Probably the best way to fix this would be to introduce a new setting (sigh) which if used would provide better behavior.