Skip to content

Allow cargo aliases to shadow existing commands with flags #9768

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

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 6 additions & 0 deletions src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
@@ -201,6 +201,12 @@ fn expand_aliases(
.get_matches_from_safe(alias)?;

let (new_cmd, _) = new_args.subcommand();
if new_cmd == cmd {
// Don't reexpand if the alias is a default-flags alias
// eg. fmt = "fmt --verbose"
return Ok((new_args, global_args));
}

already_expanded.push(cmd.to_string());
if already_expanded.contains(&new_cmd.to_string()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

happy to switch to a LinkedHashMap if we want cargo to support O(n*log(n)) resolution of dependent aliases instead of O(n^2). I figured n is probably small, and linked_hash_map crate is a new dependency - but happy to switch it if preferred.

Copy link
Contributor

Choose a reason for hiding this comment

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

Why LinkedHashMap over indexmap? But yes n is probably to small for it to matter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

wasn't familiar with indexmap - looks great to me.
It still appears that it would be an additional dependency to cargo (not sure if you want to pull it in)

Copy link
Member

Choose a reason for hiding this comment

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

I think this is fine for now, yeah, and if it's an issue there's a number of other data structures to use here instead.

// Crash if the aliases are corecursive / unresolvable
6 changes: 6 additions & 0 deletions src/doc/src/reference/config.md
Original file line number Diff line number Diff line change
@@ -289,6 +289,12 @@ r = "run"

Aliases are not allowed to redefine existing built-in commands.

Aliases are allowed to redefine non-built-in commands with default flags
```toml
[alias]
fmt = "fmt --verbose"
```

#### `[build]`

The `[build]` table controls build-time operations and compiler settings.
8 changes: 2 additions & 6 deletions tests/testsuite/cargo_alias_config.rs
Original file line number Diff line number Diff line change
@@ -99,16 +99,12 @@ fn default_args_alias() {

p.cargo("echo")
.env("PATH", &path)
.with_status(101)
.with_stderr("error: alias echo has unresolvable recursive definition: echo -> echo")
.with_stdout("echo --flag1 --flag2")
.run();

p.cargo("test-1")
.env("PATH", &path)
.with_status(101)
.with_stderr(
"error: alias test-1 has unresolvable recursive definition: test-1 -> echo -> echo",
)
.with_stdout("echo --flag1 --flag2")
.run();

// Builtins are not expanded by rule