Skip to content

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

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
tye-exe opened this issue Mar 21, 2025 · 1 comment
Closed

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

tye-exe opened this issue Mar 21, 2025 · 1 comment

Comments

@tye-exe
Copy link

tye-exe commented Mar 21, 2025

When running the unit test for a crate, the #[cfg(test)] attribute is not respected for dependencies, & the dependent crate cannot use code that is gated behind this condition.
I am not sure as to whether this is a bug or an intended feature and I cannot find documentation in regards to either.

For a minimum reproducible example see my repository.
For an in-line example:

// In crate `conditional`
#[cfg(test)]
pub struct Contitional;
// Separate crate depending on `conditional`
#[cfg(test)]
mod tests {

    #[test]
    fn use_contitional() {
        conditional::Conditional;
    }
}

Gives the following error when cargo test is run:

error[E0425]: cannot find value `Conditional` in crate `conditional`

Notes

I found this issue on the rust repository, which suggested to open an issue on the cargo repository, but i was not able to find such an issue, so i am opening one myself.

Cargo version information

cargo 1.87.0-nightly (6cf826701 2025-03-14)
release: 1.87.0-nightly
commit-hash: 6cf8267012570f63d6b86e85a2ae5627de52df9e
commit-date: 2025-03-14
host: x86_64-unknown-linux-gnu
libgit2: 1.9.0 (sys:0.20.0 vendored)
libcurl: 8.12.1-DEV (sys:0.4.80+curl-8.12.1 vendored ssl:OpenSSL/3.4.1)
ssl: OpenSSL 3.4.1 11 Feb 2025
os: NixOS 24.11.0 [64-bit]
@epage
Copy link
Contributor

epage commented Mar 24, 2025

This appears to be a duplicate #8379 and closing in favor of that. If there is a reason for us to keep this open separately, let us know!

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

2 participants