Skip to content

fix(build): send the MinGW linker --stack, not MSVC's /STACK: - #3613

Merged
KuSh merged 4 commits into
rtk-ai:developfrom
rhishi99:fix/build-mingw-stack-arg
Oct 14, 2026
Merged

KuSh merged 4 commits into
rtk-ai:developfrom
rhishi99:fix/build-mingw-stack-arg

Conversation

@rhishi99

Copy link
Copy Markdown
Contributor

Problem

The Windows stack-size reservation in build.rs has two toolchain bugs that only surface away from MSVC.

1. Host vs target. #[cfg(windows)] inside a build script describes the machine running the build, not the machine the binary is for. Cross-compiling to Windows from Linux silently skipped the flag; a Linux-targeted build on a Windows host wrongly emitted it.

2. Wrong flag for the GNU toolchain. link.exe understands /STACK:. The MinGW ld behind x86_64-pc-windows-gnu does not — it reads the argument as a filename:

ld.exe: cannot find /STACK:8388608: No such file or directory
collect2.exe: error: ld returned 1 exit status

Any cargo build/cargo test on x86_64-pc-windows-gnu fails to link. CI never caught this because windows-latest defaults to MSVC.

Fix

  • Key the platform check off CARGO_CFG_TARGET_OS (the target) rather than cfg!(windows) (the host).
  • Emit -Wl,--stack,<bytes> when CARGO_CFG_TARGET_ENV is gnu, keep /STACK:<bytes> otherwise.

Behaviour on MSVC is unchanged — same flag, same 8 MiB.

Testing

No unit test: this is build-script output, observable only through the linker invocation itself. Verified by building the workspace on stable-x86_64-pc-windows-gnu, which fails to link before the change and succeeds after:

cargo test: 2748 passed, 8 ignored (10 suites)

cargo fmt --check and cargo clippy --all-targets clean.

🤖 Generated with Claude Code

The Windows stack-size reservation had two problems that only surface off
the MSVC toolchain.

`#[cfg(windows)]` in a build script describes the machine running the
build, not the machine the binary is for, so cross-compiling to Windows
from Linux silently skipped the flag while a Linux-targeted build on a
Windows host wrongly emitted it. Key the check off CARGO_CFG_TARGET_OS
instead, which is the target.

The flag itself is also toolchain-specific. `link.exe` understands
`/STACK:`, but the MinGW `ld` behind `x86_64-pc-windows-gnu` does not --
it reads the argument as a filename and fails the link with:

    ld.exe: cannot find /STACK:8388608: No such file or directory

Select `-Wl,--stack,<bytes>` when CARGO_CFG_TARGET_ENV is `gnu` and keep
`/STACK:` otherwise. CI never caught this because windows-latest defaults
to MSVC.

Co-Authored-By: Claude Opus 5 <[email protected]>
@CLAassistant

CLAassistant commented Aug 20, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

KuSh and others added 2 commits October 13, 2026 00:46
…tion

Every Windows target reports target_env `msvc` or `gnu` (gnullvm reports
`gnu`), so msvc is the only one with its own spelling. Matching on it makes any
other environment take the `-Wl,--stack` form the gcc and clang drivers accept.

Read the reservation back from the built rtk.exe's PE header in a Windows-only
test, so a linker that rejects or drops the flag fails a test rather than
shipping a 1 or 2 MiB stack.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The test matrix links windows with link.exe only, so a stack flag that GNU ld
rejects (the failure build.rs used to have) cannot fail CI. Add a windows-latest
leg that tests x86_64-pc-windows-gnu; the runner's host stays msvc, so build.rs
also runs with host and target apart.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

@KuSh KuSh left a comment

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.

Round 1 — APPROVED

Claim: build.rs reserves the 8 MiB Windows main-thread stack with the spelling the target's linker takes, and decides on the build target rather than the host, so x86_64-pc-windows-gnu links and a cross-build to Windows keeps the reservation.
Scope: accept. #3239 and #2895 made the same change and were closed in favour of this PR; #3239 emits identical link args on the 20 *-windows-* targets, and #2895 gated on the host instead of the target.
Ran: x86_64-pc-windows-gnu cross-built from Linux (mingw) at develop, at the head you pushed and at the merge with develop: SizeOfStackReserve 2 MiB → 8 MiB, on rtk.exe and on the test executable. The verbatim set_windows_stack_size over the 20 Windows targets plus linux, macos, wasi and an unset env. GNU ld, lld-link, ld.lld and clang on a COFF object, for the spelling each takes. Then the real runners (below). Invariants: non-default targets, every decision. Not applicable: savings floor, under the hook, split streams.
Pins: 3 decisions, 5 mutated, 0 survive with the added test (the head you pushed had no test, so all 3 were unpinned).
Matrix: executed this round; pin check: executed this round
CI @885050598: all green, 12 jobs and the CLA check. The new stack test ran and passed (1 test) on test (windows-latest) (link.exe) and on test (windows-latest, x86_64-pc-windows-gnu) (MinGW), both 37 suites, 4167 passed, 0 failed.

Blocks merge (0)

Fixup pushed (maintainerCanModify)

Three commits on the branch, none rewrites yours:

  • 7bf595fdf build.rs special-cases msvc (/STACK:N) and gives every other Windows env -Wl,--stack,N. Output is the same on all 20 targets; only an unset or unknown target_env, which no stable target has, now gets the -Wl form. tests/windows_stack_reserve_test.rs reads SizeOfStackReserve back from the running test executable (64-bit Windows, PE32+). It reads the test executable and not rtk.exe because core::test_isolation refuses any file outside tests/common that names CARGO_BIN_EXE_rtk; both come from the same rustc-link-arg.
  • a18bea5ab ci.yml gets a windows-latest leg with x86_64-pc-windows-gnu in the existing test matrix, so the MinGW link is built and tested. CARGO_BUILD_TARGET: ${{ matrix.target || 'host-tuple' }} is set for every leg, so the run command stays cargo test --all; an explicit target puts each leg in cross mode, where RUSTFLAGS no longer reach build scripts or proc-macros (CI sets none).
  • 885050598 merges develop into the branch. semgrep takes its baseline from the PR's merge-base, which was 29f9bb716, so it reported develop's own src/hooks/init/opencode.rs:257 as new; with the merge-base on develop's tip it passes.

Mutations on the new code, each read back through the test's own header parser:

  • target gate removed: every Linux link fails on the -Wl,--stack argument
  • default arm emits nothing: gnu test executable reads 2 MiB, test fails
  • default arm back to /STACK:: GNU ld rejects it, the link fails
  • msvc arm emits the GNU spelling, and msvc arm emits nothing: lld-link images read 1 MiB, test fails (not run against link.exe itself; the msvc leg above reads the passing case)

Checked and correct — no need to re-verify

  • Every Windows target reports target_env gnu or msvc (gnullvm reports gnu, with target_abi llvm), so both spellings cover all 20 targets.
  • GNU ld rejects /STACK:N outright; lld-link ignores -Wl,--stack,N with a warning and keeps its default. Routing on msvc keeps msvc-flavoured linkers on /STACK:.
  • Default reservation: 2 MiB with GNU ld, 1 MiB with link.exe and ld.lld. Startup needs about 220 KiB (release) and about 2 MiB (debug) of main-thread stack on Linux, so the reservation matters for debug and test builds.
  • #[cfg(windows)] in a build script describes the host: develop cross-built from Linux emits no stack argument at all.
  • linker-flavor=ld does not link with or without the flag, so it is not something this change affects. #[cfg(test)] inside build.rs is never run by cargo test, which is why the pin is an integration test.

Your questions

None from you. One for you: would you like Fixes #3215 in the description? #1814 reports the same link failure.

Next

fixup 7bf595fdf, a18bea5ab and the merge 885050598 pushed, code-reviewed: it inverts the match, adds the stack test and a gnu CI leg, and moves the semgrep baseline. Approving.
Rounds: 1. Threads: 0 open.

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.

3 participants