cargo icon indicating copy to clipboard operation
cargo copied to clipboard

Add `cfg_version` support

Open mo8it opened this issue 8 months ago • 8 comments

What does this PR try to resolve?

#15531

mo8it avatar May 16 '25 15:05 mo8it

@epage I will of course take care of proper error handling, but I just want to get feedback on the initial direction.

~cfg(nightly) can be already parsed as a Name.~

mo8it avatar May 16 '25 15:05 mo8it

cfg(nightly) can be already parsed as a Name.

I'm not sure what this is referring to as I don't see this in the RFC or the rustc version

epage avatar May 16 '25 21:05 epage

Be sure to check out rust-lang/rust#141137 for a summary of what has changed from the RFC

epage avatar May 19 '25 21:05 epage

A question came up about how older versions of cargo will handle this.

At least for a local package, this causes an error like:

failed to parse `version('1.80')` as a cfg expression: unexpected content `('1.80')` found after cfg expression

That seems fine to me.

However, I have a little more concern about how this impacts the index.

At a minimum, can you add a test that shows using a dependency from the index that itself uses a cfg() dependency? So, essentially, something like:

    Package::new("only_newer", "1.0.0").publish();
    Package::new("bar", "2.0.0")
        .file("src/lib.rs", "extern crate only_newer;")
        .target_dep("only_newer", "1.0", "cfg(version('1.89.0'))")
        .publish();

and having a regular dependency on bar. In my quick testing, this seems to be broken somehow, but I'm not sure how. Can you look into that?

Another thing to look into is if the crates.io parsing of the manifest will have any issues with this, or how it displays dependencies.

Unfortunately we don't have very good infrastructure for checking that older versions of cargo work with newer versions of the index. I think the best we have right now is #[cargo_test(requires_rustup_stable)], where we can make sure stable is compatible with what is generated by nightly. But that doesn't really help with a fixed version like "does rust 1.87 stay compatible with nightly". We have a whole module for doing that (https://github.com/rust-lang/cargo/blob/HEAD/tests/testsuite/old_cargos.rs), but those tests are disabled due to the cost.

If we are able to figure some way to test older versions, then one approach would be to make sure the index for a crate has a mix of old and new cfgs. So something like:

    Package::new("only_newer", "1.0.0").publish();
    Package::new("bar", "1.0.0").publish();
    Package::new("bar", "2.0.0")
        .file("src/lib.rs", "extern crate only_newer;")
        .target_dep("only_newer", "1.0", "cfg(version('1.89.0'))")
        .publish();

where you have a dependency on bar = "1.0" should still work on older versions of cargo (that is, they should not choke on the 2.0 index entry). AFAIK, that should be fine, since older cargos are supposed to ignore index entries it can't parse. However, I don't remember where cargo parses the cfg strings, and I don't want old versions to choke on that.

ehuss avatar May 20 '25 16:05 ehuss

r? @ehuss

rustbot has assigned @ehuss. They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

rustbot avatar May 20 '25 18:05 rustbot

:umbrella: The latest upstream changes (possibly 2d662d919ed8bfca29ac6dd5d6eec3332cf7ec49) made this pull request unmergeable. Please resolve the merge conflicts.

rustbot avatar May 20 '25 22:05 rustbot

@epage Thanks for rewriting the history. I found it a bit disappointing that I wasn't listed as an author anymore. Therefore, I updated the author information of the commits and hope that you are happy with the current state.

I also fixed the CI by removing dbg!(input).

Finally, I resolved the merge conflicts.

Learning jj today helped a lot :D

If we agree on https://github.com/rust-lang/cargo/pull/15533#discussion_r2098745262, then the PR should be ready for merging.

mo8it avatar May 21 '25 16:05 mo8it

I found it a bit disappointing that I wasn't listed as an author anymore.

Sorry that I forgot to preserve that!

epage avatar May 21 '25 16:05 epage

FYI a concern was raised on the new stabilization issue (https://github.com/rust-lang/rust/pull/141766#issuecomment-2946372974) about what syntax is used for this.

epage avatar Jun 09 '25 14:06 epage

@epage @ehuss Unfortunately, I was not able to find time to finish this PR. I will also be away for about 3 weeks. So feel free to add your changes and then merge.

mo8it avatar Jun 20 '25 09:06 mo8it

@mo8it don't worry, there is nothing to do atm while the design is being re-evaluated

epage avatar Jun 20 '25 14:06 epage

:umbrella: The latest upstream changes (possibly #15841) made this pull request unmergeable. Please resolve the merge conflicts.

rustbot avatar Aug 17 '25 16:08 rustbot