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

Package ID specification urls must contain a host #9188

Merged
merged 10 commits into from
Mar 9, 2021

Conversation

weihanglo
Copy link
Member

Resolves #9041

Not sure which commit breaks this. Cargo shipped with rust 1.46 didn't unwrap on a None but 1.47 did. Even checkouted to 149022b (cargo of rust 1.46), it still unwrap unexpectedly.

So I ended up following the Specification grammer to make sure there is a host in the pkgid urls.

See console output

cargo of rust 1.46

$ cargo +1.46 -vV
cargo 1.46.0 (149022b1d 2020-07-17)
release: 1.46.0
commit-hash: 149022b1d8f382e69c1616f6a46b69ebf59e2dea
commit-date: 2020-07-17

$ cargo +1.46 pkgid /path
error: package ID specification `/path` matched no packages

cargo of rust 1.47

$ cargo +1.47 -vV
cargo 1.47.0 (f3c7e066a 2020-08-28)
release: 1.47.0
commit-hash: f3c7e066ad66e05439cf8eab165a2de580b41aaf
commit-date: 2020-08-28

$ cargo +1.47 pkgid /path
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src/tools/cargo/src/cargo/core/package_id_spec.rs:234:50
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

cargo on commit 149022b

$ git checkout 149022b1d8f382e69c1616f6a46b69ebf59e2dea
$ cargo run -- pkgid /path
   Compiling cargo-platform v0.1.1 ([..]/cargo/crates/cargo-platform)
   Compiling crates-io v0.31.1 ([..]/cargo/crates/crates-io)
   Compiling cargo v0.47.0 ([..]/cargo)
    Finished dev [unoptimized + debuginfo] target(s) in 22.90s
     Running `target/debug/cargo pkgid /path`
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src/cargo/core/package_id_spec.rs:234:50
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

@rust-highfive
Copy link

r? @ehuss

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 19, 2021
@weihanglo
Copy link
Member Author

weihanglo commented Feb 19, 2021

Oops. Seems some url like file:///path/#bar is valid in some scenarios. Should we enforce the "must have a host" rule or having a "fallback to a empty host" is enough?

@ehuss
Copy link
Contributor

ehuss commented Feb 19, 2021

Thanks for taking a look at this. Unfortunately, specs are kind of a mess right now (see #7725). The cargo: protocol probably needs to just be removed. Once you do that, I think it should error correctly when given a bare path.

It might be nice to detect the scenario someone gives a path and provide a better error message (like maybe suggest using file://).

@weihanglo
Copy link
Member Author

Updated. However it still unwraps a None if someone passes cargo:///path without a host deliberately. Should this case be handle by a empty host as a fallback?

@ehuss
Copy link
Contributor

ehuss commented Feb 25, 2021

So, I was thinking it could flip the :// check around, maybe something like this:

if spec.contains("://") {
    if let Ok(url) = spec.into_url() {
        return PackageIdSpec::from_url(url);
    }
} else if spec.contains('/') || spec.contains('\\') {
    let abs = std::env::current_dir().unwrap_or_default().join(spec);
    if abs.exists() {
        let maybe_url = Url::from_file_path(abs)
            .map_or_else(|_| "a file:// URL".to_string(), |url| url.to_string());
        bail!(
            "package ID specification `{}` looks like a file path, \
            maybe try {}",
            spec,
            maybe_url
        );
    }
}

And completely remove the cargo:// stuff, including the check for cargo in Display. The check for startswith / will only catch absolute paths on Unix, which I think is a little too narrow.

The error I wrote above still isn't great. Ideally it would actually tell you what the correct value is. query_str is capable of doing that, so if you are interested in trying to provide an accurate error message, that could be nice, though that would be a fair bit more work.

The tests need to be updated, and ideally there would be a UI test (maybe just add another case to suggestion_bad_pkgid).

Also, the documentation should be updated (cargo-pkgid.md, then rebuild the man pages).

@weihanglo
Copy link
Member Author

@ehuss
All you suggestions are patched!
Not sure what part of document should be refined, so I only add an example for querying local package's pkgid.

@ehuss
Copy link
Contributor

ehuss commented Mar 9, 2021

Thanks!

Sorry, I think I was mistaken on the docs, but your change seems fine. I was thinking of the pkgid-spec.md file. I'll follow up with an update to that.

@bors r+

@bors
Copy link
Collaborator

bors commented Mar 9, 2021

@ehuss: 🔑 Insufficient privileges: Not in reviewers

@ehuss
Copy link
Contributor

ehuss commented Mar 9, 2021

@bors r+

@bors
Copy link
Collaborator

bors commented Mar 9, 2021

📌 Commit c5d2304 has been approved by ehuss

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 9, 2021
@bors
Copy link
Collaborator

bors commented Mar 9, 2021

⌛ Testing commit c5d2304 with merge 0f3f47e...

@bors
Copy link
Collaborator

bors commented Mar 9, 2021

☀️ Test successful - checks-actions
Approved by: ehuss
Pushing 0f3f47e to master...

@bors bors merged commit 0f3f47e into rust-lang:master Mar 9, 2021
@weihanglo weihanglo deleted the issue-9041 branch March 9, 2021 23:51
@ehuss
Copy link
Contributor

ehuss commented Mar 10, 2021

Posted #9249 with an update to the spec docs that I was thinking of.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Mar 13, 2021
Update cargo

7 commits in 970bc67c3775781b9708c8a36893576b9459c64a..32da9eaa5de5be241cf8096ca6b749a157194f77
2021-03-07 18:09:40 +0000 to 2021-03-13 01:18:40 +0000
- Fix logic for determining prefer-dynamic for a dylib. (rust-lang/cargo#9252)
- Fix issue with filtering exclusive target dependencies. (rust-lang/cargo#9255)
- Update pkgid-spec docs. (rust-lang/cargo#9249)
- Wordsmith the edition documentation a bit more (rust-lang/cargo#9233)
- Package ID specification urls must contain a host (rust-lang/cargo#9188)
- Add documentation for JSON message_path. (rust-lang/cargo#9247)
- Fix filter_platform to run on targets other than x86. (rust-lang/cargo#9246)
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Mar 14, 2021
Update cargo

7 commits in 970bc67c3775781b9708c8a36893576b9459c64a..32da9eaa5de5be241cf8096ca6b749a157194f77
2021-03-07 18:09:40 +0000 to 2021-03-13 01:18:40 +0000
- Fix logic for determining prefer-dynamic for a dylib. (rust-lang/cargo#9252)
- Fix issue with filtering exclusive target dependencies. (rust-lang/cargo#9255)
- Update pkgid-spec docs. (rust-lang/cargo#9249)
- Wordsmith the edition documentation a bit more (rust-lang/cargo#9233)
- Package ID specification urls must contain a host (rust-lang/cargo#9188)
- Add documentation for JSON message_path. (rust-lang/cargo#9247)
- Fix filter_platform to run on targets other than x86. (rust-lang/cargo#9246)
@ehuss ehuss mentioned this pull request May 28, 2021
@ehuss ehuss added this to the 1.52.0 milestone Feb 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cargo test panics when given specific <spec> with -p
4 participants