dotenv run --no-override expands variables with the wrong precedence

未关闭
#697 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
python
领域
cli

调研方向

从 issue 中描述的 CLI run 路径开始,其中 dotenv_values() 会在环境键被筛选之前展开变量。运行提供的复现,并验证 DOTENV_TEST_BASEDOTENV_TEST_DERIVED 都使用现有的环境值,包括该值为空的情况。

由索引模型根据 Issue 内容生成。

描述

Description

With dotenv run --no-override, a variable can keep its value from the environment while another variable referring to it gets the value from .env. This leaves the child process with inconsistent configuration.

Steps to reproduce

With python-dotenv[cli] installed, run:

import os
import subprocess
import sys
import tempfile
from pathlib import Path

with tempfile.TemporaryDirectory() as directory:
    path = Path(directory) / ".env"
    path.write_text(
        "DOTENV_TEST_BASE=file\n"
        "DOTENV_TEST_DERIVED=${DOTENV_TEST_BASE}/suffix\n"
    )
    env = dict(os.environ)
    env["DOTENV_TEST_BASE"] = "environment"
    env.pop("DOTENV_TEST_DERIVED", None)
    subprocess.run(
        [
            sys.executable, "-m", "dotenv", "-f", str(path),
            "run", "--no-override", sys.executable, "-c",
            "import os; print(os.environ['DOTENV_TEST_BASE']); "
            "print(os.environ['DOTENV_TEST_DERIVED'])",
        ],
        env=env,
        check=True,
    )

Expected behavior

Both values should use the existing environment variable, as they do with load_dotenv(override=False):

environment
environment/suffix

Actual behavior

environment
file/suffix

An existing empty string also reproduces this: the derived value is file/suffix instead of /suffix.

Environment

Reproduced on Windows with Python 3.13.15 and python-dotenv 1.2.3, from commit a00cb2eed0704cd6d2071b2004c37e95ccc86ee5.

Additional context

This is similar to #282, but affects the CLI. run calls dotenv_values(), which expands variables with override=True, before filtering out keys already in the environment. The fix is in #698.

Prepared with OpenAI Codex assistance.

主要语言
Python
星标
8.9k
派生
581
PR 合并指标
30 天内没有已合并 PR

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

theskumar/python-dotenv 的其他 Issue

查看 theskumar/python-dotenv 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。