Skip to content

Re-implement aliases#8123

Merged
sophiajt merged 15 commits intonushell:mainfrom
kubouch:new-alias
Feb 27, 2023
Merged

Re-implement aliases#8123
sophiajt merged 15 commits intonushell:mainfrom
kubouch:new-alias

Conversation

@kubouch
Copy link
Contributor

@kubouch kubouch commented Feb 19, 2023

Description

This PR adds an alternative alias implementation. Old aliases still work but you need to use old-alias instead of alias.

Instead of replacing spans in the original code and re-parsing, which proved to be extremely error-prone and a constant source of panics, the new implementation creates a new command that references the old command. Consider the new alias defined as alias ll = ls -l. The parser creates a new command called ll and remembers that it is actually a ls command called with the -l flag. Then, when the parser sees the ll command, it will translate it to ls -l and passes to it any parameters that were passed to the call to ll. It works quite similar to how known externals defined with extern are implemented.

The new alias implementation should work the same way as the old aliases, including exporting from modules, referencing both known and unknown externals. It seems to preserve custom completions and pipeline metadata. It is quite robust in most cases but there are some rough edges (see later).

Fixes #7648 #8026 #7512 #5780 #7754

No effect: #8122 (we might revisit the completions code after this PR)

Should use custom command instead: #6048

User-Facing Changes

Since aliases are now basically commands, it has some new implications:

  1. alias spam = "spam" (requires command call)
    • workaround: use alias spam = echo "spam"
  2. def foo [] { 'foo' }; alias foo = ls -l (foo defined more than once)
    • workaround: use different name (commands also have this limitation)
  3. alias ls = (ls | sort-by type name -i)
    • workaround: Use custom command. The common issue with this is that it is currently not easy to pass flags through custom commands and command referencing itself will lead to stack overflow. Both of these issues are meant to be addressed.
  4. TODO: Help messages, which command, $nu.scope.aliases, etc.
    • Should we treat the aliases as commands or should they be separated from regular commands?
  5. Needs better error message and syntax highlight for recursed alias (alias f = f)
  6. Can't create alias with the same name as existing command (alias ls = ls -a)
    • Might be possible to add support for it (not 100% sure)
  7. Standalone alias doesn't list aliases anymore
  8. Can't alias parser keywords (e.g., stuff like alias ou = overlay use won't work)
    • TODO: Needs a better error message when attempting to do so

Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

  • cargo fmt --all -- --check to check standard code formatting (cargo fmt --all applies these changes)
  • cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect to check that you're using the standard code style
  • cargo test --workspace to check that all tests pass

After Submitting

If your PR had any user-facing changes, update the documentation after the PR is merged, if necessary. This will help us keep the docs up to date.

@fdncred
Copy link
Contributor

fdncred commented Feb 19, 2023

I'm going to play with this a bit. Nice POC!

@rgwood
Copy link
Contributor

rgwood commented Feb 19, 2023

Wow! Awesome idea; thank you for putting this together. Fewer panics in this area will be a big UX improvement.

@codecov
Copy link

codecov bot commented Feb 19, 2023

Codecov Report

Merging #8123 (fb50335) into main (42f0b55) will increase coverage by 13.05%.
The diff coverage is 53.27%.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             main    #8123       +/-   ##
===========================================
+ Coverage   54.77%   67.83%   +13.05%     
===========================================
  Files         613      620        +7     
  Lines       98494    99758     +1264     
===========================================
+ Hits        53954    67670    +13716     
+ Misses      44540    32088    -12452     
Impacted Files Coverage Δ
crates/nu-engine/src/eval.rs 75.00% <0.00%> (+22.04%) ⬆️
crates/nu-protocol/src/alias.rs 13.69% <13.69%> (ø)
crates/nu-parser/src/parse_keywords.rs 65.49% <54.16%> (+25.26%) ⬆️
crates/nu-protocol/src/engine/command.rs 93.54% <76.92%> (+2.80%) ⬆️
...ates/nu-command/src/deprecated/export_old_alias.rs 79.54% <79.54%> (ø)
crates/nu-command/src/deprecated/old_alias.rs 82.35% <82.35%> (ø)
crates/nu-parser/src/parser.rs 82.55% <92.79%> (+6.78%) ⬆️
crates/nu-cmd-lang/src/core_commands/alias.rs 100.00% <100.00%> (ø)
...ates/nu-cmd-lang/src/core_commands/export_alias.rs 79.54% <100.00%> (ø)
crates/nu-command/src/default_context.rs 99.78% <100.00%> (+<0.01%) ⬆️
... and 261 more

@kubouch kubouch force-pushed the new-alias branch 5 times, most recently from d898eac to 6612aad Compare February 23, 2023 16:17
@kubouch kubouch force-pushed the new-alias branch 6 times, most recently from d709674 to d28b470 Compare February 24, 2023 21:51
@kubouch kubouch marked this pull request as ready for review February 25, 2023 19:35
@fdncred
Copy link
Contributor

fdncred commented Feb 25, 2023

@kubouch Related to:

  1. Seems reasonable.
  2. Seems reasonable. I always wondered which one takes precedence. Now we won't have to wonder.
  3. I'm looking forward to this fix when/if it ever comes.
  4. I'd like to see them in either (a) $nu.scope.aliases, (b) help commands | where command_type == alias (c) help aliases, (d) help commands where category == alias . So, I think it's better to have them categorized somehow.
  5. However we end up doing it in 4, seems like something should be able to be done here maybe?
  6. It'd kind of a bummer that we can't alias other parts of the language. Maybe there's hope to do this in the future?

I'm definitely up for landing this and seeing how these edge cases manifest themselves. It's kind of hard to tell by reading these if there are deal-breakers or not.

@sophiajt sophiajt merged commit a3f817d into nushell:main Feb 27, 2023
@sophiajt
Copy link
Contributor

Let's go ahead and land, and we'll use the time between now and the next release to polish it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

nushell panics by alias

4 participants