Skip to content

[tools] fix WinError 206 caused by overlong command lines on Windows - #12085

Open
CXSforHPU wants to merge 1 commit into
RT-Thread:masterfrom
CXSforHPU:fix_link_too_long
Open

CXSforHPU wants to merge 1 commit into
RT-Thread:masterfrom
CXSforHPU:fix_link_too_long

Conversation

@CXSforHPU

Copy link
Copy Markdown
Contributor

拉取/合并请求描述:(PR description)

[

为什么提交这份PR (why to submit this PR)

Windows 的 CreateProcess 单条命令行上限为 32767 字符。工程规模较大(组件多、目标文件多)时,链接/归档阶段的命令行会超限,tools/win32spawn.py 中的 subprocess.Popen(cmdline) 抛出 [WinError 206] 文件名或扩展名太长,构建直接中断。

现有实现只把 LINKCOM / ARCOM 交给 SCons 的 TEMPFILE 包装,其余动作(编译、BSP 在 PrepareBuilding 之后又替换 LINKCOM 的情况、--buildlib、自定义 env.Command)仍会踩到同一个上限。同时旧代码把 206 映射出的 errno 2 打印成 “No such file or directory,请检查工具链 PATH”,把真实原因掩盖了,排查成本很高。

你的解决方案是什么 (what is your solution)

把兜底下沉到唯一会抛出该错误的地方——Win32Spawn.spawn():

  • 命令行长度超过 32000 字符且工具链为 GCC 时,把参数逐行写入临时 @response 文件,以 gcc @"" 执行,执行完删除;参数中的 \ 转为 /(GCC 的 @file 解析器把反斜杠当转义符)。

  • 由 PrepareBuilding / PrepareModuleBuilding 传入 rtconfig.PLATFORM == 'gcc' 决定是否启用。armcc / IAR 不支持 @file,保持原行为;Linux 及其他平台完全不走该分支,无行为变化。

  • 覆盖范围从“仅链接/归档”扩大到所有经 SCons 动作 spawn 的命令,因此 BSP 后续改写 LINKCOM、--buildlib 等场景一并覆盖。

  • 错误输出改为打印真实异常 str(e)(含 WinError 编号);只有真正的 ENOENT(且 winerror != 206)才提示检查 PATH。

涉及文件:tools/win32spawn.py、tools/building.py。

验证结果(真实 arm-none-eabi-gcc 10.3.1,独立 SCons 动作级复现,含空格路径与反斜杠路径):

场景 修复前 修复后
链接 1200 个目标(命令行 78260 字符) 失败,[WinError 206] 成功生成 elf
ar 归档 1200 个目标(78257 字符) 失败,[WinError 206] 成功生成 .a
短命令 正常 正常,不生成响应文件
工具本身报错 错误码丢失 错误码正常传播,无临时文件残留

说明:编译命令自身超过 32K 时不再出现 Python 层 206,而是转由 gcc 内部 spawn as 报错(gcc 自身限制);正常工程编译行远低于该阈值,实际溢出点是链接与归档。

请提供验证的bsp和config (provide the config and bsp)

  • BSP: 用 bsp/qemu-vexpress-a9 或任一 Windows + GCC BSP 做回归;该改动只在 win32 + gcc 生效,Linux CI 为 no-op
  • .config: 无改动
  • action:

]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用clang-format 源码格式化工具确保格式符合RT-Thread代码规范 This PR has been formatted with clang-format and complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

Windows CreateProcess rejects a command line longer than 32767
characters, so link and archive steps failed with "WinError 206:
filename or extension too long" on large RT-Thread projects.

Win32Spawn now spills an overlong GCC command line into an @response
file, which covers every build action instead of only LINKCOM/ARCOM.
The misleading "check Toolchains PATH" hint is also gone: the real
exception is printed now.
@CXSforHPU

Copy link
Copy Markdown
Contributor Author

#6569

关联 issue

@github-actions

Copy link
Copy Markdown

👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!

为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。
To ensure your code complies with RT-Thread's coding style, please run the code formatting workflow by following the steps below (If the formatting of CI fails to run).


🛠 操作步骤 | Steps

  1. 前往 Actions 页面 | Go to the Actions page
    点击进入工作流 → | Click to open workflow →

  2. 点击 Run workflow | Click Run workflow

  • Use workflow from 保持默认分支(通常为 master)
    Keep the default branch (usually master) in Use workflow from
  • 在 branch 输入框填写 PR 分支 fix_link_too_long
    Enter PR branch fix_link_too_long in the branch field
  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将作为独立提交推送至你的分支。
    The formatting changes will be pushed to your branch as a separate commit.

完成后,提交将自动更新至 fix_link_too_long 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the fix_link_too_long branch automatically, and the related Pull Request will be updated.

如有问题欢迎联系我们,再次感谢您的贡献!💐
If you have any questions, feel free to reach out. Thanks again for your contribution!

@github-actions github-actions Bot added the tools label Nov 24, 2026
@github-actions

Copy link
Copy Markdown

@BernardXiong BernardXiong left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

实现思路合理:只在 GCC 且命令行超限时使用 @response 文件,ARMCC/IAR 保持原行为,finally 中删除临时文件,并把 WinError 206 与真实 ENOENT 的提示区分开,这些改动没有问题。

非阻塞建议,位于 _write_response_file():

  1. 参数以 '\n'.join(...) 直接拼接,未对含空白的参数加引号。GCC 的 @file 解析按空白拆分参数,Windows 用户名、Program Files 等含空格的路径会被拆成多个参数。旧的非 rsp 路径同样存在该限制,但既然新增了 response file 路径,建议对含空白的参数加双引号(GCC response file 支持引号分组)。
  2. 把参数中的 \ 全部替换为 / 会改变 -D 宏等非路径参数的值。建议按 GCC @file 规则把 \ 转义为 \\,而不是改写参数内容。

以上不阻塞合入,供作者权衡。

This branch has not been deployed

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants