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

feat: add cargo pkgid support for cargo-script #14961

Merged
merged 8 commits into from
Feb 4, 2025

Conversation

Rustin170506
Copy link
Member

@Rustin170506 Rustin170506 commented Dec 19, 2024

What does this PR try to resolve?

close #14831

In this PR, we added the cargo pkgid support for the cargo-script.

For the package itself:

cargo pkgid --manifest-path foo.rs
path+file:///my-project/foo.rs/foo#0.86.0

For its dependence:

cargo pkgid --manifest-path foo.rs -p sqlx
registry+https://github.com/rust-lang/crates.io-index#[email protected]

How should we test and review this PR?

I have updated the unit tests and also added more test cases for it.

Additional information

None

@rustbot
Copy link
Collaborator

rustbot commented Dec 19, 2024

r? @epage

rustbot has assigned @epage.
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 rustbot added A-cli Area: Command-line interface, option parsing, etc. Command-pkgid S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 19, 2024
@epage
Copy link
Contributor

epage commented Dec 19, 2024

Another test we'll need is that we error when a cargo script is specified as a dependency which might become possible with this change.

@Rustin170506 Rustin170506 force-pushed the rustin-patch-pkgid branch 2 times, most recently from 3022d26 to 44ded93 Compare December 19, 2024 15:12
@Rustin170506
Copy link
Member Author

Rustin170506 commented Dec 19, 2024

Another test we'll need is that we error when a cargo script is specified as a dependency which might become possible with this change.

Do you mean by cargo pkgid --manifest-path foo.rs -p foo?

@epage
Copy link
Contributor

epage commented Dec 19, 2024

I mean

[package]
name = "foo"

[dependencies]
bar.path = "bar.rs"

@rustbot

This comment has been minimized.

@Rustin170506
Copy link
Member Author

@epage dedc674 (#14961)

I just did a really rough commit to prove the idea here. It seems to work. But the code is totally messed up.

I want to check two things with you before I move forward and improve the code.

  1. Can you take a grlance at the implementation, I think we have to change the SourceId and PackageIdSpec. I just want to check if I am on the right track.
  2. I am a little hesitant that should we include the filename or just mark it is embedded. Since from the RFC, we don't allow users to specify the name at all, it seems the information is enough to construct the filename. But I also believe that including the full name would be easier to use and expand in the future. So I am not sure what is your thought.

@epage
Copy link
Contributor

epage commented Dec 31, 2024

I am a little hesitant that should we include the filename or just mark it is embedded. Since from the RFC, we don't allow users to specify the name at all, it seems the information is enough to construct the filename. B

The RFC says package.name is defaulted but users can set it.

package.name cannot be mapped unambiguously back to a file name because we sanitize it

let name = sanitize_name(file_stem.as_ref());

/// Ensure the package name matches the validation from `ops::cargo_new::check_name`
fn sanitize_name(name: &str) -> String {
let placeholder = if name.contains('_') {
'_'
} else {
// Since embedded manifests only support `[[bin]]`s, prefer arrow-case as that is the
// more common convention for CLIs
'-'
};
let mut name = PackageName::sanitize(name, placeholder).into_inner();
loop {
if restricted_names::is_keyword(&name) {
name.push(placeholder);
} else if restricted_names::is_conflicting_artifact_name(&name) {
// Being an embedded manifest, we always assume it is a `[[bin]]`
name.push(placeholder);
} else if name == "test" {
name.push(placeholder);
} else if restricted_names::is_windows_reserved(&name) {
// Go ahead and be consistent across platforms
name.push(placeholder);
} else {
break;
}
}
name
}

@epage
Copy link
Contributor

epage commented Dec 31, 2024

Can you take a grlance at the implementation, I think we have to change the SourceId and PackageIdSpec. I just want to check if I am on the right track.

    // FIXME: It should be `path+[ROOTURL]/foo#[email protected]`.
    p.cargo("-Zscript pkgid --manifest-path script.rs")
        .masquerade_as_nightly_cargo(&["script"])
        .with_stdout_data(str![[r#"
path+[ROOTURL]/foo#[email protected]
"#]])

this should instead be

    // FIXME: It should be `path+[ROOTURL]/foo/script.rs#0.0.0`.
    p.cargo("-Zscript pkgid --manifest-path script.rs")
        .masquerade_as_nightly_cargo(&["script"])
        .with_stdout_data(str![[r#"
path+[ROOTURL]/foo/script.rs#0.0.0
"#]])

according to #14831 (comment)

Switching to that scheme should simplify the code, not requiring updates to PackageIdSpec, PackageId, or SourceId struct fields, only functions

@Rustin170506
Copy link
Member Author

The RFC says package.name is defaulted but users can set it.

Oh, I misunderstood package.name = <slugified file stem>. I thought it always used the file name.

package.name cannot be mapped unambiguously back to a file name because we sanitize it

Yes. Got it.

@Rustin170506
Copy link
Member Author

Rustin170506 commented Jan 2, 2025

this should instead be

    // FIXME: It should be `path+[ROOTURL]/foo/script.rs#0.0.0`.
    p.cargo("-Zscript pkgid --manifest-path script.rs")
        .masquerade_as_nightly_cargo(&["script"])
        .with_stdout_data(str![[r#"
path+[ROOTURL]/foo/script.rs#0.0.0
"#]])

according to #14831 (comment)

Switching to that scheme should simplify the code, not requiring updates to PackageIdSpec, PackageId, or SourceId struct fields, only functions

So I guess if the package name is foo, then we have:

path+[ROOTURL]/foo/script.rs#foo@0.0.0

@epage
Copy link
Contributor

epage commented Jan 2, 2025

So I guess if the package name is foo, then we have:

path+[ROOTURL]/foo/script.rs#foo@0.0.0

If the user explicitly overrode it, yes

@Rustin170506 Rustin170506 force-pushed the rustin-patch-pkgid branch 2 times, most recently from c10754e to 6659d5f Compare January 2, 2025 15:16
src/cargo/core/workspace.rs Outdated Show resolved Hide resolved
@Rustin170506 Rustin170506 force-pushed the rustin-patch-pkgid branch 2 times, most recently from d10f797 to fc25d9d Compare January 2, 2025 15:18
Copy link
Member Author

@Rustin170506 Rustin170506 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔢 Self-check (PR reviewed by myself and ready for feedback.)

src/cargo/sources/path.rs Outdated Show resolved Hide resolved
@Rustin170506 Rustin170506 marked this pull request as ready for review January 15, 2025 15:36
@Rustin170506 Rustin170506 requested a review from epage January 15, 2025 15:36
@Rustin170506 Rustin170506 force-pushed the rustin-patch-pkgid branch 2 times, most recently from 183eeff to 1d960c7 Compare January 25, 2025 14:55
Copy link
Member Author

@Rustin170506 Rustin170506 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔢 Self-check (PR reviewed by myself and ready for feedback.)

@Rustin170506 Rustin170506 requested a review from epage January 25, 2025 15:09
src/cargo/core/source_id.rs Outdated Show resolved Hide resolved
@epage epage added this pull request to the merge queue Feb 4, 2025
@epage
Copy link
Contributor

epage commented Feb 4, 2025

Thanks!

Merged via the queue into rust-lang:master with commit f651497 Feb 4, 2025
21 checks passed
@Rustin170506 Rustin170506 deleted the rustin-patch-pkgid branch February 5, 2025 02:16
@Rustin170506
Copy link
Member Author

Thanks for your review! 💚 💙 💜 💛 ❤️

bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 8, 2025
Update cargo

14 commits in 0e3d73849ab8cbbab3ec5c65cbd555586cb21339..2928e32734b04925ee51e1ae88bea9a83d2fd451
2025-02-01 20:14:40 +0000 to 2025-02-07 16:50:22 +0000
- Simplify backtrack (rust-lang/cargo#15150)
- Don't use on Solaris libc::LOCK_* which were removed from libc in ver… (rust-lang/cargo#15143)
- feat: emit error if package not found within workspace (rust-lang/cargo#15071)
- Make cache tracking resilient to unexpected files (rust-lang/cargo#15147)
- Small resolver cleanups (rust-lang/cargo#15040)
- feat: add `cargo pkgid` support for cargo-script (rust-lang/cargo#14961)
- Suggest similar feature names on CLI (rust-lang/cargo#15133)
- fix: Don't use "did you mean" in errors (rust-lang/cargo#15138)
- Fix changelog link (rust-lang/cargo#15142)
- chore(deps): update rust crate rand to 0.9.0 (rust-lang/cargo#15129)
- Remove the original changelog (rust-lang/cargo#15123)
- chore(deps): update rust crate gix to 0.70.0 (rust-lang/cargo#15128)
- allow windows reserved names in CI (rust-lang/cargo#15135)
- removed a word that was repeated (rust-lang/cargo#15136)
@rustbot rustbot added this to the 1.86.0 milestone Feb 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cli Area: Command-line interface, option parsing, etc. A-manifest Area: Cargo.toml issues A-workspaces Area: workspaces Command-install Command-pkgid Command-uninstall S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add cargo pkgid for cargo-script
3 participants