-
Notifications
You must be signed in to change notification settings - Fork 2.7k
refactor(SourceId): merge name and alt_registry_key into one enum
#12675
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
Conversation
|
r? @epage (rustbot has picked a reviewer for you, use r? to override) |
858c012 to
2ded388
Compare
src/cargo/sources/config.rs
Outdated
| if let Some(registry) = def.registry { | ||
| let url = url(®istry, &format!("source.{}.registry", name))?; | ||
| srcs.push(SourceId::for_alt_registry(&url, &name)?); | ||
| srcs.push(SourceId::for_named_registry(&url, &name)?); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously alt_registry_key was only set for registries defined in the [registries] table, while this change uses registry_key for names in the [source] table as well.
I wish it wasn't possible to define remote registries using the [source] table, but it is. At minimum we need to change the doc comment on registry_key, since it indicates that it's only the registries table.
There are uses such as auth/mod.rs L432 that rely on the invariant that registry_key comes from the registries table. We'd need to be sure that case isn't possible for a SourceId that came from the sources table. Otherwise it would print misleading help messages, since cargo login doesn't work for sources defined in the sources table.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A way to not break the invariant is doing this 😆:
| srcs.push(SourceId::for_named_registry(&url, &name)?); | |
| srcs.push(SourceId::for_registry(&url)?); |
I'll try to setup some test case to see whether the error message of [source] itself regresses or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've ended up replacing it with for_registry.
- It should have any regression regarding status console output. Cargo shows the original one.
- The invariant of
registry_keyonly from[registries]table is maintained. Callingfor_registry()means registry from[source]never get aregistry_keyset.
What do people think? Is there any case I am missing?
|
There is a small difference in the output in the following situation: [source.crates-io]
replace-with = 'alternative'
[source.alternative]
registry = 'file:///path/to/registry'With this, the old output would be: The output with this PR would be: This also applies to error messages, like: versus: I don't think that is a big loss, but it is a regression where it no longer tells you the name of the source. @arlosi, are you OK with reviewing this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks pretty good now. I'm ok with the change in error message for remote registry sources. I'll do one more check to make sure there aren't any further impacts tomorrow.
src/cargo/core/source_id.rs
Outdated
|
|
||
| /// Creates a `SourceId` from a remote registry URL with given name. | ||
| pub fn for_alt_registry(url: &Url, name: &str) -> CargoResult<SourceId> { | ||
| pub fn for_named_registry(url: &Url, name: &str) -> CargoResult<SourceId> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we document that the name is an entry in the [registries] table?
Also consider renaming the argument to key to match the new function
src/cargo/core/source_id.rs
Outdated
| alt_registry_key: Option<String>, | ||
| /// Name of the remote registry as defined in the `[registries]` table. | ||
| /// | ||
| /// WARNING: this is not always set for alt-registries when the name is not known. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add something like
"The value for crates.io is crates-io"
src/cargo/util/auth/mod.rs
Outdated
| let name = if sid.is_crates_io() { | ||
| Some(CRATES_IO_REGISTRY) | ||
| } else { | ||
| sid.alt_registry_key() | ||
| sid.registry_key() | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like registry_key should always be crates-io when is_crates_io() is true.
Can this if be removed and replaced with let name = sid.registry_key()?
There are a few other places with this pattern too.
12acc02 to
630bbdb
Compare
SourceId.name fieldname and alt_registry_key into one enum
|
Chose to use an enum to differentiate registries from |
Both `name` and `alt_registry_key` are mainly used for displaying. We can safely merge them into one enum. However, `alt_registry_key` is also used for telling if a SourceId is from `[registries]` so we need to provide functions to distinguish that.
`registry_key` should always be `crates-io` when `is_crates_io()` is true This also removes the test verifying `Debug` output, whose format doesn't need to be guaranteed.
630bbdb to
23e91c3
Compare
|
Thanks! Looks great. @bors r+ |
|
☀️ Test successful - checks-actions |
Update cargo 11 commits in 414d9e3a6d8096f3e276234ce220c868767a8792..e6aabe8b3fcf639be3a5bf68e77853bd7b3fa27d 2023-09-22 07:03:57 +0000 to 2023-09-26 16:31:53 +0000 - Use full target spec for `cargo rustc --print --target` (rust-lang/cargo#12743) - feat(embedded): Hack in code fence support (rust-lang/cargo#12681) - chore(ci): Update Renovate schema (rust-lang/cargo#12741) - more specific registry index not found msg (rust-lang/cargo#12732) - docs: warn about upload timeout (rust-lang/cargo#12733) - Fix some typos (rust-lang/cargo#12730) - upgrade gitoxide to v0.54 (rust-lang/cargo#12731) - Update target-arch-aware crates to support mips r6 targets (rust-lang/cargo#12720) - Buffer console status messages. (rust-lang/cargo#12727) - Fix spurious errors with networking tests. (rust-lang/cargo#12726) - refactor(SourceId): merge `name` and `alt_registry_key` into one enum (rust-lang/cargo#12675) r? ghost
What does this PR try to resolve?
Both
nameandalt_registry_keyare mainly used for displaying.We can safely merge them into one enum.
However,
alt_registry_keyis also used for telling if aSourceIdisfrom
[registries]so we need to provide functions to distinguish that.How should we test and review this PR?
A new test is added to prevent regressions like #12675 (comment).