Skip to content

prelude: Keep the library path in /WHOLEARCHIVE in the MSVC linker wrapper - #1655

Closed
phr4nky wants to merge 1 commit into
meta:mainfrom
phr4nky:fix-linker-wrapper-wholearchive
Closed

phr4nky wants to merge 1 commit into
meta:mainfrom
phr4nky:fix-linker-wrapper-wholearchive

Conversation

@phr4nky

@phr4nky phr4nky commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Summary

With the MSVC toolchain from prelude/toolchains/msvc/tools.bzl, which wraps link.exe in this script, every link that includes a link_whole library fails in prelude/cxx/tools/linker_wrapper.py with KeyError before link.exe runs. get_link_whole_args in prelude/cxx/linker.bzl passes /WHOLEARCHIVE:buck-out\...\foo.lib (a path), but the wrapper looks the value up in a table keyed by basename.

Affected version

Checked on main at 4b47e14 and on the 2026-11-15 release; both emit /WHOLEARCHIVE with a path and ship the same wrapper. First bad commit: 2c8639e ("Pass whole path to /WHOLEARCHIVE?"), which changed linker.bzl from passing the library plus /WHOLEARCHIVE:foo.lib to passing /WHOLEARCHIVE:buck-out\...\foo.lib alone, without changing the wrapper.

Reproduction

The wrapper only runs on Windows; this drives it through its command line with ntpath standing in for the wrapper's os.path and a stub instead of link.exe:

cd prelude/cxx/tools
printf '%s\n' '"/OUT:app.exe"' '"/WHOLEARCHIVE:buck-out\v2\gen\root\lib\__foo__\foo.lib"' > /tmp/link.argsfile
python3 -c '
import ntpath, os, subprocess, sys, types, linker_wrapper
linker_wrapper.os = types.SimpleNamespace(path=ntpath, getcwd=os.getcwd)
subprocess.run = lambda cmd, **kw: print("link.exe", open(cmd[1][1:], "rb").read().decode("utf-16").split())
sys.argv = ["linker_wrapper", "link.exe", "@/tmp/link.argsfile"]
linker_wrapper.main()'

Expected

link.exe is called with /WHOLEARCHIVE: followed by the absolute path of foo.lib. Passing the path is intended: 2c8639e made linker.bzl pass it because lld-link could not find libraries by basename alone.

Actual

  File "linker_wrapper.py", line 38, in expand_library_path
    return "/WHOLEARCHIVE:" + full_library_paths[library_name]
KeyError: 'buck-out\\v2\\gen\\root\\lib\\__foo__\\foo.lib'

The same argsfile with the plain library path instead of /WHOLEARCHIVE: links.

Analysis

main records every argument ending in .lib under its basename, then expand_library_path looks up everything after the first : of the /WHOLEARCHIVE argument. A path is never a basename key, so the lookup raises. The /WHOLEARCHIVE:... argument itself ends in .lib and was also recorded as a library under its basename.

Patch

This PR keeps a path after /WHOLEARCHIVE: as it is (a relative buck-out\ path is made absolute like every other argument, for paths over 260 characters), expands only a basename that names a library given earlier, and no longer records the option itself as a library. The argument rewriting moves into rewrite_args so it can be tested.

Test plan: new prelude/cxx/tools/tests/test_linker_wrapper.py (target prelude//cxx/tools/tests:test_linker_wrapper), 7 tests run with python3 -m unittest; the main() test fails on main with the KeyError above and passes with the patch. Not run on a Windows machine.

@meta-cla

meta-cla Bot commented Nov 25, 2026

Copy link
Copy Markdown

Hi @phr4nky!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

…apper

`get_link_whole_args` passes `/WHOLEARCHIVE:<path>` for every link_whole
archive with the windows linker type (since 2c8639e), but
`linker_wrapper.py`, which the MSVC toolchain wraps around link.exe, looked
the value up in a table keyed by basename. A path is never a key there, so
every link of a link_whole library failed with a KeyError.

Keep a path after `/WHOLEARCHIVE:` as it is (making a relative buck-out path
absolute, as for every other argument), expand only a basename that names a
library given earlier, and do not record the `/WHOLEARCHIVE:...` option
itself as a library.
@phr4nky
phr4nky force-pushed the fix-linker-wrapper-wholearchive branch from c52d092 to 7500687 Compare November 25, 2026 21:48
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 25, 2026
@meta-codesync

meta-codesync Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D134594241. (Because this pull request was imported automatically, there will not be any future comments.)

@dtolnay dtolnay left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks!

meta-codesync Bot pushed a commit to meta/buck2-prelude that referenced this pull request Nov 28, 2026
…apper

Summary:
With the MSVC toolchain from `prelude/toolchains/msvc/tools.bzl`, which wraps `link.exe` in this script, every link that includes a `link_whole` library fails in `prelude/cxx/tools/linker_wrapper.py` with `KeyError` before `link.exe` runs. `get_link_whole_args` in `prelude/cxx/linker.bzl` passes `/WHOLEARCHIVE:buck-out\...\foo.lib` (a path), but the wrapper looks the value up in a table keyed by basename.

## Affected version

Checked on main at 4b47e1430e and on the 2026-11-15 release; both emit `/WHOLEARCHIVE` with a path and ship the same wrapper. First bad commit: 2c8639e28c ("Pass whole path to /WHOLEARCHIVE?"), which changed `linker.bzl` from passing the library plus `/WHOLEARCHIVE:foo.lib` to passing `/WHOLEARCHIVE:buck-out\...\foo.lib` alone, without changing the wrapper.

## Reproduction

The wrapper only runs on Windows; this drives it through its command line with `ntpath` standing in for the wrapper's `os.path` and a stub instead of `link.exe`:

```
cd prelude/cxx/tools
printf '%s\n' '"/OUT:app.exe"' '"/WHOLEARCHIVE:buck-out\v2\gen\root\lib\__foo__\foo.lib"' > /tmp/link.argsfile
python3 -c '
import ntpath, os, subprocess, sys, types, linker_wrapper
linker_wrapper.os = types.SimpleNamespace(path=ntpath, getcwd=os.getcwd)
subprocess.run = lambda cmd, **kw: print("link.exe", open(cmd[1][1:], "rb").read().decode("utf-16").split())
sys.argv = ["linker_wrapper", "link.exe", "@/tmp/link.argsfile"]
linker_wrapper.main()'
```

## Expected

`link.exe` is called with `/WHOLEARCHIVE:` followed by the absolute path of `foo.lib`. Passing the path is intended: 2c8639e28c made `linker.bzl` pass it because lld-link could not find libraries by basename alone.

## Actual

```
  File "linker_wrapper.py", line 38, in expand_library_path
    return "/WHOLEARCHIVE:" + full_library_paths[library_name]
KeyError: 'buck-out\\v2\\gen\\root\\lib\\__foo__\\foo.lib'
```

The same argsfile with the plain library path instead of `/WHOLEARCHIVE:` links.

## Analysis

`main` records every argument ending in `.lib` under its basename, then `expand_library_path` looks up everything after the first `:` of the `/WHOLEARCHIVE` argument. A path is never a basename key, so the lookup raises. The `/WHOLEARCHIVE:...` argument itself ends in `.lib` and was also recorded as a library under its basename.

## Patch

This PR keeps a path after `/WHOLEARCHIVE:` as it is (a relative `buck-out\` path is made absolute like every other argument, for paths over 260 characters), expands only a basename that names a library given earlier, and no longer records the option itself as a library. The argument rewriting moves into `rewrite_args` so it can be tested.

X-link: meta/buck2#1655

Reviewed By: dtolnay

Differential Revision: D134594241

fbshipit-source-id: 604fb0e3c3f34c6c782b358da27c714d55a59a3a
@meta-codesync meta-codesync Bot closed this in 279c2c8 Nov 28, 2026
@meta-codesync meta-codesync Bot added the Merged label Nov 28, 2026
@meta-codesync

meta-codesync Bot commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

This pull request has been merged in 279c2c8.

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants