Skip to content

cdylib extra link args apply to unit tests #12663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wmmc88 opened this issue Sep 13, 2023 · 3 comments · Fixed by #15317
Closed

cdylib extra link args apply to unit tests #12663

wmmc88 opened this issue Sep 13, 2023 · 3 comments · Fixed by #15317
Labels
A-build-scripts Area: build.rs scripts A-linkage Area: linker issues, dylib, cdylib, shared libraries, so C-bug Category: bug S-needs-team-input Status: Needs input from team on whether/how to proceed.

Comments

@wmmc88
Copy link

wmmc88 commented Sep 13, 2023

Problem

According to The Cargo Book, the build.rs file can emit the cargo:rustc-link-arg-cdylib=FLAG setting to have it passed to rustc as a -C link-arg=FLAG but only when building a "cdylib library target". From the documentation, its not clear that building and running the test binaries for unittests in a cdylib crate will also apply -C link-arg=FLAG.

In my specific case, I am passing (among other linker arguments) rustc-link-arg-cdylib=/INTEGRITYCHECK to a cdylib crate via build.rs. Doing so also passes it to the unit tests, so it makes it impossible to execute my unit tests.

This is sort of the opposite problem of #10937. In that issue, they want cargo:rustc-link-arg-tests=FLAG to apply to unittests. In this issue, I want all the other rustc-link-arg variants to not apply to unit tests (or at least have some option that lets me do this).

Steps

  1. Create project with files:
    Cargo.toml:
[package]
name = "rustc-link-arg-bug"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

build.rs

fn main() {
    println!("cargo:rustc-link-arg-cdylib=/INTEGRITYCHECK");
}

src/lib.rs:
empty file

  1. Run Cargo Test:
cargo test
    Finished test [unoptimized + debuginfo] target(s) in 0.02s
     Running unittests src\lib.rs (target\debug\deps\rustc_link_arg_bug-d9c1a47e9d03aa01.exe)
error: test failed, to rerun pass `--lib`

Caused by:
  could not execute process `D:\rustc-link-arg-bug\target\debug\deps\rustc_link_arg_bug-d9c1a47e9d03aa01.exe` (never executed)

Caused by:
  Windows cannot verify the digital signature for this file. A recent hardware or software change might have installed a file that is signed incorrectly or damaged, or that might be malicious software from an unknown source. (os error 577)

cargo test fails to run because /INTEGRITYCHECK was passed to the unittest "targets" (Aside: are these unit test binaries considered a separate target in the world of cargo?)

note: /INTEGRITYCHECK is an msvc-specific linker argument

Possible Solution(s)

I think a way to control whether extra link args are passed or not passed to unit tests would be a good thing. The cargo documentation could also be improved as it pretty loose with the word "tests", and the way it is worded could confuse people (like me) as to what directives apply to tests(including unittests) and what applies only to integration tests(ie. test targets?).

Note: I would be very happy if anyone knows any workaround to this issue that allows me to pass linker arguments only to the cdylib and not to the cdylib's tests.

Notes

No response

Version

cargo 1.74.0-nightly (2fc85d15a 2023-09-09)
release: 1.74.0-nightly
commit-hash: 2fc85d15a542bfb610aff7682073412cf635352f
commit-date: 2023-09-09
host: x86_64-pc-windows-msvc
libgit2: 1.7.1 (sys:0.18.0 vendored)
libcurl: 8.2.1-DEV (sys:0.4.65+curl-8.2.1 vendored ssl:Schannel)
os: Windows 10.0.23541 (Windows 11 Enterprise) [64-bit]
@wmmc88 wmmc88 added C-bug Category: bug S-triage Status: This issue is waiting on initial triage. labels Sep 13, 2023
@ehuss ehuss changed the title extra link args apply to unit tests cdylib extra link args apply to unit tests Sep 25, 2023
@ehuss ehuss added A-linkage Area: linker issues, dylib, cdylib, shared libraries, so A-build-scripts Area: build.rs scripts S-needs-team-input Status: Needs input from team on whether/how to proceed. and removed S-triage Status: This issue is waiting on initial triage. labels Sep 25, 2023
@ehuss
Copy link
Contributor

ehuss commented Sep 25, 2023

I'm pretty sure this is not the intended behavior for rustc-cdylib-link-arg. I would say that it should only be passed when building as a cdylib. This is pretty much the same as #10937 (comment) where it is not considering the mode when determining which flags to use.

@wmmc88
Copy link
Author

wmmc88 commented Aug 6, 2024

@weihanglo Is this cargo behaviour intended or is it a bug for rustc-cdylib-link-arg like @ehuss suggested?

@weihanglo
Copy link
Member

It sounds absolutely like what ehuss said, and I have no doubt in their memory of Cargo's history.

github-merge-queue bot pushed a commit that referenced this issue Mar 25, 2025
<!--
Thanks for submitting a pull request 🎉! Here are some tips for you:

* If this is your first contribution, read "Cargo Contribution Guide"
first:
  https://doc.crates.io/contrib/
* Run `cargo fmt --all` to format your code changes.
* Small commits and pull requests are always preferable and easy to
review.
* If your idea is large and needs feedback from the community, read how:
  https://doc.crates.io/contrib/process/#working-on-large-features
* Cargo takes care of compatibility. Read our design principles:
  https://doc.crates.io/contrib/design.html
* When changing help text of cargo commands, follow the steps to
generate docs:

https://github.com/rust-lang/cargo/tree/master/src/doc#building-the-man-pages
* If your PR is not finished, set it as "draft" PR or add "WIP" in its
title.
* It's ok to use the CI resources to test your PR, but please don't
abuse them.

### What does this PR try to resolve?

Explain the motivation behind this change.
A clear overview along with an in-depth explanation are helpful.

You can use `Fixes #<issue number>` to associate this PR to an existing
issue.

### How should we test and review this PR?

Demonstrate how you test this change and guide reviewers through your
PR.
With a smooth review process, a pull request usually gets reviewed
quicker.

If you don't know how to write and run your tests, please read the
guide:
https://doc.crates.io/contrib/tests

### Additional information

Other information you want to mention in this PR, such as prior arts,
future extensions, an unresolved problem, or a TODO list.
-->

fixes #12663
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-build-scripts Area: build.rs scripts A-linkage Area: linker issues, dylib, cdylib, shared libraries, so C-bug Category: bug S-needs-team-input Status: Needs input from team on whether/how to proceed.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants