Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement --locked for build-std #14589

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
7 changes: 1 addition & 6 deletions src/cargo/core/compiler/standard_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,8 @@ pub fn resolve_std<'gctx>(
let src_path = detect_sysroot_src_path(target_data)?;
let std_ws_manifest_path = src_path.join("Cargo.toml");
let gctx = ws.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)?;
// 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.
std_ws.set_require_optional_deps(false);
std_ws.set_is_locked(true);
// `sysroot` is not in the default set because it is optional, but it needs
// to be part of the resolve in case we do need it or `libtest`.
let mut spec_pkgs = Vec::from(crates);
Expand Down
15 changes: 15 additions & 0 deletions src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ pub struct Workspace<'gctx> {
/// file. This is set for `cargo install` without `--locked`.
ignore_lock: bool,

/// If `true`, then the resolver will not update the `Cargo.lock` file and
/// return an error if the lockfile is missing or out of date, similar
/// to the `GlobalContext::locked()` behaviour. Note that
/// the lockfile is not written if `require_optional_deps` is false.
is_locked: bool,

/// Requested path of the lockfile (i.e. passed as the cli flag)
requested_lockfile_path: Option<PathBuf>,

Expand Down Expand Up @@ -240,6 +246,7 @@ impl<'gctx> Workspace<'gctx> {
member_ids: HashSet::new(),
default_members: Vec::new(),
is_ephemeral: false,
is_locked: false,
require_optional_deps: true,
loaded_packages: RefCell::new(HashMap::new()),
ignore_lock: false,
Expand Down Expand Up @@ -635,6 +642,14 @@ impl<'gctx> Workspace<'gctx> {
self
}

pub fn is_locked(&self) -> bool {
self.is_locked
}

pub fn set_is_locked(&mut self, is_locked: bool) {
self.is_locked = is_locked
}

/// Returns the directory where the lockfile is in.
pub fn lock_root(&self) -> Filesystem {
if let Some(requested) = self.requested_lockfile_path.as_ref() {
Expand Down
8 changes: 8 additions & 0 deletions src/cargo/ops/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ pub fn write_pkg_lockfile(ws: &Workspace<'_>, resolve: &mut Resolve) -> CargoRes
);
}

if ws.is_locked() {
anyhow::bail!(
"Attempted to write to the standard library's lockfile.\n\
This most likely means the lockfile has been previously modified by mistake.\
Try removing and readding the `rust-src` component."
);
}

// 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
Expand Down
101 changes: 101 additions & 0 deletions tests/testsuite/mock-std/library/Cargo.lock

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