cargo:rustc-link-args not applied to doctest builds #9895
Labels
A-build-scripts
Area: build.rs scripts
A-doctests
Area: rustdoc --test
C-bug
Category: bug
E-easy
Experience: Easy
I'm not sure if this is a bug or not, but it doesn't seem like desired behavior.
The docs for cargo:rustc-link-arg say:
Are doctest binaries intended to be supported or not? They seem to fall under "tests". Empirically, it looks like the requested linker flags are not passed to the compiler when building doctests. The rest of this comment shows why I think this.
To show this, I created this repo. This is a library crate that depends on pq-sys (which just happens to be the crate I discovered this with). This test crate has one function that uses something in the native libpq, wrapped by pq-sys. There's a unit test and a doctest that use this function.
My libpq is installed in "/opt/ooce/pgsql-13/lib/amd64", which is not on ld.so's default search path, so I need to build with
-R/opt/ooce/pgsql-13/lib/amd64
, which means passing-Wl,-R/opt/ooce/pgsql-13/lib/amd64
to gcc. To achieve this, my test crate has a build.rs that uses cargo:rustc-link-arg. To make it easier to show the problem, the build.rs script looks at the environment variable TEST_LIBPQ_DIR. If it's set, it usescargo:rustc-link-arg
to set the corresponding linker flag. If the environment variable is not set, the build.rs does nothing.I'm using:
If I just run
cargo +nightly test
, the unit test fails because ld.so can't find libpq:This means our linker flag was not passed -- that's expected.
Now, if I use RUSTFLAGS and RUSTDOCFLAGS to specify the linker args, everything works:
This is also expected -- we forced rustc and rustdoc to pass gcc the right linker arg so that it would find the library.
What's surprising is that if I set my environment variable so that build.rs uses
cargo:rustc-link-args
, then the unit test works, but the doctest doesn't:Since the unit test passed, we know the linker arg was passed correctly for that. We can also see that the test binary has the right directory on the RUNPATH:
But the doctest build doesn't.
To be really sure, here's the very verbose output:
The rustc invocation for building the unit test includes
-C link-arg=-Wl,-R/opt/ooce/pgsql-13/lib/amd64
but the one for rustdoc does not. If I take the rustdoc invocation, add "+nightly" and-C link-arg=-Wl,-R/opt/ooce/pgsql-13/lib/amd64
by hand, then the rustdoc test works:Anyway, I expected that the link args from "cargo:rustc-link-args" would apply to rustdoc tests, but they don't seem to be. Is this a bug?
The text was updated successfully, but these errors were encountered: