Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/dotenv/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
39 changes: 39 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import shutil
import sys
from pathlib import Path
from typing import Optional
Expand Down Expand Up @@ -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):
"""
Expand Down