Repository navigation
open/save: give a clear error suggesting def when the from/to converter is an alias - #19205
Conversation
`open` and `save` look up `from <ext>` / `to <ext>` by name at runtime. When that name is an alias they called the alias itself, which fails with the internal "Can't run alias directly" error. Evaluate the call the alias stands for instead, keeping its arguments. Fixes nushell#18725
|
I don't understand why we need this when we can just do |
Fair question. What I was mainly trying to fix is that hitting this today ends in I see two ways to close it:
I'm happy to switch the PR to option 2 if that fits better with where you want aliases to go. |
|
Ya, let's do number 2 here with the error message about defs. Thanks. |
Aliases aren't supported as from/to converters. Return a normal error
that suggests the matching def, e.g. def 'from jsonc' [] { from json },
instead of failing with "Can't run alias directly".
open/save: run from/to aliases instead of failing with "Can't run alias directly"open/save: give a clear error suggesting def when the from/to converter is an alias
Done. Switched to option 2: |
There was a problem hiding this comment.
Review overview
🟡 Changes recommended
The new error replaces the internal failure from #18725, but its def suggestion doesn't work when the alias shadows the command it wraps, and http get still hits the internal error.
Findings: 1
Open (5)
The suggesteddefbreaks when the alias shadows the command it wrapshttp getstill fails with the internal error. client.rs:1026 looks upfrom {ext}the same way and calls.run()on whatever it finds, so an alias there still reachesAlias::run. On 4450bda,alias "from json" = from json --objects; http get http://127.0.0.1:18765/x.jsongivesNushell failed: Can't run alias directly. Unwrap it first.It's the same bug, so the same check fits there. The alias check and error are copied betweenopenandsave The empty-body fallback can't be reachedsavelabels thesavekeyword,openlabels the file
Notes from my clanker, checked on 4450bda. Built the branch, ran the two new tests (both pass), and ran each repro above on the branch binary.
| format!("`{}` is an alias, not a command", alias.name), | ||
| arg_span, | ||
| ) | ||
| .with_help(format!( |
There was a problem hiding this comment.
def breaks when the alias shadows the command it wraps
alias X = X --flag is the usual way to change a converter's defaults, and for that form the suggestion doesn't work. Inside a def body the name already refers to the new def, so the inner call never reaches the built-in. On 4450bda:
alias "from json" = from json --objects; open x.json
# help: ... Define one with `def` instead: `def 'from json' [] { from json --objects }`
def 'from json' [] { from json --objects }; open x.json
# Error: nu::parser::unknown_flag The `from json` command doesn't have flag `objects`.
def 'to csv' [] { to csv }; [{a: 1}] | save out.csv
# Error: nu::shell::recursion_limit_reachedsave gives the same advice at save.rs:338. The tests use from foo → from csv and to jsonc → to json, so they don't cover this. When alias.command.as_ref().is_some_and(|cmd| cmd.name() == alias.name), no def can stand in for the alias, so the help could point at converting by hand instead, e.g. open --raw x.json | from json --objects or ... | to json --raw | save out.json. A test for that case would help too.
There was a problem hiding this comment.
Fixed in 20e2010. When the alias wraps the command it is named after (the wrapped call resolves to a decl with the alias's own name), the help no longer suggests a def, since that def would call itself. It shows the explicit pipeline instead: open --raw data.json | from json --objects for open, ... | to json --raw | save out.json for save. Other aliases keep the def suggestion. Covered by open_with_a_shadowing_from_alias_suggests_a_pipeline and save_with_a_shadowing_to_alias_labels_the_file.
| if let Some(decl_id) = engine_state.find_decl(format!("to {extension}").as_bytes(), &[]) { | ||
| let decl = engine_state.get_decl(decl_id); | ||
| if let Some(block_id) = decl.block_id() { | ||
| if let Some(alias) = decl.as_alias() { |
There was a problem hiding this comment.
open and save
This block and open.rs:221-241 differ only in the verb and the span. http get needs the same check (see the review body), which would make three copies. One function next to these, something like alias_converter_error(engine_state, alias, verb, span) -> ShellError, would keep the wording in one place. The repo's CLAUDE.md asks for this: "Make sure that all the changes are written in idiomatic Rust with minimal code duplication without adding a bunch of new helpers."
There was a problem hiding this comment.
Done in 20e2010. The check and wording now live in one function, alias_converter_error in crates/nu-command/src/formats/alias_converter.rs. open, save and the http response conversion all call it, each passing its own verb, label span and pipeline example.
| engine_state.get_span_contents(alias.wrapped_call.span), | ||
| ); | ||
| let body = match wrapped.trim() { | ||
| "" => "...", |
There was a problem hiding this comment.
parse_alias only builds an Alias when there's at least one span after = (parse_alias.rs:157, spans.len() >= split_id + 3), and the call parsed from those spans covers them, so wrapped.trim() is never empty. The "..." arm can go, here and at save.rs:329.
There was a problem hiding this comment.
Removed in 20e2010. You are right that parse_alias never builds an Alias with an empty expansion, so the "..." arm is gone from both places. The shared helper uses the trimmed expansion directly.
| GenericError::new( | ||
| format!("Can't save file with alias `{}`", alias.name), | ||
| format!("`{}` is an alias, not a command", alias.name), | ||
| span, |
There was a problem hiding this comment.
save labels the save keyword, open labels the file
open puts the label under the file path. Here span is call.head, so the label lands under save:
1 │ alias "to json" = to json --raw; {a: 1} | save out.json
· ──┬─
· ╰── `to json` is an alias, not a command
path.span is in scope in Save::run and could be passed to convert_to_extension for this error, so both commands point at the file whose extension picked the converter.
There was a problem hiding this comment.
Fixed in 20e2010. convert_to_extension now takes path.span and passes it to the error, so the label sits under the file name, as with open. save_with_a_shadowing_to_alias_labels_the_file checks that the labelled span covers out.json.
…a pipeline for shadowing aliases - One helper, alias_converter_error, builds the error for open, save and http. - http get looks up from <ext> the same way, so it reports an alias instead of failing with "Can't run alias directly". - An alias over the converter it shadows (alias "from json" = from json --objects) can't become a def of that name, so the help shows the explicit pipeline. - save labels the file path, as open does. - Drop the unreachable empty-expansion fallback.
|
Thanks for the thorough review! All five are addressed in 20e2010:
New tests: |
|
please respond to each review comment so we can be sure nothing is missed |
|
On the |
|
Thanks |



Description
openandsavepick a converter by name at runtime:open file.jsonclooks upfrom jsonc, andsave file.jsonclooks upto jsonc. When that name is an alias, for examplealias "from jsonc" = from json, both commands call the alias itself. Aliases only exist while parsing, so this ends in an internal error that asks the user to file an issue:As discussed in this PR, aliases aren't supported as converters. Both commands now check whether the converter they found is an alias, and if it is they return a normal error that suggests a
definstead. The suggestion reuses the text the alias wraps, so the alias's flags are kept:savegives the same error. It's raised before anything is written, so no file is created.Tests:
commands::open::open_with_a_from_alias_suggests_def: a plainfromalias, and one with--noheadersto check that the suggesteddefkeeps the flag.commands::save::save_with_a_to_alias_suggests_def:alias "to jsonc" = to json --rawsuggestsdef 'to jsonc' [] { to json --raw }and doesn't create the file.cargo fmt --all,cargo nuclippyandtyposare clean.User-facing changes (Release notes)
openandsaveno longer fail with an internal "Nushell failed: Can't run alias directly" error when the converter for a file's extension is an alias, such asalias "from jsonc" = from json. They now explain that the converter must be a command and suggest the matchingdef, for exampledef 'from jsonc' [] { from json }.Additional notes
Fixes #18725
Reply to fdncred (comment on the PR):
Done. Switched to option 2:
open/savenow give a normal error when the converter is an alias and suggest the equivalentdef(e.g.def 'from jsonc' [] { from json }). I updated the description and tests too.