Conditionally mark the test cfg as a well known cfg#15007
Conditionally mark the test cfg as a well known cfg#15007epage merged 3 commits intorust-lang:masterfrom
test cfg as a well known cfg#15007Conversation
92cf66c to
a02f269
Compare
cee68ea to
eb3c3a9
Compare
| .with_stderr_contains(x!("rustc" => "cfg" of "docsrs")) | ||
| .with_stderr_data(str![[r#" | ||
| ... | ||
| [WARNING] unexpected `cfg` condition name: `test` |
There was a problem hiding this comment.
If user has specified --lib, doesn't it mean that they requested to test the lib crate so cfg(test) is expected to be there?
There was a problem hiding this comment.
What the command line arguments are shouldn't matter, otherwise users would get a different behavior between a "normal" cargo test and one with arguments like --all-targets.
There was a problem hiding this comment.
However, lib.test = false doesn't mean there is no test. It, as of today, is defined as "test" won't run by default. cfg(test) is still relevant regardless if a test is in the default test set.
Maybe I should put the question this way: Why is cfg(test) in a test=false crate unexpected?
There was a problem hiding this comment.
IMO having test = false meaning not tested by default compared to not tests at all is unexpected, at least before reading the documentation I would have assumed that it disabled all the testing.
@jplatte made a further argument in his comment:
Hm, maybe. I guess it depends on what the intention of
lib.test = falseis. I think the vast majority of projects that set it don't have unit tests at all, and if any were to be introduced, getting a warning would be valuable (whether it results in the test author removing the flag or moving the test code into unit tests). I wonder if there are any projects that uselib.test = falsewhile having unit tests, intentionally.
A similar comment was also put in the URLO thread.
There was a problem hiding this comment.
Thanks for linking the thread here. Yeah I've read that, and agree that lib.test = false largely implies no test in lib crate at all. That is a valid use case and deserves a lint or warning.
However, fields under Cargo targets are mostly about including in or excluding from the default crate set, and are largely affected by the command-line argument. If Cargo has started emitting unexpected warning for Cargo targets, it may also bite users that disabling tests for other reasons. For example, I've seen people setting test=false because some tests are not expected to run by default for daily development, but only on CI or other special environments.
While that kind of scenario I guess is far less than who don't want tests at all, it is still a valid use case aligning with what the current doc describes. Treating unexpected_cfgs as an indirect way of banning tests in a crate doesn't seem to be the best venue. It is more like a hack because there is no lint for really banning tests.
That being said, I am not surprised if Cargo targets settings don't meet people's expectation. It is always a source of confusions1 😞.
Footnotes
-
I can actually name a lot of issues here. Let's just have a little peek of them:
* https://github.com/rust-lang/cargo/issues/8338
* https://github.com/rust-lang/cargo/issues/10936
* https://github.com/rust-lang/cargo/issues/10958
* https://github.com/rust-lang/cargo/issues/13668
* https://github.com/rust-lang/cargo/issues/13437
* https://github.com/rust-lang/cargo/issues/13828 ↩
There was a problem hiding this comment.
The unexpected_cfgs lint do trigger on #[test] but only when passed with --test.
Details
$ rustc +nightly --crate-type=lib --test --check-cfg='cfg()' src/lib.rs
warning: unexpected `cfg` condition name: `test`
--> src/lib.rs:9:5
|
9 | #[test]
| ^^^^^^^
|
= help: expected names are: `clippy`, `debug_assertions`, `doc`, `doctest`, `fmt_debug`, `miri`, `over
flow_checks`, `panic`, `proc_macro`, `relocation_model`, `rustfmt`, `sanitize`, `sanitizer_cfi_generaliz
e_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_e
nv`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `ub_checks`, `unix`, and `windows`
= note: using a cfg inside a attribute macro will use the cfgs from the destination crate and not the ones from the defining crate
= help: try referring to `test` crate for guidance on how handle this unexpected cfg
= help: to expect this configuration use `--check-cfg=cfg(test)`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
= note: `#[warn(unexpected_cfgs)]` on by default
= note: this warning originates in the attribute macro `test` (in Nightly builds, run with -Z macro-backtrace for more info)
warning: 1 warning emitted
We can probably make the lint fire without the --test argument.
There was a problem hiding this comment.
Unless I missed something in catching up on this, I feel like we'd get more out of test = false being consistent, independent of the build-target type. I'm not too concerned about false positives from people who have #[test] but set test = false.
There was a problem hiding this comment.
My main point is the meaning of test = false will change after this PR. I don't mind shipping the particular change. Just want to make sure people aware of and feel good with it. And probably need doc update as well.
There was a problem hiding this comment.
I have adjusted the documentation with the new behavior.
There was a problem hiding this comment.
There are multiple ways which a target may be tested even when it has test = false, notably, cargo test --all-targets and cargo test some_test_name_filter. Therefore, I believe this change is problematic (and have filed #15131 for tracking of that).
eb3c3a9 to
b757af9
Compare
|
Talked it over with @weihanglo. While this is an observable change in behavior it seems relatively innocuous and could likely be revisited if needed. |
Update cargo 11 commits in cecde95c119a456c30e57d3e4b31fff5a7d83df4..776129a2b93928a67ec4c2f8e5bc3cfeb5bc06cd 2025-01-24 17:15:24 +0000 to 2025-01-30 15:34:14 +0000 - Don't suggest `cargo login` when using incompatible credental providers (rust-lang/cargo#15124) - chore: Update clap_complete (rust-lang/cargo#15121) - Move the changelog to the cargo book (rust-lang/cargo#15119) - Conditionally mark the `test` cfg as a well known cfg (rust-lang/cargo#15007) - fix broken links in the Cargo book (rust-lang/cargo#15109) - Fix a typo and touch up documentation (rust-lang/cargo#15108) - Fix shared_std_dependency_rebuild running on Windows (rust-lang/cargo#15111) - Fix warnings on Windows (rust-lang/cargo#15112) - fix(login): Deprecate CLI token (rust-lang/cargo#15057) - Update tests to fix nightly errors (rust-lang/cargo#15110) - Fix comment on Ord for SourceId (rust-lang/cargo#15103)
This is needed because since rust-lang/cargo#15007 `test` is no longer a built-in well-known cfg when `[lib] test = false` is set.
Update cargo try-job: dist-aarch64-linux 11 commits in cecde95c119a456c30e57d3e4b31fff5a7d83df4..776129a2b93928a67ec4c2f8e5bc3cfeb5bc06cd 2025-01-24 17:15:24 +0000 to 2025-01-30 15:34:14 +0000 - Don't suggest `cargo login` when using incompatible credental providers (rust-lang/cargo#15124) - chore: Update clap_complete (rust-lang/cargo#15121) - Move the changelog to the cargo book (rust-lang/cargo#15119) - Conditionally mark the `test` cfg as a well known cfg (rust-lang/cargo#15007) - fix broken links in the Cargo book (rust-lang/cargo#15109) - Fix a typo and touch up documentation (rust-lang/cargo#15108) - Fix shared_std_dependency_rebuild running on Windows (rust-lang/cargo#15111) - Fix warnings on Windows (rust-lang/cargo#15112) - fix(login): Deprecate CLI token (rust-lang/cargo#15057) - Update tests to fix nightly errors (rust-lang/cargo#15110) - Fix comment on Ord for SourceId (rust-lang/cargo#15103)
…ang#15007)" This reverts commit 26ce027, reversing changes made to 730d997.
…" (#15132) This reverts #15007, as it causes confusions and needs more time to figure out a proper solution. See * #15131 * <https://rust-lang.zulipchat.com/#narrow/channel/246057-t-cargo/topic/.60test.60.20well.20known.20cfg.20error> * <#15007 (comment)>
Update cargo 12 commits in cecde95c119a456c30e57d3e4b31fff5a7d83df4..0e3d73849ab8cbbab3ec5c65cbd555586cb21339 2025-01-24 17:15:24 +0000 to 2025-02-01 20:14:40 +0000 - Revert "Conditionally mark the `test` cfg as a well known cfg (rust-lang/cargo#15007)" (rust-lang/cargo#15132) - Don't suggest `cargo login` when using incompatible credental providers (rust-lang/cargo#15124) - chore: Update clap_complete (rust-lang/cargo#15121) - Move the changelog to the cargo book (rust-lang/cargo#15119) - Conditionally mark the `test` cfg as a well known cfg (rust-lang/cargo#15007) - fix broken links in the Cargo book (rust-lang/cargo#15109) - Fix a typo and touch up documentation (rust-lang/cargo#15108) - Fix shared_std_dependency_rebuild running on Windows (rust-lang/cargo#15111) - Fix warnings on Windows (rust-lang/cargo#15112) - fix(login): Deprecate CLI token (rust-lang/cargo#15057) - Update tests to fix nightly errors (rust-lang/cargo#15110) - Fix comment on Ord for SourceId (rust-lang/cargo#15103)
Update cargo 12 commits in cecde95c119a456c30e57d3e4b31fff5a7d83df4..0e3d73849ab8cbbab3ec5c65cbd555586cb21339 2025-01-24 17:15:24 +0000 to 2025-02-01 20:14:40 +0000 - Revert "Conditionally mark the `test` cfg as a well known cfg (rust-lang/cargo#15007)" (rust-lang/cargo#15132) - Don't suggest `cargo login` when using incompatible credental providers (rust-lang/cargo#15124) - chore: Update clap_complete (rust-lang/cargo#15121) - Move the changelog to the cargo book (rust-lang/cargo#15119) - Conditionally mark the `test` cfg as a well known cfg (rust-lang/cargo#15007) - fix broken links in the Cargo book (rust-lang/cargo#15109) - Fix a typo and touch up documentation (rust-lang/cargo#15108) - Fix shared_std_dependency_rebuild running on Windows (rust-lang/cargo#15111) - Fix warnings on Windows (rust-lang/cargo#15112) - fix(login): Deprecate CLI token (rust-lang/cargo#15057) - Update tests to fix nightly errors (rust-lang/cargo#15110) - Fix comment on Ord for SourceId (rust-lang/cargo#15103)
What does this PR try to resolve?
This PR conditionally mark the
testcfg as a well known cfg depending on the target unit "test" field (ielib.test = false,[[bin]] test = falseand others).This is related to rust-lang/rust#117778 and https://users.rust-lang.org/t/cargo-what-is-the-purpose-of-lib-test-false/102361.
When defining
lib.test = false(and others), any use ofcfg(test)will trigger theunexpected_cfgslint.How should we test and review this PR?
Best reviewed commit by commit. Second commit removes the
testcfg from the--check-cfgargs.Additional information
T-compiler MCP#785 and #14963 were of preparatory work.
r? @epage