diff --git a/CHANGELOG.md b/CHANGELOG.md index 71c9740d..f8d0fc31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ### Fixed +- `dotenv run` now resolves mixed-case `Path` overrides like `PATH` on Windows, while keeping environment names case-sensitive on POSIX (#761). - Fix a package build deprecation warning caused by a non-string `license` value in `pyproject.toml` by [@kurtmckee] in [#648] - `set_key`, `unset_key` and the `dotenv set`/`unset` commands now name the `.env` path instead of an internal temporary file when its directory is missing or not writable, and the CLI prints a short error and exits with code 2 instead of a traceback by [@jamalkamaladdin] in [#711] - `set_key` and `unset_key` no longer leave a `.tmp_*` file behind on Windows when writing a read-only `.env` fails, and the error raised is the one from the failed write rather than from cleaning up the temporary file by [@MohammedAlkindi] in [#686] diff --git a/README.md b/README.md index 3400e67c..1a64b8af 100644 --- a/README.md +++ b/README.md @@ -167,6 +167,10 @@ $ dotenv run -- python foo.py Run `dotenv --help` for more information about the options and subcommands. +For `dotenv run` on Windows, environment variable names are case-insensitive: +`Path` and `PATH` both override the inherited search path. On POSIX, names +remain case-sensitive. + ## File format The format is not formally specified and still improves over time. That being diff --git a/src/dotenv/cli.py b/src/dotenv/cli.py index 83849690..dd0c10df 100644 --- a/src/dotenv/cli.py +++ b/src/dotenv/cli.py @@ -233,6 +233,9 @@ def run_command(command: List[str], env: Dict[str, str]) -> None: # copy the current environment variables and add the vales from # `env` cmd_env = os.environ.copy() + if sys.platform == "win32": + # Match os.environ's case-insensitive names before looking up PATH. + env = {key.upper(): value for key, value in env.items()} cmd_env.update(env) if sys.platform == "win32": diff --git a/tests/test_cli.py b/tests/test_cli.py index a719e195..106431fd 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,4 +1,5 @@ import os +import shutil import sys from pathlib import Path from typing import Optional @@ -381,6 +382,44 @@ def test_run_with_path_from_env_file(tmp_path): check_process(result, exit_code=0, stdout="x\n") +@pytest.mark.skipif(sys.platform != "win32", reason="Windows environment names") +@pytest.mark.parametrize("path_name", ["PATH", "Path", "path", "pAtH"]) +def test_run_with_mixed_case_path(tmp_path, path_name): + bin_dir = tmp_path / "bin" + bin_dir.mkdir() + shutil.copy2(os.environ["COMSPEC"], bin_dir / "dotenv-case-probe.exe") + (tmp_path / ".env").write_text(f"{path_name}={bin_dir}\n") + result = run_dotenv( + ["run", "dotenv-case-probe", "/c", "echo PATH_CASE_OK"], cwd=tmp_path + ) + check_process(result, exit_code=0, stdout="PATH_CASE_OK\n") + + +@pytest.mark.parametrize( + "key", ["DOTENV_CASE_VALUE", "Dotenv_Case_Value", "dotenv_case_value"] +) +@pytest.mark.parametrize("override", [True, False]) +def test_run_environment_key_case(tmp_path, key, override): + (tmp_path / ".env").write_text(f"{key}=child\n") + env = {**os.environ, "DOTENV_CASE_VALUE": "parent"} + result = run_dotenv( + [ + "run", + "--override" if override else "--no-override", + sys.executable, + "-c", + "import os; print(os.environ['DOTENV_CASE_VALUE']); " + f"print(os.environ[{key!r}])", + ], + cwd=tmp_path, + env=env, + ) + aliases_uppercase = sys.platform == "win32" or key == "DOTENV_CASE_VALUE" + uppercase_value = "child" if override and aliases_uppercase else "parent" + exact_value = uppercase_value if aliases_uppercase else "child" + check_process(result, exit_code=0, stdout=f"{uppercase_value}\n{exact_value}\n") + + @pytest.mark.skipif(sys.platform != "win32", reason="Windows-specific lookup") def test_run_python_uses_path_on_windows(dotenv_path, tmp_path): """