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

MemBrowse Memory Report · Explore what changed →

simulator

  • Code: .eh_frame -4 B, .rodata +3 B (-0.0%, 2,677,226 B)
  • Data: .bss +28 B (+0.0%, 1,611,136 B)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@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 的引号分组,建议一并调整。

其余部分未见阻塞问题。

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.
Backslashes are doubled and arguments containing whitespace are quoted
so the @file reproduces the original argument list. The misleading
"check Toolchains PATH" hint is also gone: the real exception is
printed now.

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