Skip to content

Add support for aliases#17229

Merged
tgamblin merged 5 commits intospack:developfrom
michaelkuhn:aliases
Nov 6, 2023
Merged

Add support for aliases#17229
tgamblin merged 5 commits intospack:developfrom
michaelkuhn:aliases

Conversation

@michaelkuhn
Copy link
Copy Markdown
Member

@michaelkuhn michaelkuhn commented Jun 24, 2020

Aliases can be used to define new commands. For instance, sp: spec -I will define a new command sp that will execute spec with the -I argument. Aliases cannot override existing commands.

This is still WIP and is missing some things, including schema adaptions and porting the old aliases to the new system. It already works in general. For convenience, there is an example alias sp (which I will remove for the final version). I would just like to get some feedback on whether this is something of interest.

@adamjstewart @tgamblin @alalazo

Closes #2705

@adamjstewart
Copy link
Copy Markdown
Member

Also see #2705

@adamjstewart adamjstewart added the feature A feature is missing in Spack label Jun 25, 2020
@michaelkuhn
Copy link
Copy Markdown
Member Author

I have read that PR and think the main issue back then was that standard commands could be overridden. This PR does not allow that (like Git).

@michaelkuhn michaelkuhn force-pushed the aliases branch 3 times, most recently from 119510a to 32ebbc0 Compare June 25, 2020 18:25
@michaelkuhn michaelkuhn mentioned this pull request Jun 25, 2020
3 tasks
@michaelkuhn
Copy link
Copy Markdown
Member Author

Looks like I'm hitting some strange test failures due to using spack.config.get in the main function. I have opened #17262 about it.

@tgamblin tgamblin mentioned this pull request Jul 9, 2020
@tgamblin
Copy link
Copy Markdown
Member

tgamblin commented Jul 9, 2020

I like it! Go for it!

@michaelkuhn michaelkuhn marked this pull request as ready for review August 21, 2020 11:10
@michaelkuhn
Copy link
Copy Markdown
Member Author

This is ready from my side. If you have any more comments, please let me know.

@michaelkuhn
Copy link
Copy Markdown
Member Author

@tgamblin @adamjstewart ping

@adamjstewart
Copy link
Copy Markdown
Member

I don't usually merge core changes, but I do agree that this is a nice feature to add. Maybe @becker33 or @scheibelp can review?

@michaelkuhn
Copy link
Copy Markdown
Member Author

@tgamblin @becker33 @scheibelp ping

I have fixed a conflict, so this should be ready again.

@michaelkuhn
Copy link
Copy Markdown
Member Author

@tgamblin @becker33 @scheibelp ping ping

@michaelkuhn
Copy link
Copy Markdown
Member Author

This has been finished for some time from my point of view, I would really appreciate a review.

@adamjstewart
Copy link
Copy Markdown
Member

How does this work for subsubcommands, like spack compiler find? Does it only support aliases of subcommands? Do we want support for subsubcommands? Is that even possible?

@michaelkuhn
Copy link
Copy Markdown
Member Author

michaelkuhn commented Jun 6, 2021

It currently only supports creating aliases on a subcommand level. You can, however, map them so subsubcommands. For instance, the following works: foo: compiler list

Edit: Looking at the code again, it might be possible to support them on a subsubcommand level. I'm not sure whether that's a good idea, though.

@scheibelp

This comment has been minimized.

@scheibelp scheibelp self-assigned this Jun 7, 2021
@tgamblin
Copy link
Copy Markdown
Member

tgamblin commented Nov 5, 2023

@psakievich:

In this case perhaps we want a warning?

It is probably helpful sometimes to spit out a warning but that’s as far as I’d go. Something like “alias ‘foo’ would run ‘bar’ which is not a spack command”, but again it could be annoying going back in time — if you alias a recent spack command then use an older version of spack.

@michaelkuhn? @adamjstewart?

@tgamblin
Copy link
Copy Markdown
Member

tgamblin commented Nov 5, 2023

@michaelkuhn

Do we want to make this work or should we just not support aliases with a space?

I am having a hard time thinking of when I’d actually want a space… and we have no commands with spaces. Let’s disallow it. I can’t decide if it should just warn or if it should throw an error. Leaning towards error.

@adamjstewart
Copy link
Copy Markdown
Member

@michaelkuhn

Do we want to make this work or should we just not support aliases with a space?

I am having a hard time thinking of when I’d actually want a space… and we have no commands with spaces. Let’s disallow it. I can’t decide if it should just warn or if it should throw an error. Leaning towards error.

What about something like:

config:
  aliases:
    comp a: compiler add
    comp f: compiler find
    ...

@tgamblin
Copy link
Copy Markdown
Member

tgamblin commented Nov 5, 2023

@adamjstewart I think we should save that for later, for subcommand aliases. Right now that won't be parsed the way I think you're expecting -- you'd have to quote it like spack "comp a".

@tgamblin
Copy link
Copy Markdown
Member

tgamblin commented Nov 5, 2023

So I guess, given that, how about a warning for aliases with spaces in them -- so that old versions of spack aren't too prickly if you end up with a configured subcommand alias for some future spack version.

@michaelkuhn
Copy link
Copy Markdown
Member Author

michaelkuhn commented Nov 5, 2023

I've pushed a new commit that will print a warning for aliases containing a space. And FYI, #40895 implements the "did you mean" functionality.

Edit: Added one more commit to emit a warning for aliases attempting to override built-in commands.

@psakievich
Copy link
Copy Markdown
Contributor

@adamjstewart I think we should save that for later, for subcommand aliases. Right now that won't be parsed the way I think you're expecting -- you'd have to quote it like spack "comp a".

Or aliases of aliases... 😉

michaelkuhn and others added 5 commits November 6, 2023 12:42
Aliases can be used to define new commands. For instance, `sp: spec -I`
will define a new command `sp` that will execute `spec` with the `-I`
argument. Aliases cannot override existing commands.
The test makes sure that aliases cannot override Spack commands. This
also fixes a small bug in SpackCommand and delays the `add_command` call
until it is actually necessary; otherwise, it will complain about
aliases it does not know about.
Print a warning for aliases containing a space.
@tgamblin tgamblin merged commit 5074b7e into spack:develop Nov 6, 2023
@adamjstewart
Copy link
Copy Markdown
Member

3 years later. Glad we finally got this in!

michaelkuhn added a commit to michaelkuhn/spack that referenced this pull request Nov 20, 2023
The shell wrapper currently does not preserve spaces in commands. For
example, `bin/spack 'foo bar'` has `'foo bar'` interpreted as its
command, while `spack 'foo bar'` turns this into `bin/spack foo bar`.
This is more of a theoretical problem, but while we are at it,
incorporate a few more suggestions given by ShellCheck.

See: spack#17229 (comment)
gabrielctn pushed a commit to gabrielctn/spack that referenced this pull request Nov 24, 2023
Add a new config section: `config:aliases`, which is a dictionary mapping aliases
to commands.

For instance:


```yaml
config:
    aliases:
        sp: spec -I
```

will define a new command `sp` that will execute `spec` with the `-I`
argument. 

Aliases cannot override existing commands, and this is ensured with a test.

We cannot currently alias subcommands. Spack will warn about any aliases
containing a space, but will not error, which leaves room for subcommand
aliases in the future.

---------

Co-authored-by: Todd Gamblin <[email protected]>
mtaillefumier pushed a commit to mtaillefumier/spack that referenced this pull request Dec 14, 2023
Add a new config section: `config:aliases`, which is a dictionary mapping aliases
to commands.

For instance:


```yaml
config:
    aliases:
        sp: spec -I
```

will define a new command `sp` that will execute `spec` with the `-I`
argument. 

Aliases cannot override existing commands, and this is ensured with a test.

We cannot currently alias subcommands. Spack will warn about any aliases
containing a space, but will not error, which leaves room for subcommand
aliases in the future.

---------

Co-authored-by: Todd Gamblin <[email protected]>
@haampie haampie mentioned this pull request Mar 7, 2024
3 tasks
michaelkuhn added a commit to michaelkuhn/spack that referenced this pull request Apr 2, 2024
The shell wrapper currently does not preserve spaces in commands. For
example, `bin/spack 'foo bar'` has `'foo bar'` interpreted as its
command, while `spack 'foo bar'` turns this into `bin/spack foo bar`.
This is more of a theoretical problem, but while we are at it,
incorporate a few more suggestions given by ShellCheck.

See: spack#17229 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commands core PR affects Spack core functionality defaults documentation Improvements or additions to documentation feature A feature is missing in Spack tests General test capability(ies)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants