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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we remove this? Did it cause any issue?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order for Cargo to actually attempt to write the std lockfile we need to resolve with optional_deps again. Optional packages aren't actually built.

I guess it is for this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let print = if !ws.is_ephemeral() && ws.require_optional_deps() {
if !dry_run {
ops::write_pkg_lockfile(ws, &mut resolve)?
} else {
true
}

Okay it is this one.

I am unsure if removing this is the correct approach. require_optional_deps affects how we resolve dependencies. And with that std starts respecting path override.

} else if ws.require_optional_deps() {
// First, resolve the root_package's *listed* dependencies, as well as
// downloading and updating all remotes and such.
let resolve = resolve_with_registry(ws, &mut registry, dry_run)?;
// No need to add patches again, `resolve_with_registry` has done it.
let add_patches = false;
// Second, resolve with precisely what we're doing. Filter out
// transitive dependencies if necessary, specify features, handle
// overrides, etc.
add_overrides(&mut registry, ws)?;

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
Comment on lines +106 to +108
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are already too many fields controlling the locked behavior. Per Ed's suggestion, I'd like to see if we can add one line explaining this field is independent from --locked CLI flag. Perhaps something like:

Suggested change
/// 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
/// 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.
///
/// This field is independent from any flags like `--locked` or `--frozen`.
/// Use [`GlobalContext::locked()`] whenever you have an access to mutable
/// to [`GlobalContext`], such as at Command arg parsing level.

/// 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."
Comment on lines +71 to +73
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Lower case for first letter by convention in this repo.
  • For rust-src component, it should be conditioned on the availability of rustup. You may use cargo::util::is_rustup to check.
Suggested change
"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."
"Attempted to write to the standard library's lockfile at: {}.\n\
This most likely means the lockfile has been previously modified by mistake.\
Try removing and readding the `rust-src` rustup component.
lock_root.as_path_unlocked().join(LOCKFILE_NAME).display(),

);
}

// 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.