Skip to content
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

cargo test does not export CARGO_CFG_TEST for build.rs scripts #4789

Closed
nagisa opened this issue Dec 7, 2017 · 4 comments
Closed

cargo test does not export CARGO_CFG_TEST for build.rs scripts #4789

nagisa opened this issue Dec 7, 2017 · 4 comments

Comments

@nagisa
Copy link
Member

nagisa commented Dec 7, 2017

e here is fairly simple – I need to build a shared library to be used for tests, however it is also important to me that this library is not built when people are compiling the library for regular use.

rustc --test --print=cfg does output the test configuration variable, however when cargo test is run, the build script does not receive this variable in form of CARGO_CFG_TEST.

Caveat is that rerunning the build script for both test and non-test would potentially re-run expensive build.rs operations by default, however that seems easily remedied by requiring people to print out a cargo:rerun-if-env-changed=CARGO_CFG_TEST (it would become the first exception to the list of environment variables set by cargo and tracked by default).

@alexcrichton
Copy link
Member

This is currently intentional behavior because build scripts don't have a "I'm running tests" mode. That would require a test build script most likely and it's an unimplemented feature so far.

@stale
Copy link

stale bot commented Sep 18, 2018

As there hasn't been any activity here in over 6 months I've marked this as stale and if no further activity happens for 7 days I will close it.

I'm a bot so this may be in error! If this issue should remain open, could someone (the author, a team member, or any interested party) please comment to that effect?

The team would be especially grateful if such a comment included details such as:

  • Is this still relevant?
  • If so, what is blocking it?
  • Is it known what could be done to help move this forward?

Thank you for contributing!

(The cargo team is currently evaluating the use of Stale bot, and using #6035 as the tracking issue to gather feedback.)

If you're reading this comment from the distant future, fear not if this was closed automatically. If you believe it's still an issue please leave a comment and a team member can reopen this issue. Opening a new issue is also acceptable!

@stale stale bot added the stale label Sep 18, 2018
@stale
Copy link

stale bot commented Oct 18, 2018

As I didn't see any updates in 30 days I'm going to close this. Please see the previous comment for more information!

@nvzqz
Copy link

nvzqz commented Jun 6, 2019

@alexcrichton why would a separate test build script be needed and not just allow for #[cfg(test)] to work?

palfrey added a commit to palfrey/wait-for-db that referenced this issue Oct 27, 2019
rust-lang/cargo#4789) so always set the env
variable changers
paholg added a commit to paholg/typenum that referenced this issue Apr 10, 2020
They weren't running for two reasons.

1. I was annotating them with `#[cfg(tests)]` instead of `#[cfg(test)]`.
2. Build scripts don't know about test mode:
   rust-lang/cargo#4789
paholg added a commit to paholg/typenum that referenced this issue Apr 10, 2020
* Fix generated tests

They weren't running for two reasons.

1. I was annotating them with `#[cfg(tests)]` instead of `#[cfg(test)]`.
2. Build scripts don't know about test mode:
   rust-lang/cargo#4789

* Appease clippy while keeping build working on Rust 1.22
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

No branches or pull requests

3 participants