diff --git a/src/cargo/core/compiler/standard_lib.rs b/src/cargo/core/compiler/standard_lib.rs index a3b2ff8acd19..6168fd5a5e25 100644 --- a/src/cargo/core/compiler/standard_lib.rs +++ b/src/cargo/core/compiler/standard_lib.rs @@ -78,6 +78,7 @@ pub fn resolve_std<'gctx>( // TODO: Consider doing something to enforce --locked? Or to prevent the // lock file from being written, such as setting ephemeral. let mut std_ws = Workspace::new(&std_ws_manifest_path, gctx)?; + std_ws.set_is_std(true); // Don't require optional dependencies in this workspace, aka std's own // `[dev-dependencies]`. No need for us to generate a `Resolve` which has // those included because we'll never use them anyway. diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index 28897365b890..855e698872d0 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -120,6 +120,9 @@ pub struct Workspace<'gctx> { /// Local overlay configuration. See [`crate::sources::overlay`]. local_overlays: HashMap, + + /// Whether this is the workspace used for building the standard library + is_std: bool, } // Separate structure for tracking loaded packages (to avoid loading anything @@ -250,6 +253,7 @@ impl<'gctx> Workspace<'gctx> { resolve_honors_rust_version: false, custom_metadata: None, local_overlays: HashMap::new(), + is_std: false, } } @@ -1790,6 +1794,14 @@ impl<'gctx> Workspace<'gctx> { Ok(ret.into_iter()) } + + pub fn is_std(&self) -> bool { + self.is_std + } + + pub fn set_is_std(&mut self, is_std: bool) { + self.is_std = is_std + } } impl<'gctx> Packages<'gctx> { diff --git a/src/cargo/ops/lockfile.rs b/src/cargo/ops/lockfile.rs index d865a7186f64..b927756b2886 100644 --- a/src/cargo/ops/lockfile.rs +++ b/src/cargo/ops/lockfile.rs @@ -66,6 +66,13 @@ pub fn write_pkg_lockfile(ws: &Workspace<'_>, resolve: &mut Resolve) -> CargoRes ); } + if ws.is_std() { + anyhow::bail!( + "Attempted to write the standard library's lockfile.\n\ + This most likely means the lockfile has been previously modified by mistake." + ); + } + // While we're updating the lock file anyway go ahead and update its // encoding to whatever the latest default is. That way we can slowly roll // out lock file updates as they're otherwise already updated, and changes