Skip to content

Commit

Permalink
fix(cargo): added extra tool opts (#3144)
Browse files Browse the repository at this point in the history
Fixes #2753
Fixes #3143
Fixes #2963
  • Loading branch information
jdx authored Nov 22, 2024
1 parent 2a78871 commit 9169070
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 9 deletions.
39 changes: 34 additions & 5 deletions docs/dev-tools/backends/cargo.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
You may install packages directly from [Cargo Crates](https://crates.io/) even if there
isn't an asdf plugin for it.

The code for this is inside of the mise repository at [`./src/backend/cargo.rs`](https://github.com/jdx/mise/blob/main/src/backend/cargo.rs).
The code for this is inside the mise repository at [`./src/backend/cargo.rs`](https://github.com/jdx/mise/blob/main/src/backend/cargo.rs).

## Dependencies

Expand Down Expand Up @@ -74,18 +74,47 @@ go in `[tools]` in `mise.toml`.

### `features`

Install additional components:
Install additional components (passed as `cargo install --features`):

```toml
[tools]
"cargo:cargo-edit" = { features = "add" }
"cargo:cargo-edit" = { version = "latest", features = "add" }
```

### `default-features`

Disable default features:
Disable default features (passed as `cargo install --no-default-features`):

```toml
[tools]
"cargo:cargo-edit" = { default-features = false }
"cargo:cargo-edit" = { version = "latest", default-features = false }
```

### `bin`

Select the CLI bin name to install when multiple are available (passed as `cargo install --bin`):

```toml
[tools]
"cargo:github.com/username/demo" = { version = "tag:v1.0.0", bin = "demo" }
```

### `crate`

Select the crate name to install when multiple are available (passed as
`cargo install --git=<repo> <crate>`):

```toml
[tools]
"cargo:github.com/username/demo" = { version = "tag:v1.0.0", crate = "demo" }
```

### `locked`

Use Cargo.lock (passes `cargo install --locked`) when building CLI. This is the default behavior,
pass `false` to disable:

```toml
[tools]
"cargo:github.com/username/demo" = { version = "latest", locked = false }
```
17 changes: 14 additions & 3 deletions src/backend/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,28 @@ impl Backend for CargoBackend {
};

let opts = tv.request.options();
if let Some(bin) = opts.get("bin") {
cmd = cmd.arg(format!("--bin={bin}"));

This comment has been minimized.

Copy link
@liskin

liskin Dec 15, 2024

Contributor

Until cargo-bins/cargo-binstall#1961 is done, we'll need to check for bin in is_binstall_enabled I'm afraid, otherwise it fails with:

mise ERROR cargo-binstall failed
error: unexpected argument '--bin' found
}
if !opts
.get("locked")
.is_some_and(|v| v.to_lowercase() == "false")
{
cmd = cmd.arg("--locked");
}
if let Some(features) = opts.get("features") {
cmd = cmd.arg(format!("--features={}", features));
cmd = cmd.arg(format!("--features={features}"));
}
if let Some(default_features) = opts.get("default-features") {
if default_features.to_lowercase() == "false" {
cmd = cmd.arg("--no-default-features");
}
}
if let Some(c) = opts.get("crate") {
cmd = cmd.arg(c);
}

cmd.arg("--locked")
.arg("--root")
cmd.arg("--root")
.arg(tv.install_path())
.with_pr(ctx.pr.as_ref())
.envs(ctx.ts.env_with_path(&config)?)
Expand Down
9 changes: 8 additions & 1 deletion src/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashSet;
use std::ffi::{OsStr, OsString};
use std::fmt::{Display, Formatter};
use std::fmt::{Debug, Display, Formatter};
use std::io::{BufRead, BufReader, Write};
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus, Stdio};
Expand Down Expand Up @@ -442,6 +442,13 @@ impl Display for CmdLineRunner<'_> {
}
}

impl Debug for CmdLineRunner<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let args = self.get_args().join(" ");
write!(f, "{} {args}", self.get_program())
}
}

enum ChildProcessOutput {
Stdout(String),
Stderr(String),
Expand Down

0 comments on commit 9169070

Please sign in to comment.