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 taiki-e#234
  • Loading branch information
epage committed Feb 20, 2024
1 parent 6204729 commit 81c1167
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ jobs:
cargo hack check --feature-powerset --workspace --message-format=json
cd ../rust-version
rustup toolchain remove 1.63 1.64 1.65
cargo hack check --rust-version --workspace
cargo hack check --rust-version --workspace --locked
cargo uninstall cargo-hack
- uses: taiki-e/install-action@cargo-hack
- uses: taiki-e/install-action@cargo-minimal-versions
Expand Down
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 @@ -25,6 +25,8 @@ pub(crate) struct Args {
pub(crate) manifest_path: Option<String>,
/// --no-manifest-path
pub(crate) no_manifest_path: bool,
/// --locked
pub(crate) locked: bool,
/// -p, --package <SPEC>...
pub(crate) package: Vec<String>,
/// --exclude <SPEC>...
Expand Down Expand Up @@ -155,6 +157,7 @@ impl Args {
let mut keep_going = false;
let mut print_command_list = false;
let mut no_manifest_path = false;
let mut locked = false;
let mut rust_version = false;
let mut version_range = None;
let mut version_step = None;
Expand Down Expand Up @@ -307,6 +310,7 @@ impl Args {
Long("keep-going") => parse_flag!(keep_going),
Long("print-command-list") => parse_flag!(print_command_list),
Long("no-manifest-path") => parse_flag!(no_manifest_path),
Long("locked") => parse_flag!(locked),
Long("ignore-unknown-features") => parse_flag!(ignore_unknown_features),
Short('v') | Long("verbose") => verbose += 1,

Expand Down Expand Up @@ -609,6 +613,7 @@ impl Args {
subcommand,

manifest_path,
locked,
package,
exclude,
workspace,
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
4 changes: 2 additions & 2 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1466,7 +1466,7 @@ fn version_range() {
"--workspace",
"--locked",
])
.assert_failure("rust-version")
.assert_success("rust-version")
.stderr_contains(
"
running `rustup run 1.63 cargo check --locked` on member1 (1/7)
Expand Down Expand Up @@ -1496,7 +1496,7 @@ fn rust_version() {
",
);
cargo_hack(["check", "--rust-version", "--workspace", "--locked"])
.assert_failure("rust-version")
.assert_success("rust-version")
.stderr_contains(
"
running `rustup run 1.63 cargo check --locked` on member1 (1/4)
Expand Down

0 comments on commit 81c1167

Please sign in to comment.