Repository navigation
Conversation
|
Hi @phr4nky! Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 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.
c52d092 to
7500687
Compare
|
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.) |
…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
|
This pull request has been merged in 279c2c8. |
Summary
With the MSVC toolchain from
prelude/toolchains/msvc/tools.bzl, which wrapslink.exein this script, every link that includes alink_wholelibrary fails inprelude/cxx/tools/linker_wrapper.pywithKeyErrorbeforelink.exeruns.get_link_whole_argsinprelude/cxx/linker.bzlpasses/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
/WHOLEARCHIVEwith a path and ship the same wrapper. First bad commit: 2c8639e ("Pass whole path to /WHOLEARCHIVE?"), which changedlinker.bzlfrom passing the library plus/WHOLEARCHIVE:foo.libto passing/WHOLEARCHIVE:buck-out\...\foo.libalone, without changing the wrapper.Reproduction
The wrapper only runs on Windows; this drives it through its command line with
ntpathstanding in for the wrapper'sos.pathand a stub instead oflink.exe:Expected
link.exeis called with/WHOLEARCHIVE:followed by the absolute path offoo.lib. Passing the path is intended: 2c8639e madelinker.bzlpass it because lld-link could not find libraries by basename alone.Actual
The same argsfile with the plain library path instead of
/WHOLEARCHIVE:links.Analysis
mainrecords every argument ending in.libunder its basename, thenexpand_library_pathlooks up everything after the first:of the/WHOLEARCHIVEargument. A path is never a basename key, so the lookup raises. The/WHOLEARCHIVE:...argument itself ends in.liband was also recorded as a library under its basename.Patch
This PR keeps a path after
/WHOLEARCHIVE:as it is (a relativebuck-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 intorewrite_argsso it can be tested.Test plan: new
prelude/cxx/tools/tests/test_linker_wrapper.py(targetprelude//cxx/tools/tests:test_linker_wrapper), 7 tests run withpython3 -m unittest; themain()test fails on main with theKeyErrorabove and passes with the patch. Not run on a Windows machine.