Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ cargo-platform = { path = "crates/cargo-platform", version = "0.3.3" }
cargo-test-macro = { version = "0.4.15", path = "crates/cargo-test-macro" }
cargo-test-support = { version = "0.12.0", path = "crates/cargo-test-support" }
cargo-util = { version = "0.2.33", path = "crates/cargo-util" }
cargo-util-schemas = { version = "0.14.4", path = "crates/cargo-util-schemas" }
cargo-util-schemas = { version = "0.15.0", path = "crates/cargo-util-schemas" }
cargo-util-terminal = { version = "0.1.3", path = "crates/cargo-util-terminal" }
cargo_metadata = "0.23.1"
clap = "4.6.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/cargo-util-schemas/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-util-schemas"
version = "0.14.4"
version = "0.15.0"
rust-version = "1.98" # MSRV:1
edition.workspace = true
license.workspace = true
Expand Down
7 changes: 7 additions & 0 deletions crates/cargo-util-schemas/src/core/source_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub enum SourceKind {
LocalRegistry,
/// A directory-based registry.
Directory,
/// Package sources distributed with the rust toolchain
Builtin,
}

// The hash here is important for what folder packages get downloaded into.
Expand All @@ -40,6 +42,7 @@ impl SourceKind {
SourceKind::SparseRegistry => None,
SourceKind::LocalRegistry => Some("local-registry"),
SourceKind::Directory => Some("directory"),
SourceKind::Builtin => Some("builtin"),
}
}
}
Expand Down Expand Up @@ -71,6 +74,10 @@ impl Ord for SourceKind {
(_, SourceKind::Directory) => Ordering::Greater,

(SourceKind::Git(a), SourceKind::Git(b)) => a.cmp(b),
(SourceKind::Git(_), _) => Ordering::Less,
(_, SourceKind::Git(_)) => Ordering::Greater,

(SourceKind::Builtin, SourceKind::Builtin) => Ordering::Equal,
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions crates/resolver-tests/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ impl<T: AsRef<str>, U: AsRef<str>> ToPkgId for (T, U) {
}
}

#[derive(Copy, Clone)]
pub struct BuiltinPid {
pub name: &'static str,
}

impl ToPkgId for BuiltinPid {
fn to_pkgid(&self) -> PackageId {
PackageId::try_new(self.name, "0.0.0", builtin_loc()).unwrap()
}
}

#[macro_export]
macro_rules! pkg {
($pkgid:expr => [$($deps:expr),* $(,)? ]) => ({
Expand All @@ -108,6 +119,13 @@ fn registry_loc() -> SourceId {
*example_dot
}

fn builtin_loc() -> SourceId {
static LOCAL_PATH: OnceLock<SourceId> = OnceLock::new();
let local_path = LOCAL_PATH
.get_or_init(|| SourceId::for_builtin(&std::env::current_dir().unwrap()).unwrap());
*local_path
}

pub fn pkg<T: ToPkgId>(name: T) -> Summary {
pkg_dep(name, Vec::new())
}
Expand Down Expand Up @@ -215,6 +233,10 @@ pub fn dep_loc(name: &str, location: &str) -> Dependency {
Dependency::parse(name, Some("1.0.0"), source_id).unwrap()
}

pub fn dep_builtin(name: &str) -> Dependency {
Dependency::parse(name, None, builtin_loc()).unwrap()
}

pub fn dep_kind(name: &str, kind: DepKind) -> Dependency {
let mut dep = dep(name);
dep.set_kind(kind);
Expand All @@ -235,6 +257,12 @@ pub fn names<P: ToPkgId>(names: &[P]) -> Vec<PackageId> {
names.iter().map(|name| name.to_pkgid()).collect()
}

/// For a set of name specifiers of varying types
#[macro_export]
macro_rules! names {
($($name:expr),* $(,)?) => {&vec![$($name.to_pkgid()),*]};
}

pub fn loc_names(names: &[(&'static str, &'static str)]) -> Vec<PackageId> {
names
.iter()
Expand Down
24 changes: 21 additions & 3 deletions crates/resolver-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ pub fn resolve_and_validated_raw(
root_pkg_id: PackageId,
sat_resolver: &mut SatResolver,
) -> CargoResult<Vec<(PackageId, Vec<InternedString>)>> {
let resolve = resolve_with_global_context_raw(
let resolve = resolve_with_gctx_implicit_deps_raw(
deps.clone(),
registry,
root_pkg_id,
&GlobalContext::default().unwrap(),
&[],
);

match resolve {
Expand Down Expand Up @@ -115,20 +116,36 @@ fn collect_features(resolve: &Resolve) -> Vec<(PackageId, Vec<InternedString>)>
.collect()
}

pub fn resolve_with_implicit_builtins(
deps: Vec<Dependency>,
registry: &[Summary],
implicit_builtin_deps: &[Dependency],
) -> CargoResult<Resolve> {
let gctx = GlobalContext::default().unwrap();
resolve_with_gctx_implicit_deps_raw(
deps,
registry,
pkg_id("root"),
&gctx,
implicit_builtin_deps,
)
}

pub fn resolve_with_global_context(
deps: Vec<Dependency>,
registry: &[Summary],
gctx: &GlobalContext,
) -> CargoResult<Vec<(PackageId, Vec<InternedString>)>> {
let resolve = resolve_with_global_context_raw(deps, registry, pkg_id("root"), gctx)?;
let resolve = resolve_with_gctx_implicit_deps_raw(deps, registry, pkg_id("root"), gctx, &[])?;
Ok(collect_features(&resolve))
}

pub fn resolve_with_global_context_raw(
fn resolve_with_gctx_implicit_deps_raw(
deps: Vec<Dependency>,
registry: &[Summary],
root_pkg_id: PackageId,
gctx: &GlobalContext,
implicit_builtin_deps: &[Dependency],
) -> CargoResult<Resolve> {
struct MyRegistry<'a> {
list: &'a [Summary],
Expand Down Expand Up @@ -205,6 +222,7 @@ pub fn resolve_with_global_context_raw(
&version_prefs,
ResolveVersion::with_rust_version(None),
gctx,
implicit_builtin_deps,
);

// The largest test in our suite takes less then 30 secs.
Expand Down
73 changes: 70 additions & 3 deletions crates/resolver-tests/tests/resolve.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use cargo::util::GlobalContext;
use cargo::util::interning::InternedString;
use cargo::workspace::Dependency;
use cargo::workspace::dependency::DepKind;
use resolver_tests::helpers::dep_builtin;
use resolver_tests::resolve_with_implicit_builtins;
use snapbox::assert_data_eq;
use snapbox::str;

use resolver_tests::{
helpers::{
ToDep, ToPkgId, assert_contains, assert_same, dep, dep_kind, dep_loc, dep_req, loc_names,
names, pkg, pkg_dep, pkg_dep_with, pkg_id, pkg_loc, registry,
BuiltinPid, ToDep, ToPkgId, assert_contains, assert_same, dep, dep_kind, dep_loc, dep_req,
loc_names, names, pkg, pkg_dep, pkg_dep_with, pkg_id, pkg_loc, registry,
},
pkg, resolve, resolve_with_global_context,
names, pkg, resolve, resolve_with_global_context,
};

#[test]
Expand Down Expand Up @@ -1036,3 +1039,67 @@ failed to select a version for `F` which could resolve this conflict
"#]]
);
}

#[test]
fn test_builtin_dependency() {
let core = BuiltinPid { name: "core" };
let reg = registry(vec![pkg!(core)]);

let builtin_dep = dep_builtin("core");

let res = resolve(vec![builtin_dep], &reg).unwrap();

assert_same(&res, &names!("root", core));
}

#[test]
fn normal_dependency_is_not_satisfied_by_builtin_package() {
let core = BuiltinPid { name: "core" };
let reg = registry(vec![pkg!(core)]);

assert!(resolve(vec![dep("core")], &reg).is_err());
}

#[test]
fn missing_builtin_dependency_errors() {
assert!(resolve(vec![dep_builtin("core")], &registry(vec![])).is_err());
}

#[test]
fn injected_builtins() {
let core = BuiltinPid { name: "core" };
let core = pkg!(core);
let compiler_builtins = BuiltinPid {
name: "compiler_builtins",
};
let compiler_builtins = pkg!(compiler_builtins);

let reg = registry(vec![core.clone(), compiler_builtins.clone()]);

let mut deps = vec![];
deps.push(
Dependency::new_implicit_builtin(
InternedString::new("core"),
&core.source_id().local_path().unwrap(),
)
.unwrap(),
);
deps.push(
Dependency::new_implicit_builtin(
InternedString::new("compiler_builtins"),
&core.source_id().local_path().unwrap(),
)
.unwrap(),
);

let resolve = resolve_with_implicit_builtins(Vec::new(), &reg, &deps).unwrap();

let root_deps = resolve
.deps(pkg_id("root"))
.map(|(pkg_id, _)| pkg_id)
.collect::<Vec<_>>();
assert_same(
&root_deps,
&[core.package_id(), compiler_builtins.package_id()],
);
}
10 changes: 7 additions & 3 deletions src/compiler/standard_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::ops::{self, Packages};
use crate::resolver::HasDevUnits;
use crate::resolver::Resolve;
use crate::resolver::features::{CliFeatures, FeaturesFor, ResolvedFeatures};
use crate::util::errors::CargoResult;
use crate::util::CargoResult;
use crate::workspace::profiles::{Profiles, UnitFor};
use crate::workspace::{PackageId, PackageSet, Workspace};

Expand All @@ -16,7 +16,11 @@ use std::path::PathBuf;

use super::BuildConfig;

fn std_crates<'a>(crates: &'a [String], default: &'static str, units: &[Unit]) -> HashSet<&'a str> {
pub fn std_crates<'a>(
crates: &'a [String],
default: &'static str,
units: &[Unit],
) -> HashSet<&'a str> {
let mut crates = HashSet::from_iter(crates.iter().map(|s| s.as_str()));
// This is a temporary hack until there is a more principled way to
// declare dependencies in Cargo.toml.
Expand Down Expand Up @@ -217,7 +221,7 @@ fn generate_roots(
Ok(())
}

fn detect_sysroot_src_path(target_data: &RustcTargetData<'_>) -> CargoResult<PathBuf> {
pub(crate) fn detect_sysroot_src_path(target_data: &RustcTargetData<'_>) -> CargoResult<PathBuf> {
if let Some(s) = target_data.gctx.get_env_os("__CARGO_TESTS_ONLY_SRC_ROOT") {
return Ok(s.into());
}
Expand Down
4 changes: 4 additions & 0 deletions src/ops/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,13 +528,17 @@ pub fn resolve_with_previous<'gctx>(

let replace = lock_replacements(ws, previous, &keep);

//TODO: Enable implicit builtin dependencies for `-Zbuild-std` once builtins are fully implemented
let implicit_builtin_deps = &[];

let mut resolved = resolver::resolve(
&summaries,
&replace,
registry,
&version_prefs,
ResolveVersion::with_rust_version(ws.lowest_rust_version()),
ws.gctx(),
implicit_builtin_deps,

@adamgemmell adamgemmell Sep 2, 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 concern from @epage regarding this being tunnelled through to the resolver #16675 (comment)

Since then this no longer needs to be tunnelled from outside of ops/resolve.rs from other modules in opts as whether to inject builtins can be determined from within this function.

View changes since the review

)?;

let patches = registry.patches().values().flat_map(|v| v.iter());
Expand Down
12 changes: 11 additions & 1 deletion src/resolver/dep_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,16 @@ pub struct RegistryQueryer<'a, T: Registry> {
(Option<PackageId>, Summary, ResolveOpts),
(Rc<(HashSet<InternedString>, Rc<Vec<DepInfo>>)>, bool),
>,
/// The set of builtin dependencies to inject when appropriate
implicit_builtin_deps: &'a [Dependency],
}

impl<'a, T: Registry> RegistryQueryer<'a, T> {
pub fn new(
registry: &'a T,
replacements: &'a [(PackageIdSpec, Dependency)],
version_prefs: &'a VersionPreferences,
implicit_builtin_deps: &'a [Dependency],
) -> Self {
let inner = Rc::new(RegistryQueryerAsync::new(
registry,
Expand All @@ -236,6 +239,7 @@ impl<'a, T: Registry> RegistryQueryer<'a, T> {
inner: inner.clone(),
poller: LocalPollAdapter::new(inner),
summary_cache: HashMap::default(),
implicit_builtin_deps,
}
}

Expand Down Expand Up @@ -308,7 +312,13 @@ impl<'a, T: Registry> RegistryQueryer<'a, T> {
// First, figure out our set of dependencies based on the requested set
// of features. This also calculates what features we're going to enable
// for our own dependencies.
let (used_features, deps) = resolve_features(parent, candidate, opts)?;
let (used_features, mut deps) = resolve_features(parent, candidate, opts)?;

if !candidate.source_id().is_builtin() {
for dep in self.implicit_builtin_deps {
deps.push((dep.clone(), Rc::new(BTreeSet::default())));

@adamgemmell adamgemmell Sep 2, 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 where exactly we should be injecting builtin deps: #16675 (comment)

Since the original comment this has moved to build_deps, which is the original point where the resolver discovers dependencies. Moving this lower means modifying data structures which are intended to be immutable, and moving it higher means adding to already huge functions and moves it outside the cache.

View changes since the review

}
}

// Next, transform all dependencies into a list of possible candidates
// which can satisfy that dependency.
Expand Down
2 changes: 1 addition & 1 deletion src/resolver/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ pub fn encodable_package_id(
}

fn encodable_source_id(id: SourceId, version: ResolveVersion) -> Option<TomlLockfileSourceId> {
if id.is_path() {
if id.is_path() || id.is_builtin() {
None
} else {
Some(
Expand Down
9 changes: 8 additions & 1 deletion src/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,19 @@ pub fn resolve(
version_prefs: &VersionPreferences,
resolve_version: ResolveVersion,
gctx: &GlobalContext,
implicit_builtin_deps: &[Dependency],

@adamgemmell adamgemmell Sep 2, 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 concern from @epage regarding usage of bools in parameter lists #16675 (comment)

Since then this has been changed to a more descriptive slice of Dependencys.

View changes since the review

) -> CargoResult<Resolve> {
let first_version = gctx
.cli_unstable()
.direct_minimal_versions
.then_some(VersionOrdering::MinimumVersionsFirst);
let mut registry = RegistryQueryer::new(registry, replacements, version_prefs);

let mut registry = RegistryQueryer::new(
registry,
replacements,
version_prefs,
&implicit_builtin_deps,
);

// Global cache of the reasons for each time we backtrack.
let mut past_conflicting_activations = conflict_cache::ConflictCache::new();
Expand Down
Loading