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自查

@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

github-actions Bot commented Nov 24, 2026 •

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 规则把 \ 转义为 \\,而不是改写参数内容。

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

@CXSforHPU

Copy link
Copy Markdown
Contributor Author

两条非阻塞建议均已采纳:

def _response_file_arg(arg):
    # GCC's @file reader (buildargv) treats '\' as an escape character,
    # unlike the Windows command line, so backslashes must be doubled.
    arg = arg.replace('\\', '\\\\')
    # Arguments SCons already quoted keep their own grouping.
    if '"' not in arg and (' ' in arg or '\t' in arg):
        arg = '"' + arg + '"'
    return arg
  1. \ 转义:不再改写为 /,改为按 buildargv 规则转义为 \\,-D 宏等非路径参数取值不再被改写。
  2. 空白参数加引号:含空白的裸参数用双引号分组;但已含 " 的 token 不再二次加引号。

关于第 2 点的边界:实测 SCons 4.11.1 对字符串命令是按空白分词、引号字符保留在 token 内(-o "a b.elf" 到达 SPAWN 时是 "a、b.elf" 两个 token),只有 Literal 节点才会被 win32 ESCAPE 预先加引号。因此只对"不含引号的 token"补引号:对跨 token 的引号分组原样透传,对已加引号的 Literal 不做二次包裹。

验证(arm-none-eabi-gcc 10.3.1):-DSTR=a\b、-DPATH=C:\x\y、-DSPACED=a b 三种参数,普通命令行与 @file 两条路径的 -dM -E 宏展开、字符串化结果完全一致(rsp 内容为 -DSTR=a\\b / -DPATH=C:\\x\\y / "-DSPACED=a b");回归用例全部通过:78K 字符 link、78K ar 超长命令成功且临时文件无泄漏,短命令与非 gnu 模式行为不变,工具错误码正常透传。

残留边界仅是"SCons 已加引号且以 \ 结尾的 Literal 参数"会多一个尾反斜杠,RT-Thread 构建命令中不出现这种形式。

@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.

Review / 审查 — 0fc7d821

已复核新提交:_response_file_arg() 的 buildargv 反斜杠转义、临时 rsp 的清理、以及异常路径的返回值处理均正确,两条非阻塞建议已落实。CI 97 success / 3 skipped。

非阻塞 / Non-blocking:_write_response_file() 用 '\n'.join() 分隔参数(tools/win32spawn.py:44)。GCC 的 @file 解析中,引号内的空白属于参数内容、不会作为分隔符;按你描述的 SCons 行为(-o "a b.elf" 到达 SPAWN 时拆成 "a、b.elf" 两个 token),这两个 token 会被写成 "a\nb.elf",解析结果是含换行的单个参数 a\nb.elf,而不是 a b.elf。该路径只在命令行超过 32000 字符且参数含空格时触发,影响很小;若改用空格连接(' '.join(...)),既能保留已加引号参数的分组,也能原样还原跨 token 的引号分组,建议一并调整。

其余部分未见阻塞问题。

@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.

复核 ba7bfee 的 Win32 长命令行修复:

  • Win32Spawn 仅在 GCC 且命令行超过 32000 字符时生成 @response 文件,路径取 mkstemp 返回的绝对路径,正常/异常路径均由 finally 清理;
  • @file 中反斜杠按 GCC buildargv 规则双写,含空白参数补引号,SCons 已引用的参数保持原样;写入内容与原有 args[1:] 一致;
  • building.py 已 import rtconfig,PrepareBuilding/PrepareModuleBuilding 传入 GCC 判断,armcc/IAR 行为不变。
    非阻塞:PR 描述写“反斜杠转为 /”,实际实现为双写反斜杠,建议后续同步描述。CI 绿,批准。

Comment thread tools/win32spawn.py Resolved

@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.

Review / 审查 — 9104dc04(增量)

Rbb666 指出的 @response 转义问题已按正确顺序修复:

  • _windows_argv() 先用 Windows CRT 规则把 SCons 命令行还原成程序实际收到的 argv;
  • _response_file_arg() 再按 GCC buildargv 规则转义写入响应文件,含空白参数用引号分组;
  • Win32Spawn.init() 仅在 GCC 且命令行超过 32000 字符时启用,临时文件在 finally 中删除。

-DBUILD_ID=\"test\"、含空格参数、路径结尾反斜杠等场景的往返转换与短命令行一致;armcc/IAR(gnu_response_file=False)保持原路径。未发现新的阻塞问题。

CI 说明:armclang-stm32 与 RH850 :kernel/mem.cfg 均为基础设施失败(工具链 Version 3.8 was not found in the local cache、外部下载 curl ... 618、apt exit 8),与本 PR 的 Python 改动无关,建议重跑后再合并。

Comment thread tools/win32spawn.py
# GCC's @file reader (buildargv) always treats '\' as an escape
# character, so backslashes and quotes must be escaped; any whitespace
# (buildargv splits on all of it) needs a quoted group.
arg = arg.replace('\\', '\\\\').replace('"', '\\"')

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.

Windows ARM GCC 实测,-DCHR='A' 在短命令中展开为字符常量 'A',走响应文件后变成标识符 A。请给含单引号的参数加双引号,保留宏值。

Comment thread tools/win32spawn.py Outdated
cmdline = cmd + " " + newargs

rsp = None
if self.gnu_response_file and len(cmdline) > _WIN32_CMDLINE_LIMIT:

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 工程的所有命令都生效。实测一条可直接运行的 32166 字符 Python 命令被改写后,Python 把 @文件 当作脚本名,退出码为 2。请按实际执行的工具判断是否支持 GNU 响应文件。

Comment thread tools/win32spawn.py

return e.errno
return e.errno
return proc.wait()

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.

这里等子进程退出才恢复进程级 PATH。scons -j 下,若 A 启动、B 启动、A 结束、B 结束,B 会把父进程的 PATH 恢复成 A 的工具链路径。建议按 _e['PATH'] 解析可执行文件的绝对路径,避免并行任务共享这次修改。

@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.

Incremental review / 增量复核 — 9104dc04

复核 Rbb666 在本 head 新提的三条意见:

  1. tools/win32spawn.py:76:_response_file_arg() 只转义 \ 和 "。GCC 的 @file 解析使用 libiberty buildargv,它同时把单引号当作引号字符,因此 -DCHR='A' 写入响应文件后会被还原成 -DCHR=A。该问题成立,建议单引号一并转义,或在参数含 ' 时用双引号包裹。
  2. tools/win32spawn.py:110:响应文件开关按 rtconfig.PLATFORM == 'gcc' 对整个 GCC 工程启用,而不是按实际执行的工具判断。GCC 工程内仍可能 spawn 不支持 @file 的子进程(如 Python),超长时会被错误改写。风险成立,建议限定到已知支持 GNU 响应文件的驱动/工具,或对不支持的工具有回退。
  3. tools/win32spawn.py:134:进程级 os.environ['PATH'] 在 scons -j 并行下会互相覆盖。该写法在本次改动前即存在,非本 PR 引入,可作为后续改进,不阻塞本 PR。

其余结论与上轮一致。CI 中 RH850 / armclang-stm32 的失败仍是已知环境/基础设施问题,与本改动无关。

Windows CreateProcess rejects command lines longer than 32767
characters, breaking link and archive actions on large projects.

Use temporary GNU response files for overlong GCC and binutils
commands only. Resolve the executable exclusively against the action
PATH (or its explicit path), then check the resolved tool name; leave
Python, Clang and other helpers untouched. Pass the absolute executable
to Popen without changing process-wide PATH during parallel builds.

Decode raw tokens using the MinGW CRT rules, including consecutive
quotes, then encode them for buildargv with backslash/quote escaping
and double-quoted groups for whitespace, single quotes and empty args.
Write using the Windows ANSI code page independently of Python UTF-8
mode, and remove response files on write, launch and wait failures.
Print the actual launch exception without suggesting a PATH error for
WinError 206.

This removes the SCons-to-tool command-line limit. GCC may still hit
Windows limits when it launches its internal compiler or assembler;
response files do not remove those downstream toolchain limits.
@CXSforHPU

Copy link
Copy Markdown
Contributor Author
  1. 带单引号的宏定义丢引号
    原来生成参数文件的时候,会把 ' 当成引号删掉,导致 -DCHR='A' 传到 gcc 变成 -DCHR=A,和预期不一样。
    改成:只要参数里面有空格、单引号、或者是空内容,就整体套一层双引号,里面的单引号保留;反斜杠和双引号该转义还是转义。测了一堆引号、反斜杠组合,直接传参数和走响应文件给 gcc 的结果一模一样。
  2. 不是所有程序都支持 @响应文件
    之前的逻辑很粗暴:命令太长就自动丢进 @xxx 响应文件。但像 Python 根本不认这种语法,直接会出错。
    现在改成先看你实际跑的是什么程序:只有 gcc/g++、binutils 那几个工具(ar、ld、objcopy 等)才启用响应文件;Python、cmd、clang 这些不走这个逻辑。测试确认白名单工具正常生效,超长 Python 命令维持旧行为不变。
  3. 并发下改 PATH 有风险
    老代码会临时改环境变量 PATH 再改回去,多线程并发的时候容易乱。而且现在我们已经要精准定位工具路径,顺便把这块改掉。
    不再临时修改进程的 PATH;解析程序位置时只用当前任务自带的 PATH,找不到就直接报错,不偷偷回退用父进程 PATH;启动的时候直接给 Popen 填好找到的绝对路径。多线程并发跑不会互相干扰,也不会误跑到别的同名程序。

@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.

审阅当前 head ba97498961(完整 diff 仅 tools/building.py 2 行、tools/win32spawn.py +124/-18),CI 全部通过(35 pass / 2 skipped)。已逐一核对上一轮的四项问题:

  • 响应文件编码前先用 Windows CRT 规则还原 argv,再按 GCC buildargv 规则转义反斜杠/双引号,-DBUILD_ID=\"test\" 这类参数不会再被二次转义;
  • 含单引号的参数整体加双引号,-DCHR='A' 可保留字符常量语义;
  • @file 仅对 gcc/g++/ar/ld 等 GNU 工具启用,并由 rtconfig.PLATFORM == 'gcc' 收口,Python/cmd 不再被误改;
  • 不再修改进程级 PATH,改为按 action PATH 解析绝对路径并用 executable= 启动,避免 scons -j 下的并发污染。

本次为静态审阅,未在 Windows 上复跑作者给出的宏展开与并行构建用例;基于代码路径和 CI 结果未发现新的阻塞问题。

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.

3 participants