Skip to content

Commit cc3c4c1

Browse files
committed
give a hard error upon redefining a built-in command
previously, aliases to built-in commands were silently ignored. this matches git's behavior, but seems unhelpful, especially if the user doesn't know that a command with that name already exists. give a hard error rather than silently ignoring it.
1 parent 5b517b5 commit cc3c4c1

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
* The minimum supported Rust version (MSRV) is now 1.76.0.
1515

16+
* Attempting to alias a built-in command now gives a hard error, rather than being silently ignored.
17+
1618
### New features
1719

1820
* Templates now support logical operators: `||`, `&&`, `!`

cli/src/cli_util.rs

+9
Original file line numberDiff line numberDiff line change
@@ -2699,6 +2699,7 @@ fn resolve_aliases(
26992699
}
27002700
}
27012701
}
2702+
27022703
let mut resolved_aliases = HashSet::new();
27032704
let mut real_commands = HashSet::new();
27042705
for command in app.get_subcommands() {
@@ -2707,6 +2708,14 @@ fn resolve_aliases(
27072708
real_commands.insert(alias.to_string());
27082709
}
27092710
}
2711+
for alias in aliases_map.keys() {
2712+
if real_commands.contains(alias) {
2713+
return Err(user_error(format!(
2714+
"Cannot define an alias that overrides the built-in command '{alias}'"
2715+
)));
2716+
}
2717+
}
2718+
27102719
loop {
27112720
let app_clone = app.clone().allow_external_subcommands(true);
27122721
let matches = app_clone.try_get_matches_from(&string_args).ok();

cli/tests/test_alias.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,10 @@ fn test_alias_cannot_override_builtin() {
164164
let repo_path = test_env.env_root().join("repo");
165165

166166
test_env.add_config(r#"aliases.log = ["rebase"]"#);
167-
// Alias should be ignored
168-
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-r", "root()"]);
169-
insta::assert_snapshot!(stdout, @r###"
170-
◉ zzzzzzzz root() 00000000
167+
// Alias should give a hard error
168+
let stderr = test_env.jj_cmd_failure(&repo_path, &["log", "-r", "root()"]);
169+
insta::assert_snapshot!(stderr, @r###"
170+
Error: Cannot define an alias that overrides the built-in command 'log'
171171
"###);
172172
}
173173

0 commit comments

Comments
 (0)