Skip to content

open/save: give a clear error suggesting def when the from/to converter is an alias - #19205

Merged
fdncred merged 3 commits into
nushell:mainfrom
itsF4LCON:fix/18725-alias-open-save
Oct 13, 2026
Merged

fdncred merged 3 commits into
nushell:mainfrom
itsF4LCON:fix/18725-alias-open-save

Conversation

@itsF4LCON

@itsF4LCON itsF4LCON commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Description

open and save pick a converter by name at runtime: open file.jsonc looks up from jsonc, and save file.jsonc looks up to jsonc. When that name is an alias, for example alias "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:

Error: nu::shell::nushell_failed_spanned
  × Nushell failed: Can't run alias directly. Unwrap it first.
  help: This shouldn't happen. Please 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 def instead. The suggestion reuses the text the alias wraps, so the alias's flags are kept:

Error: nu::shell::error

  x Can't open file with alias `from jsonc`
   ,-[source:1:38]
 1 | alias "from jsonc" = from json; open deno.jsonc
   :                                      ^^^^^|^^^^
   :                                           `-- `from jsonc` is an alias, not a command
   `----
  help: `open` can only use a command as a converter. Define one with `def`
        instead: `def 'from jsonc' [] { from json }`

save gives 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 plain from alias, and one with --noheaders to check that the suggested def keeps the flag.
  • commands::save::save_with_a_to_alias_suggests_def: alias "to jsonc" = to json --raw suggests def 'to jsonc' [] { to json --raw } and doesn't create the file.

cargo fmt --all, cargo nuclippy and typos are clean.

User-facing changes (Release notes)

open and save no 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 as alias "from jsonc" = from json. They now explain that the converter must be a command and suggest the matching def, for example def 'from jsonc' [] { from json }.

Additional notes

Fixes #18725


Reply to fdncred (comment on the PR):

Done. Switched to option 2: open/save now give a normal error when the converter is an alias and suggest the equivalent def (e.g. def 'from jsonc' [] { from json }). I updated the description and tests too.

`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
Copilot AI balanced review requested due to automatic review settings October 10, 2026 19:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@fdncred

fdncred commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

I don't understand why we need this when we can just do def 'from jsonc' [] { from json }.

@itsF4LCON

Copy link
Copy Markdown
Contributor Author

I don't understand why we need this when we can just do def 'from jsonc' [] { from json }.

Fair question. def does work, and I read your comments on #18725 about not wanting aliases to match custom commands.

What I was mainly trying to fix is that hitting this today ends in NushellFailed ("Can't run alias directly. Unwrap it first"). That's the variant for impossible states, so it tells the user "This shouldn't happen. Please file an issue." That's how #18725 got filed.

I see two ways to close it:

  1. Keep this PR, so open/save run the aliased from/to call. You mentioned the from jsonc case could probably work.
  2. Don't support aliases here. Instead, open/save reject an alias converter with a normal error that suggests def 'from jsonc' [] { from json }.

I'm happy to switch the PR to option 2 if that fits better with where you want aliases to go.

@fdncred

fdncred commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

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".
@itsF4LCON itsF4LCON changed the title 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 Oct 11, 2026
@itsF4LCON

Copy link
Copy Markdown
Contributor Author

Ya, let's do number 2 here with the error message about defs. Thanks.

Done. Switched to option 2: open/save now give a normal error when the converter is an alias and suggest the equivalent def (e.g. def 'from jsonc' [] { from json }). I updated the description and tests too.

@fdncred fdncred left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 High severity · 1 Medium severity · 3 Low severity

Open (5)

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!(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

High severity The suggested 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_reached

save 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Low severity The alias check and error are copied between 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."

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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() {
"" => "...",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Low severity The empty-body fallback can't be reached

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Low severity 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.
@itsF4LCON

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review! All five are addressed in 20e2010:

  • Shadowing alias: when the alias wraps the command it's named after (alias "from json" = from json --objects), the help now shows the explicit pipeline instead of a def that would call itself: open --raw data.json | from json --objects, and ... | to json --raw | save out.json for save. Other aliases keep the def suggestion.
  • http get: transform_response_using_content_type now checks for an alias the same way, so it reports Can't convert the response with alias from json`` instead of the internal error.
  • Duplication: the check and wording live in one function, alias_converter_error (formats/alias_converter.rs), used by open, save and http.
  • Unreachable fallback: the "..." arm is gone.
  • save's label: convert_to_extension takes path.span, so the label sits under the file name, as with open.

New tests: open_with_a_shadowing_from_alias_suggests_a_pipeline, save_with_a_shadowing_to_alias_labels_the_file (checks the label covers out.json) and http_get_with_a_from_alias_reports_it (mockito). cargo fmt, clippy with the CI flags on nu-command, and typos pass.

@fdncred

fdncred commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

please respond to each review comment so we can be sure nothing is missed

@itsF4LCON

Copy link
Copy Markdown
Contributor Author

On the http get point from the review summary (it had no inline thread): fixed in 20e2010. transform_response_using_content_type now checks whether the from <ext> it found is an alias before running it, and reports Can't convert the response with alias from json`` through the same alias_converter_error, with `http ... --raw | from json --objects` as the explicit form. The new test `http_get_with_a_from_alias_reports_it` covers it with a mockito server.

@fdncred fdncred added A:error-unhelpful The error message you observe is not helpful to identify the problem A:error-handling How errors in externals/nu code are caught or handled programmatically (see also unhelpful-error) notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes labels Oct 13, 2026
@fdncred

fdncred commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Thanks

@fdncred
fdncred merged commit b23af9f into nushell:main Oct 13, 2026
11 checks passed
@github-actions github-actions Bot added this to the 1.0 milestone Oct 13, 2026
@hustcer hustcer modified the milestones: 1.0, v0.117.0 Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A:error-handling How errors in externals/nu code are caught or handled programmatically (see also unhelpful-error) A:error-unhelpful The error message you observe is not helpful to identify the problem notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nushell failed: Can't run alias directly. Unwrap it first.

4 participants