Skip to content

Commit 4e7dfb1

Browse files
committed
Implement --locked for build-std
1 parent 15fbd2f commit 4e7dfb1

File tree

4 files changed

+125
-6
lines changed

4 files changed

+125
-6
lines changed

src/cargo/core/compiler/standard_lib.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,8 @@ pub fn resolve_std<'gctx>(
7575
let src_path = detect_sysroot_src_path(target_data)?;
7676
let std_ws_manifest_path = src_path.join("Cargo.toml");
7777
let gctx = ws.gctx();
78-
// TODO: Consider doing something to enforce --locked? Or to prevent the
79-
// lock file from being written, such as setting ephemeral.
8078
let mut std_ws = Workspace::new(&std_ws_manifest_path, gctx)?;
81-
// Don't require optional dependencies in this workspace, aka std's own
82-
// `[dev-dependencies]`. No need for us to generate a `Resolve` which has
83-
// those included because we'll never use them anyway.
84-
std_ws.set_require_optional_deps(false);
79+
std_ws.set_is_locked(true);
8580
// `sysroot` is not in the default set because it is optional, but it needs
8681
// to be part of the resolve in case we do need it or `libtest`.
8782
let mut spec_pkgs = Vec::from(crates);

src/cargo/core/workspace.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ pub struct Workspace<'gctx> {
103103
/// file. This is set for `cargo install` without `--locked`.
104104
ignore_lock: bool,
105105

106+
/// If `true`, then the resolver will not update the `Cargo.lock` file and
107+
/// return an error if the lockfile is missing or out of date, similar
108+
/// to the `GlobalContext::locked()` behaviour. Note that
109+
/// the lockfile is not written if `require_optional_deps` is false.
110+
is_locked: bool,
111+
106112
/// Requested path of the lockfile (i.e. passed as the cli flag)
107113
requested_lockfile_path: Option<PathBuf>,
108114

@@ -240,6 +246,7 @@ impl<'gctx> Workspace<'gctx> {
240246
member_ids: HashSet::new(),
241247
default_members: Vec::new(),
242248
is_ephemeral: false,
249+
is_locked: false,
243250
require_optional_deps: true,
244251
loaded_packages: RefCell::new(HashMap::new()),
245252
ignore_lock: false,
@@ -635,6 +642,14 @@ impl<'gctx> Workspace<'gctx> {
635642
self
636643
}
637644

645+
pub fn is_locked(&self) -> bool {
646+
self.is_locked
647+
}
648+
649+
pub fn set_is_locked(&mut self, is_locked: bool) {
650+
self.is_locked = is_locked
651+
}
652+
638653
/// Returns the directory where the lockfile is in.
639654
pub fn lock_root(&self) -> Filesystem {
640655
if let Some(requested) = self.requested_lockfile_path.as_ref() {

src/cargo/ops/lockfile.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ pub fn write_pkg_lockfile(ws: &Workspace<'_>, resolve: &mut Resolve) -> CargoRes
6666
);
6767
}
6868

69+
if ws.is_locked() {
70+
anyhow::bail!(
71+
"Attempted to write to the standard library's lockfile.\n\
72+
This most likely means the lockfile has been previously modified by mistake.\
73+
Try removing and readding the `rust-src` component."
74+
);
75+
}
76+
6977
// While we're updating the lock file anyway go ahead and update its
7078
// encoding to whatever the latest default is. That way we can slowly roll
7179
// out lock file updates as they're otherwise already updated, and changes

tests/testsuite/mock-std/library/Cargo.lock

Lines changed: 101 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)