Skip to content

Commit

Permalink
Add workspace support for source overlays
Browse files Browse the repository at this point in the history
Adds workspace configuration options (not user-exposed) for overlaying
sources.
  • Loading branch information
jneem committed Jun 10, 2024
1 parent 2a5633b commit c22f25b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/cargo/core/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,13 @@ pub struct LockedPatchDependency {

impl<'gctx> PackageRegistry<'gctx> {
pub fn new(gctx: &'gctx GlobalContext) -> CargoResult<PackageRegistry<'gctx>> {
let source_config = SourceConfigMap::new(gctx)?;
Self::new_with_source_config(gctx, SourceConfigMap::new(gctx)?)
}

pub fn new_with_source_config(
gctx: &'gctx GlobalContext,
source_config: SourceConfigMap<'gctx>,
) -> CargoResult<PackageRegistry<'gctx>> {
Ok(PackageRegistry {
gctx,
sources: SourceMap::new(),
Expand Down
35 changes: 35 additions & 0 deletions src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ pub struct Workspace<'gctx> {

/// Workspace-level custom metadata
custom_metadata: Option<toml::Value>,

/// Local overlay configuration. See [`crate::sources::overlay`].
local_overlays: HashMap<SourceId, PathBuf>,
}

// Separate structure for tracking loaded packages (to avoid loading anything
Expand Down Expand Up @@ -237,6 +240,7 @@ impl<'gctx> Workspace<'gctx> {
resolve_behavior: ResolveBehavior::V1,
resolve_honors_rust_version: false,
custom_metadata: None,
local_overlays: HashMap::new(),
}
}

Expand Down Expand Up @@ -1674,6 +1678,37 @@ impl<'gctx> Workspace<'gctx> {
// Cargo to panic, see issue #10545.
self.is_member(&unit.pkg) && !(unit.target.for_host() || unit.pkg.proc_macro())
}

/// Adds a local package registry overlaying a `SourceId`.
///
/// See [`crate::sources::overlay::DependencyConfusionThreatOverlaySource`] for why you shouldn't use this.
pub fn add_local_overlay(&mut self, id: SourceId, registry_path: PathBuf) {
self.local_overlays.insert(id, registry_path);
}

/// Returns all the configured local overlays, including the ones from our secret environment variable.
pub fn local_overlays(&self) -> CargoResult<impl Iterator<Item = (SourceId, SourceId)>> {
let mut ret = self
.local_overlays
.iter()
.map(|(id, path)| Ok((*id, SourceId::for_local_registry(path)?)))
.collect::<CargoResult<Vec<_>>>()?;

if let Ok(overlay) = self
.gctx
.get_env("__CARGO_TEST_DEPENDENCY_CONFUSION_VULNERABILITY_DO_NOT_USE_THIS")
{
let (url, path) = overlay.split_once('=').ok_or(anyhow::anyhow!(
"invalid overlay format. I won't tell you why; you shouldn't be using it anyway"
))?;
ret.push((
SourceId::from_url(url)?,
SourceId::for_local_registry(path.as_ref())?,
));
}

Ok(ret.into_iter())
}
}

impl<'gctx> Packages<'gctx> {
Expand Down
8 changes: 5 additions & 3 deletions src/cargo/ops/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ use crate::core::PackageSet;
use crate::core::SourceId;
use crate::core::Workspace;
use crate::ops;
use crate::sources::RecursivePathSource;
use crate::sources::{RecursivePathSource, SourceConfigMap};
use crate::util::cache_lock::CacheLockMode;
use crate::util::errors::CargoResult;
use crate::util::CanonicalUrl;
Expand Down Expand Up @@ -116,7 +116,8 @@ version. This may also occur with an optional dependency that is not enabled.";
/// This is a simple interface used by commands like `clean`, `fetch`, and
/// `package`, which don't specify any options or features.
pub fn resolve_ws<'a>(ws: &Workspace<'a>, dry_run: bool) -> CargoResult<(PackageSet<'a>, Resolve)> {
let mut registry = PackageRegistry::new(ws.gctx())?;
let source_config = SourceConfigMap::new_with_overlays(ws.gctx(), ws.local_overlays()?)?;
let mut registry = PackageRegistry::new_with_source_config(ws.gctx(), source_config)?;
let resolve = resolve_with_registry(ws, &mut registry, dry_run)?;
let packages = get_resolved_packages(&resolve, registry)?;
Ok((packages, resolve))
Expand All @@ -142,7 +143,8 @@ pub fn resolve_ws_with_opts<'gctx>(
force_all_targets: ForceAllTargets,
dry_run: bool,
) -> CargoResult<WorkspaceResolve<'gctx>> {
let mut registry = PackageRegistry::new(ws.gctx())?;
let source_config = SourceConfigMap::new_with_overlays(ws.gctx(), ws.local_overlays()?)?;
let mut registry = PackageRegistry::new_with_source_config(ws.gctx(), source_config)?;
let (resolve, resolved_with_overrides) = if ws.ignore_lock() {
let add_patches = true;
let resolve = None;
Expand Down

0 comments on commit c22f25b

Please sign in to comment.