From ad125abf6ae620e87985c8f44772a87c7e63a327 Mon Sep 17 00:00:00 2001 From: Ayush Singh Date: Sat, 8 Jul 2023 09:48:42 +0100 Subject: [PATCH] fixes `which` showing aliases as built-in nushell commands (#9580) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixes #8577 # Description Currently, using `which` on an alias describes it as a nushell built-in command: ```bash > alias foo = print "foo!" > which ls foo --all ╭───┬─────┬──────────────────────────┬──────────╮ │ # │ arg │ path │ built-in │ ├───┼─────┼──────────────────────────┼──────────┤ │ 0 │ ls │ Nushell built-in command │ true │ │ 1 │ ls │ /bin/ls │ false │ │ 2 │ foo │ Nushell built-in command │ true │ ╰───┴─────┴──────────────────────────┴──────────╯ ``` This PR fixes the behaviour above to the following: ```bash > alias foo = print "foo!" > which ls foo --all ╭───┬─────┬──────────────────────────┬──────────╮ │ # │ arg │ path │ built-in │ ├───┼─────┼──────────────────────────┼──────────┤ │ 0 │ ls │ Nushell built-in command │ true │ │ 1 │ ls │ /bin/ls │ false │ │ 2 │ foo │ Nushell alias │ false │ ╰───┴─────┴──────────────────────────┴──────────╯ ``` # User-Facing Changes Passing in an alias to `which` will no longer return `Nushell built-in command`, `true` for `path` and `built-in` respectively. # Tests + Formatting # After Submitting --- crates/nu-command/src/system/which_.rs | 2 ++ crates/nu-command/tests/commands/which.rs | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/crates/nu-command/src/system/which_.rs b/crates/nu-command/src/system/which_.rs index 62154cf229..3e89a9896d 100644 --- a/crates/nu-command/src/system/which_.rs +++ b/crates/nu-command/src/system/which_.rs @@ -77,6 +77,8 @@ fn get_entry_in_commands(engine_state: &EngineState, name: &str, span: Span) -> if let Some(decl_id) = engine_state.find_decl(name.as_bytes(), &[]) { let (msg, is_builtin) = if engine_state.get_decl(decl_id).is_custom_command() { ("Nushell custom command", false) + } else if engine_state.get_decl(decl_id).is_alias() { + ("Nushell alias", false) } else { ("Nushell built-in command", true) }; diff --git a/crates/nu-command/tests/commands/which.rs b/crates/nu-command/tests/commands/which.rs index 75557d9beb..b9f02e8b63 100644 --- a/crates/nu-command/tests/commands/which.rs +++ b/crates/nu-command/tests/commands/which.rs @@ -21,6 +21,19 @@ fn which_alias_ls() { assert_eq!(actual.out, "Nushell alias: ls -a"); } +#[test] +fn which_custom_alias() { + let actual = nu!( + cwd: ".", + r#"alias foo = print "foo!"; which foo | to nuon"# + ); + + assert_eq!( + actual.out, + "[[arg, path, built-in]; [foo, \"Nushell alias\", false]]" + ); +} + #[test] fn which_def_ls() { let actual = nu!(