Skip to content

feat(builtin-dep): Add Builtin Source - #17567

Open
adamgemmell wants to merge 3 commits into
rust-lang:masterfrom
adamgemmell:dev/builtins/source
Open

adamgemmell wants to merge 3 commits into
rust-lang:masterfrom
adamgemmell:dev/builtins/source

Conversation

@adamgemmell

Copy link
Copy Markdown
Contributor

What does this PR try to resolve?

As part of #16960 this PR adds the Builtin source which is capable of satisfying dependencies on builtin packages. It does this by reading the rust-src component with a RecursivePathSource, returning any packages found with the SourceId it is constructed with.

This PR does not implement opaque dependencies, and as such trying to resolve builtin dependencies other than core will fail as they will depend on rustc-std-workspace-* crates which will not be patched according to the patches in the library workspace's manifest. Not attempting to resolve the transitive dependencies of builtin dependencies is what I intend to be the immediate follow up to this.

This PR depends on #17566.

How to test and review this PR?

The visible changes are shown by updating tests.

@rustbot rustbot added A-cfg-expr Area: Platform cfg expressions A-cli Area: Command-line interface, option parsing, etc. A-dependency-resolution Area: dependency resolution and the resolver A-lockfile Area: Cargo.lock issues A-testing-cargo-itself Area: cargo's tests Command-clean Command-fetch Command-fix Command-metadata Command-tree S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 8, 2026
@rustbot

rustbot commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator

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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @Muscraft, @epage, @weihanglo
  • @Muscraft, @epage, @weihanglo expanded to Muscraft, epage, weihanglo
  • Random selection from Muscraft, epage, weihanglo

Comment thread src/sources/builtin.rs Outdated
/// The unique identifier for this source
source_id: SourceId,
/// The underlying path source which discovers packages
path_source: RecursivePathSource<'gctx>,

@adamgemmell adamgemmell Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The decision to discover real packages here is an interesting one. I'm not sure it's technically necessary, but it certainly feels safer than creating synthetic Packages and Summarys especially for some unstable features like depending on crates other than core, alloc and std.

The build-std RFC intends to make the rust-src component mandatory, allowing us to do this, but this is an unresolved question.

View changes since the review

Comment thread src/workspace/source_id.rs Outdated
Ok(Box::new(PathSource::new(&path, self, gctx)))
}
SourceKind::Builtin => todo!("builtin source"),
SourceKind::Builtin => Ok(Box::new(BuiltinSource::new(self, gctx)?)),

@adamgemmell adamgemmell Oct 8, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Existing comment from @epage regarding which source to use: #16675 (comment)

Since that comment I've moved to using a separate Builtin source which wraps a RecursivePathSouce.

View changes since the review

Comment thread src/sources/builtin.rs Outdated
impl<'gctx> BuiltinSource<'gctx> {
pub fn new(source_id: SourceId, gctx: &'gctx GlobalContext) -> CargoResult<Self> {
let target_data = RustcTargetData::new(gctx, None, &[CompileKind::Host])?;
let path = detect_sysroot_src_path(&target_data)?;

@epage epage Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems overkill for users of builtin-deps without build-std and not needed for this patch series until we get to the build-std part.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's not technically needed even with build-std, though I suspect it will improve error messages when doing some things intended to be unstable like depending on unstable crates or enabling features.

The main reason I went down this route is that there's several fields which don't have sensible values and which could cause issues in the future or for users of Cargo as a library. For example, there's no valid value for the path to the manifest, and emitting an empty feature map would cause the resolver to fail on enabling features unless we hardcode it to work around that.

This design performs worse, but I feel it's significantly simpler to work with.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

To be clear, I'm happy going either way if you think returning synthetic packages is worth the complexity tradeoff. If we have to lie about anything significant, like the manifest path, I can rework Package and Summary fields make it an Option, and direct users to look at the unit graph instead when the value is None.

We would also have to be comfortable with crate metadata (authors etc) possibly being different to the real package.

Another thing to be aware of with this patch in it's current state is that resolving builtin dependencies would rely on rust-src which would cause disruption for users who aren't using rustup and wouldn't have that component installed by default when we make it so.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Small bump on this. Would you be ok with going this approach for now? I'll add a note to the tracking issue to revisit this when more features are implemented so we have more of an idea of what may break.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would recommend we start with synthetic and then adapt as needed.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I had missed that it would be a default component. I think there is a minimal profile that it wouldn't then be a part of? I think CI tends to run with minimal. Personally, I don't think it should be in minimal nor required for cargo operation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, looks like the github runners install the minimal profile. Lets try to avoid it until necessary then.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Discussed in #t-cargo > built-in deps and `cargo metadata` @ 💬.

Should we go synthetic for now?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've mostly got a synthetic in-memory version of the patch working but it might need some refactoring. An alternative I've explored but also not got quite working yet is to have the source create dummy packages on demand in the .cargo/ directory in the shape that we want. It should mean a lot less internal changes to Cargo and we wouldn't need to change anything for cargo metadata either. At the moment I'm exploring both so we can compare them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've pushed a WIP solution to in-memory-only packages to this branch, handled in cb9cef2. There's a lot of expect()s when Cargo needs a package to exist on the filesystem. Usually this happens when creating a Unit, but some pathways need a path earlier. Going forward I think I'd use an alternative type to Package to model the optional manifest path, and then convert that into a real Package when needed which would throw a hard error. I'm not sure how messy that would be given how much shared functionality the types would have.

I also opened adamgemmell#2 which caches builtin stubs on the filesystem that manifest_path (and cargo manifest) can refer to. Under this model, all packages with a "builtin" source_id() would be stub packages not intended to be built. I'd expect the performance cost to this to be small given that the cache doesn't need to be read to generate Summaries, and usually specifying a dependency results in a package being parsed from the filesystem anyway. There'd be a lot less churn

Could you please let me know which of these routes you think you'd prefer?

Comment thread src/compiler/unit_dependencies.rs Resolved
@rustbot rustbot added A-build-execution Area: anything dealing with executing the compiler A-build-scripts Area: build.rs scripts A-dep-info Area: dep-info, .d files A-directory-source Area: directory sources (vendoring) A-git Area: anything dealing with git A-interacts-with-crates.io Area: interaction with registries labels Nov 26, 2026
@rustbot

rustbot commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-build-execution Area: anything dealing with executing the compiler A-build-scripts Area: build.rs scripts A-cfg-expr Area: Platform cfg expressions A-cli Area: Command-line interface, option parsing, etc. A-dep-info Area: dep-info, .d files A-dependency-resolution Area: dependency resolution and the resolver A-directory-source Area: directory sources (vendoring) A-git Area: anything dealing with git A-interacts-with-crates.io Area: interaction with registries A-lockfile Area: Cargo.lock issues A-manifest Area: Cargo.toml issues A-rebuild-detection Area: rebuild detection and fingerprinting A-testing-cargo-itself Area: cargo's tests A-workspaces Area: workspaces Command-add Command-clean Command-fetch Command-fix Command-info Command-install Command-metadata Command-package Command-publish Command-remove Command-run Command-test Command-tree Command-vendor 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.

4 participants