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

#[cfg(not(test))] doesn't work in dependencies #59168

Closed
nhynes opened this issue Mar 13, 2019 · 5 comments
Closed

#[cfg(not(test))] doesn't work in dependencies #59168

nhynes opened this issue Mar 13, 2019 · 5 comments

Comments

@nhynes
Copy link
Contributor

nhynes commented Mar 13, 2019

I have something that looks like

// crateA/lib.rs

#[cfg(not(test))]
extern "C" {
	fn ext_fn();
}

#[cfg(test)]
fn ext_fn() {
	unimplemented!();
}

pub fn do_ext() {
	unsafe { ext_fn() };
}
// crateB/lib.rs
crateA::do_ext()

but despite using cargo test, I still get a link error saying that ext_fn isn't found. Is #[cfg(not(test))] a valid attribute?

@jonas-schievink
Copy link
Contributor

AFAIK cargo test only applies cfg(test) to the crate you run it in, not to any dependencies.

@nhynes
Copy link
Contributor Author

nhynes commented Mar 14, 2019

That's unfortunate. It seems that a workaround is to use [features] test = [], but it'd be nice if conditional compilation flags were propagated through the entire graph.

@sfackler
Copy link
Member

This would be best filed on cargo, not rustc itself, but I don't think we want to rebuild the entire dependency tree for tests necessarily.

@mleduque
Copy link

mleduque commented Apr 2, 2020

Was there a follow-up issue somewhere else for this bug?

@mimoo
Copy link

mimoo commented Nov 5, 2021

IIRC, the way we fixed that in diem was to have a [features] testing = [] in every crate Cargo.toml, and import dependencies with this feature in [dev-dependencies] (see https://github.com/diem/diem/blob/0cdde0d258a3706d7d84d05225899df8518f319a/testsuite/smoke-test/Cargo.toml#L52 for an example):

[dev-dependencies]
diem-genesis-tool = {path = "../../config/management/genesis", features = ["testing"] }

but then in every other crate you had to do stuff like

if cfg!(any(test, feature = "testing")) {
  // ...
}

and

#[cfg(any(test, feature = "testing"))]
// ...

instead of just having "test"

it's a pain

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

5 participants