Skip to content
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

Fix command name lookup for known externals #7830

Merged
merged 1 commit into from Jan 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion crates/nu-parser/src/known_external.rs
Expand Up @@ -51,7 +51,15 @@ impl Command for KnownExternal {

let mut extern_call = Call::new(head_span);

let extern_name = engine_state.get_decl(call.decl_id).name();
let extern_name = if let Some(name_bytes) = engine_state.find_decl_name(call.decl_id, &[]) {
String::from_utf8_lossy(name_bytes)
} else {
return Err(ShellError::NushellFailedSpanned(
"known external name not found".to_string(),
"could not find name for this command".to_string(),
call.head,
));
};

let extern_name: Vec<_> = extern_name.split(' ').collect();

Expand Down
18 changes: 18 additions & 0 deletions crates/nu-protocol/src/engine/engine_state.rs
Expand Up @@ -585,6 +585,24 @@ impl EngineState {
None
}

pub fn find_decl_name(&self, decl_id: DeclId, removed_overlays: &[Vec<u8>]) -> Option<&[u8]> {
let mut visibility: Visibility = Visibility::new();

for overlay_frame in self.active_overlays(removed_overlays).iter().rev() {
visibility.append(&overlay_frame.visibility);

if visibility.is_decl_id_visible(&decl_id) {
for ((name, _), id) in overlay_frame.decls.iter() {
if id == &decl_id {
return Some(name);
}
}
}
}

None
}

pub fn find_alias(&self, name: &[u8], removed_overlays: &[Vec<u8>]) -> Option<AliasId> {
let mut visibility: Visibility = Visibility::new();

Expand Down
31 changes: 31 additions & 0 deletions src/tests/test_known_external.rs
Expand Up @@ -76,3 +76,34 @@ fn known_external_misc_values() -> TestResult {
"abc a b c",
)
}

/// GitHub issue #7822
#[test]
fn known_external_subcommand_from_module() -> TestResult {
run_test_contains(
r#"
module cargo {
export extern check []
};
use cargo;
cargo check -h
"#,
"cargo check",
)
}

/// GitHub issue #7822
#[test]
fn known_external_aliased_subcommand_from_module() -> TestResult {
run_test_contains(
r#"
module cargo {
export extern check []
};
use cargo;
alias cc = cargo check;
cc -h
"#,
"cargo check",
)
}