Repository: mozilla/cargo-vet
Pull request #581 (2023-12-01 19:45:11)

Add --cargo-arg option for arguments to pass through to cargo
Some projects may require additional arguments in the cargo metadata command in order to parse their Cargo.toml. For example Chromium requires `-Zbindeps` as it uses the feature for a dependency on cxxbridge-cmd. This allows such projects to use `cargo vet --cargo-arg=<STUFF>` in order to add one or more arguments to the cargo metadata command line.

danakj: Not sure if there's an expectation for tests here, but locally I did the following:

```
% cargo run -- vet --cargo-arg=-Zbindeps --verbose info
    Finished dev [unoptimized + debuginfo] target(s) in 0.14s
     Running `target/debug/cargo-vet vet --cargo-arg=-Zbindeps --verbose info`
 INFO Running: Command {
    program: "/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/cargo",
    args: [
        "/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/cargo",
        "metadata",
        "--format-version",
        "1",
        "--all-features",
        "--locked",
        "--color=always",
        "-Zbindeps",
    ],
    create_pidfd: false,
}
 INFO Final Metadata Config: 
...
```
bholley: Thanks for the patch! @mystor can you review?

Patch:
diff --git a/src/cli.rs b/src/cli.rs
index de6a492..11213a6 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -166,6 +166,16 @@ pub struct Cli {
     #[clap(help_heading = "GLOBAL OPTIONS", global = true)]
     pub filter_graph: Option<Vec<GraphFilter>>,
 
+    /// Arguments to pass through to cargo. It can be specified multiple times for
+    /// multiple arguments.
+    ///
+    /// Example: `--cargo-arg=-Zbindeps`
+    ///
+    /// This allows using unstable options in Cargo if a project's Cargo.toml requires them.
+    #[clap(long, action)]
+    #[clap(help_heading = "GLOBAL OPTIONS", global = true)]
+    pub cargo_arg: Vec<String>,
+
     // Args for `Check` when the subcommand is not explicitly specified.
     //
     // These are exclusive with specifying a subcommand due to
diff --git a/src/main.rs b/src/main.rs
index 016337a..7e6bee3 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -390,6 +390,7 @@ fn real_main() -> Result<(), miette::Report> {
     {
         other_options.push("--color=always".to_string());
     }
+    other_options.extend(cli.cargo_arg.iter().cloned());
     cmd.other_options(other_options);
 
     info!("Running: {:#?}", cmd.cargo_command());


Test patch:
diff --git a/tests/snapshots/test_cli__long-help.snap b/tests/snapshots/test_cli__long-help.snap
index 6cf6efc..5a155ff 100644
--- a/tests/snapshots/test_cli__long-help.snap
+++ b/tests/snapshots/test_cli__long-help.snap
@@ -1,5 +1,6 @@
 ---
 source: tests/test-cli.rs
+assertion_line: 104
 expression: format_outputs(&output)
 ---
 stdout:
@@ -125,6 +126,14 @@ GLOBAL OPTIONS:
             * `is_dev_only($bool)`: whether it's only used by dev (test) builds in the original
             graph
 
+        --cargo-arg <CARGO_ARG>
+            Arguments to pass through to cargo. It can be specified multiple times for multiple
+            arguments.
+            
+            Example: `--cargo-arg=-Zbindeps`
+            
+            This allows using unstable options in Cargo if a project's Cargo.toml requires them.
+
 SUBCOMMANDS:
     check
             \[default\] Check that the current project has been vetted
diff --git a/tests/snapshots/test_cli__markdown-help.snap b/tests/snapshots/test_cli__markdown-help.snap
index c7ef473..080db01 100644
--- a/tests/snapshots/test_cli__markdown-help.snap
+++ b/tests/snapshots/test_cli__markdown-help.snap
@@ -1,5 +1,6 @@
 ---
 source: tests/test-cli.rs
+assertion_line: 134
 expression: format_outputs(&output)
 ---
 stdout:
@@ -134,6 +135,14 @@ tested)
 * `is_dev_only($bool)`: whether it's only used by dev (test) builds in the original
 graph
 
+#### `--cargo-arg <CARGO_ARG>`
+Arguments to pass through to cargo. It can be specified multiple times for multiple
+arguments.
+
+Example: `--cargo-arg=-Zbindeps`
+
+This allows using unstable options in Cargo if a project's Cargo.toml requires them.
+
 ### SUBCOMMANDS
 * [check](#cargo-vet-check): \[default\] Check that the current project has been vetted
 * [suggest](#cargo-vet-suggest): Suggest some low-hanging fruit to review
diff --git a/tests/snapshots/test_cli__short-help.snap b/tests/snapshots/test_cli__short-help.snap
index 90dc1c6..738e362 100644
--- a/tests/snapshots/test_cli__short-help.snap
+++ b/tests/snapshots/test_cli__short-help.snap
@@ -1,5 +1,6 @@
 ---
 source: tests/test-cli.rs
+assertion_line: 119
 expression: format_outputs(&output)
 ---
 stdout:
@@ -63,6 +64,10 @@ GLOBAL OPTIONS:
         --filter-graph <FILTER_GRAPH>
             Filter out different parts of the build graph and pretend that's the true graph
 
+        --cargo-arg <CARGO_ARG>
+            Arguments to pass through to cargo. It can be specified multiple times for multiple
+            arguments
+
 SUBCOMMANDS:
     check               \[default\] Check that the current project has been vetted
     suggest             Suggest some low-hanging fruit to review