Skip to content

Escape generated build environment metadata - #714

Open
Mr-Neutr0n wants to merge 2 commits into
deepseek-ai:mainfrom
Mr-Neutr0n:fix/persistent-env-escaping
Open

Escape generated build environment metadata#714
Mr-Neutr0n wants to merge 2 commits into
deepseek-ai:mainfrom
Mr-Neutr0n:fix/persistent-env-escaping

Conversation

@Mr-Neutr0n

Copy link
Copy Markdown

Problem

CustomBuildPy interpolates persistent environment values directly into single-quoted Python literals when generating deep_ep/envs.py. A valid cache path containing an apostrophe produces invalid Python, while backslashes can silently change the stored value.

Fix

Extract the source renderer and use Python repr for both environment names and values. Only the existing persistent-environment allowlist is emitted.

Regression coverage

The host-only tests compile and execute the generated source, then verify exact round trips for apostrophes, backslashes, and spaces. They also verify that unrelated environment values are ignored.

Validation

  • python -m unittest tests.utils.test_setup_helpers (2 passed)
  • ruff check tests/utils/test_setup_helpers.py
  • Python compilation and git diff --check

Prepared with OpenAI Codex assistance; I reproduced the invalid generated source and reviewed the serialized output.

Comment thread setup.py
code = render_default_envs(os.environ)

# Create temporary build directory
build_include_dir = os.path.join(self.build_lib, 'deep_ep')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 suggestion: generate_default_envs 中 build_include_dir 变量创建后仅用于 os.makedirs,随后 open 又重新用 os.path.join(self.build_lib, 'deep_ep', ...) 拼了一次相同路径;可复用 build_include_dir 减少重复。非阻塞。

🤖 v5


def load_setup_module():
path = Path(__file__).parents[2] / 'setup.py'
spec = importlib.util.spec_from_file_location('deep_ep_setup_under_test', path)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 suggestion: load_setup_module 会执行 setup.py 的全部顶层代码,包括 import torch.utils.cpp_extension 和加载 find_pkgs 模块,测试因此隐式依赖 torch 安装。当前环境可用(已验证 2 passed),但若未来希望在无 torch 的 CI 上运行这些 host-only 测试,可考虑把 render_default_envs 与 persistent_env_names 移入独立的轻量模块。非阻塞。

🤖 v5

@ds-review-bot

Copy link
Copy Markdown
Collaborator

🤖 ds-review-bot Code Review

v6

该变更使用 repr 安全序列化允许列表中的环境变量名和值,能够保持特殊字符的精确往返,同时未改变原有生成流程。未发现会破坏现有功能的缺陷。

v4

⚠️ 未完成评审(no_result_file:模型未产出结果文件)

v5

此 MR 修复了 CustomBuildPy 生成 deep_ep/envs.py 时的转义缺陷:旧实现将持久化环境变量的值直接内插进单引号 Python 字面量,含撇号的路径会生成非法源码,含反斜杠的值会被静默改变。修复将渲染逻辑抽取为模块级纯函数 render_default_envs(environ),对键和值统一使用 Python repr({name!r} / {environ[name]!r}),并保持只输出既有的 persistent_env_names 白名单,无关环境变量不会被写入。新增的 host-only 测试通过 compile(source, 'envs.py', 'exec') 真正执行生成源码,验证撇号+反斜杠(/tmp/o'neil\jit-cache)、空格(/opt/NCCL dir)值的精确往返,以及白名单外变量被忽略,覆盖了描述中的全部回归场景。变更范围小、合法值下输出语义不变、可测性重构方向正确,建议合并。验证:python -m unittest tests.utils.test_setup_helpers(2 passed)、ruff check 通过、git diff --check 无问题。

Files reviewed: 2
Issues found: 🔵 2 suggestion
Inline comments posted: 2

⚠️ Parse warning: [v4] no_result_file:模型未产出结果文件

@Mr-Neutr0n

Copy link
Copy Markdown
Author

Addressed the review: generate_default_envs now reuses build_include_dir. I kept the renderer in setup.py because it is setup-only and splitting out a module solely to avoid the existing Torch build dependency would widen this fix. Mr-Neutr0n@ecbce8f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants