Skip to content

Commit

Permalink
fix: Respect --locked flag
Browse files Browse the repository at this point in the history
When using `--rust-version`, the lockfile can be blown away.
While that is a problem in of its own,
this focuses on an incremental step of not blowing it away if `--locked`
is used.

This is part of #234
  • Loading branch information
epage committed Feb 19, 2024
1 parent 6350a49 commit 57f037b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ OPTIONS:
--manifest-path <PATH>
Path to Cargo.toml.

--locked
Require Cargo.lock is up to date.

-F, --features <FEATURES>...
Space or comma separated list of features to activate.

Expand Down
6 changes: 6 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub(crate) struct Args {

pub(crate) subcommand: Option<String>,

/// --locked
pub(crate) locked: bool,
/// --manifest-path <PATH>
pub(crate) manifest_path: Option<String>,
/// --no-manifest-path
Expand Down Expand Up @@ -154,6 +156,7 @@ impl Args {
let mut clean_per_version = false;
let mut keep_going = false;
let mut print_command_list = false;
let mut locked = false;
let mut no_manifest_path = false;
let mut rust_version = false;
let mut version_range = None;
Expand Down Expand Up @@ -306,6 +309,7 @@ impl Args {
Long("clean-per-version") => parse_flag!(clean_per_version),
Long("keep-going") => parse_flag!(keep_going),
Long("print-command-list") => parse_flag!(print_command_list),
Long("locked") => parse_flag!(locked),
Long("no-manifest-path") => parse_flag!(no_manifest_path),
Long("ignore-unknown-features") => parse_flag!(ignore_unknown_features),
Short('v') | Long("verbose") => verbose += 1,
Expand Down Expand Up @@ -624,6 +628,7 @@ impl Args {
clean_per_version,
keep_going,
print_command_list,
locked,
no_manifest_path,
include_features: include_features.into_iter().map(Into::into).collect(),
at_least_one_of,
Expand Down Expand Up @@ -700,6 +705,7 @@ const HELP: &[HelpText<'_>] = &[
"This flag can only be used together with --workspace",
]),
("", "--manifest-path", "<PATH>", "Path to Cargo.toml", &[]),
("", "--locked", "", "Require Cargo.lock is up to date", &[]),
("-F", "--features", "<FEATURES>...", "Space or comma separated list of features to activate", &[]),
("", "--each-feature", "", "Perform for each feature of the package", &[
"This also includes runs with just --no-default-features flag, and default features.",
Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ fn try_main() -> Result<()> {

// First, generate the lockfile using the oldest cargo specified.
// https://github.com/taiki-e/cargo-hack/issues/105
let mut generate_lockfile = true;
let mut generate_lockfile = !cx.locked;
// Workaround for spurious "failed to select a version" error.
// (This does not work around the underlying cargo bug: https://github.com/rust-lang/cargo/issues/10623)
let mut regenerate_lockfile_on_51_or_up = false;
Expand Down Expand Up @@ -392,6 +392,9 @@ fn exec_on_packages(
keep_going: &mut KeepGoing,
cargo_version: u32,
) -> Result<()> {
if cx.locked {
line.arg("--locked");
}
if cx.target.is_empty() || cargo_version >= 64 {
// TODO: We should test that cargo's multi-target build does not break the resolver behavior required for a correct check.
for target in &cx.target {
Expand Down Expand Up @@ -625,6 +628,9 @@ fn exec_cargo_inner(
fn cargo_clean(cx: &Context, id: Option<&PackageId>) -> Result<()> {
let mut line = cx.cargo();
line.arg("clean");
if cx.locked {
line.arg("--locked");
}
if let Some(id) = id {
line.arg("--package");
line.arg(&cx.packages(id).name);
Expand Down
3 changes: 3 additions & 0 deletions tests/long-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ OPTIONS:
--manifest-path <PATH>
Path to Cargo.toml.

--locked
Require Cargo.lock is up to date.

-F, --features <FEATURES>...
Space or comma separated list of features to activate.

Expand Down
1 change: 1 addition & 0 deletions tests/short-help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ OPTIONS:
--workspace Perform command for all packages in the workspace
--exclude <SPEC>... Exclude packages from the check
--manifest-path <PATH> Path to Cargo.toml
--locked Require Cargo.lock is up to date
-F, --features <FEATURES>... Space or comma separated list of features to activate
--each-feature Perform for each feature of the package
--feature-powerset Perform for the feature powerset of the package
Expand Down

0 comments on commit 57f037b

Please sign in to comment.