-
-
Notifications
You must be signed in to change notification settings - Fork 644
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
Add [cli.alias]
config section for command line alias support.
#13228
Conversation
Signed-off-by: Andreas Stenius <andreas.stenius@svenskaspel.se> # Rust tests and lints will be skipped. Delete if not intended. [ci skip-rust] # Building wheels and fs_util will be skipped. Delete if not intended. [ci skip-build-wheels]
First of all, this is just a wonderful idea. Thank you! Secondly, have you given thought to how you might deal with new goals introduced in Pants that clash with an alias that's already defined? (my first thoughts are this should error, but I haven't thought much about it) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is SO cool! 🎉
This is a good question. You could clash even on an existing goal, right? I guess this is a "there be dragons" situation, and we document that you have to be careful... |
I suspect erroring completely would be better behaviour, unless explicitly silenced – better to know that something's wrong and fix it, than having weird and hard-to-debug weirdnesses that nobody else can make sense of |
Yeah, I guess we should: A) Validate that aliases don't contain slashes or dashes (so they cannot collide with an option or a spec) |
[GLOBAL.alias]
config section for command line alias support.[CLI.alias]
config section for command line alias support.
[ci skip-rust] [ci skip-build-wheels]
# Rust tests and lints will be skipped. Delete if not intended. [ci skip-rust] # Building wheels and fs_util will be skipped. Delete if not intended. [ci skip-build-wheels]
# Rust tests and lints will be skipped. Delete if not intended. [ci skip-rust] # Building wheels and fs_util will be skipped. Delete if not intended. [ci skip-build-wheels]
# Rust tests and lints will be skipped. Delete if not intended. [ci skip-rust] # Building wheels and fs_util will be skipped. Delete if not intended. [ci skip-build-wheels]
# Rust tests and lints will be skipped. Delete if not intended. [ci skip-rust] # Building wheels and fs_util will be skipped. Delete if not intended. [ci skip-build-wheels]
[CLI.alias]
config section for command line alias support.[cli.alias]
config section for command line alias support.
# Rust tests and lints will be skipped. Delete if not intended. [ci skip-rust] # Building wheels and fs_util will be skipped. Delete if not intended. [ci skip-build-wheels]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Users are going to absolutely love this! This is definitely worth a blog post.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Amazing 🎉
@@ -31,7 +32,7 @@ | |||
|
|||
|
|||
# Subsystems used outside of any rule. | |||
_GLOBAL_SUBSYSTEMS: Set[Type[Subsystem]] = {GlobalOptions, Changed} | |||
_GLOBAL_SUBSYSTEMS: Set[Type[Subsystem]] = {GlobalOptions, Changed, CliOptions} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe, but I don't recall why. If anything, I think that there are a lot of potential usecases for breaking out our bootstrap and global options into more global subsystems.
# Rust tests and lints will be skipped. Delete if not intended. [ci skip-rust] # Building wheels and fs_util will be skipped. Delete if not intended. [ci skip-build-wheels]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea: thanks!
pants.toml
Outdated
|
||
[cli.alias] | ||
all-changed = "--changed-since=HEAD --changed-dependees=transitive" | ||
green = "fmt lint check" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's hold off on adding this one... it has bikeshed potential due to the ordering of the commands.
@@ -31,7 +32,7 @@ | |||
|
|||
|
|||
# Subsystems used outside of any rule. | |||
_GLOBAL_SUBSYSTEMS: Set[Type[Subsystem]] = {GlobalOptions, Changed} | |||
_GLOBAL_SUBSYSTEMS: Set[Type[Subsystem]] = {GlobalOptions, Changed, CliOptions} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe, but I don't recall why. If anything, I think that there are a lot of potential usecases for breaking out our bootstrap and global options into more global subsystems.
# Finally, we expand any aliases and re-populates the bootstrap args, in case there was | ||
# any from an alias. | ||
alias = CliAlias.from_dict(post_bootstrap_config.get("cli", "alias", dict, {})) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's possible that this will break the rust client when aliases are used, since it also does options parsing: #11922 ... but maybe only if bootstrap options are defined in the alias? Probably not a blocker.
I have made some headway on porting options parsing to fully consuming that backend, but haven't been able to make a push to land it yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that #21695 ports the CLI aliasing feature to Rust, so it will be available to the rust client.
flags = set() | ||
short_flags = set() | ||
|
||
def capture_the_flags(*args: str, **kwargs) -> None: | ||
for arg in args: | ||
flags.add(arg) | ||
if len(arg) == 2: | ||
short_flags.add(arg) | ||
elif kwargs.get("type") == bool: | ||
flags.add(f"--no-{arg[2:]}") | ||
|
||
GlobalOptions.register_bootstrap_options(capture_the_flags) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This registration will now run each time this method is called: should consider moving it into a dataclass to be used for both calls in OptionsBootstrapper.create
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just as a refactoring, to get the method out of the OptionsBootstrapper
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: I commented on specific lines in here.
Mostly for performance reasons. _get_bootstrap_args
is called twice, and so will re-register the bootstrap options twice, which will render silenced deprecation warnings twice, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, but the alias may expand to a bootstrap arg, so we need to visit them again.. will look a bit closer at this..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure: but look at the specific section I commented on: it's only the portion that captures the flags and short-flags. So that part can be reused I think?
This isn't a huge deal: don't worry too much about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it's fine, and I see my confusion now, the args in the closure is shadowing the args from the method. Will fix.
I.e. I came to the wrong conclusion for my last comment.
src/python/pants/option/alias.py
Outdated
"Notice: this option must be placed in a config file (e.g. `pants.toml`) to have " | ||
"any effect." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the implementation, it's not clear to me that this is true (although it would be pretty pointless to define aliases on the CLI): they'll be ignored during the bootstrap pass, then loaded from the bootstrap args, which includes CLI args?
It also looks like these should work in pantsrc
files, so maybe worth mentioning that: users are likely to want to define their own aliases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with the pantsrc
, mentioning it too.
The aliases are loaded from post_bootstrap_config
which is a Config.load()
, with only config files and bootstrap options, so any options for the cli
subsystem from a command line flag won't ever be in there, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, you're right. Should be fine... can possibly move where it is constructed later if it ends up mattering, but it likely won't.
[ci skip-rust] [ci skip-build-wheels]
# Rust tests and lints will be skipped. Delete if not intended. [ci skip-rust] # Building wheels and fs_util will be skipped. Delete if not intended. [ci skip-build-wheels]
There's a TODO for a typing issue with |
I'll address any remaining comments in a follow up PR, if there are any. |
…tsbuild#13228) This allows you to run pants with commands that are easier to remember, even if there are long or many options involved. The following two invocations are equivalent, given the pants configuration below: ``` $ ./pants all-changed fmt $ ./pants --changed-since=HEAD --changed-dependees=transitive fmt ``` `pants.toml`: ``` [cli.alias] all-changed = "--changed-since=HEAD --changed-dependees=transitive" ``` # Rust tests and lints will be skipped. Delete if not intended. [ci skip-rust] # Building wheels and fs_util will be skipped. Delete if not intended. [ci skip-build-wheels]
Pre-work for user friendly ad-hoc script execution support.
slack thread
Included in this PR is an example alias for running
pyupgrade
, as well asgreen
andall-changed
aliases:Example use to run on the docker backend:
By adding this to
pants.toml
:So this is generally useful already on its own.
I have not run any benchmarks to see how big impact this will have on pants startup times.
UPDATED
Added checks for alias names, so they can not be confused with potential options or dir specs. Also verifies that an alias does not shadow an existing goal/subsystem. Thanks @chrisjrn for pointing that out.
Added debug logging of alias expansion, to aid troubleshooting. I haven't looked, but if there is a
verbose
flag, perhaps that log could be made an info level when verbose==true.Also supports nesting aliases. This will be useful to build up aliases in incremental steps, while still keeping them DRY.
Such as this example: