Skip to content

make external signatures do not enforce flag checking - #7461

Closed
WindSoilder wants to merge 25 commits into
nushell:mainfrom
WindSoilder:extern_flags
Closed

WindSoilder wants to merge 25 commits into
nushell:mainfrom
WindSoilder:extern_flags

Conversation

@WindSoilder

@WindSoilder WindSoilder commented Dec 13, 2022 •

Copy link
Copy Markdown
Contributor

Description

The pr is based on #6567 made by @merelymyself

Fixes #4659. Fixes #6020. Kind of Fixes #5103. Fixes #5294. Fixes #6124

User-Facing Changes

❯ export extern "git" []

# don't check for unknown flag
❯ git --version
git version 2.37.3
❯ git -v
git version 2.37.3

# still check for variable
❯ let x = "--version"; git $x
❯ git $y
Error: nu::parser::variable_not_found (link)

  × Variable not found.
   ╭─[entry #36:1:1]
 1 │ git $y
   ·     ─┬
   ·      ╰── variable not found
   ╰────

# For some complicated usage
❯ extern tar [-x -v]
❯ touch a b c

❯ tar -cvzf tmp.tar.gz a b c
a a
a b
a c

❯ tar -tzf tmp.tar.gz
a
b
c

About the change

It's a little big change, because auto-completion makes some assumption on parse function, which is incompatible to fall-through logic. In detail, based on user input, it relys on parse function returns error on UnknownFlag, for short flag, it may returns an extra -a Flag, for long flag, it always add long flag argument even if it doesn't match.

So I rename from original parse_internal_call to parse_realcomp_internal_call, which is used for auto-completion, and nushell's custom command. For known extern, the main parsing logic is in parse_known_extern_call parse_internal_call_loosely.

Then I make a copy of parse function called parse_for_completion, the different between them is just the way to parse custom command and known external. In parse_for_completion, it will always go to parse_realcomp_internal_call. In normal parse, it may goes to parse_known_external_call parse_internal_call_loosely or parse_realcomp_internal_call, based on Signature.is_known_external Signature.allows_unknown_flags attribute.

## One restriction for unknown flag Because it's unknown, we don't know if it's taking value or not, so some flags which take values will not likely work

Take the following extern as example:

❯ export extern "git commit" []

We can't pass an unknown flag which takes values, which means the following doesn't work:

git commit -m "first commit"

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.

@sholderbach sholderbach added the notes:breaking-changes Noted in Breaking Changes section label Dec 13, 2022
@WindSoilder
WindSoilder marked this pull request as draft December 14, 2022 21:51
@WindSoilder

Copy link
Copy Markdown
Contributor Author

Make as draft in case of restriction, need to find a way to pass unknown flags which take values

@kubouch

kubouch commented Dec 14, 2022 •

Copy link
Copy Markdown
Contributor

Yeah, the git commit -m "first commit" is a bit surprising. It probably parses -m as a flag and "first commit" as a positional. I think export extern "git commit" [...rest] should solve it but it's not exactly correct.

@WindSoilder

Copy link
Copy Markdown
Contributor Author

I'm thinking about parsing it as internal call, if we get unknown flag error, then parse it as external call

But it breaks our builtin auto completion :-( Not sure if we can change internal completor to make it right

@kubouch

kubouch commented Dec 14, 2022

Copy link
Copy Markdown
Contributor

I think we might be going too fancy here. An idea: extern "git commit" [...rest] would fail to parse git commit -m "first commit" because of unknown -m. But you could write extern "git commit" [...-rest] (or some other syntax) which would take everything, even flags, as positionals.

More complex example: With an external signature defined as extern "git commit" [--all (-a), -C: string, ...-rest] a command line git commit --all -b --foo -DSOMETHING=123 -- bar baz -C spam - would be parsed as git commit with --all true and -C set to spam, and everything else would be collected as positionals: -b, --foo, -DSOMETHING=123, --, bar, baz, and -. By typing ...-rest (or some better syntax we come up with), we're basically saying: "Parse only the parameters defined in the signature, everything else are string positionals, don't bother with them.

This one would work for exec as well. Its signature would be defined as exec [...-rest] so it would basically pass everything along as strings to the spawned external. It would work also in custom commands, you'd just have to keep in mind that the flags captured in ...-rest are strings, not real flags: You can pass them to externals but not to other Nushell commands. Also one disadvantage is that it breaks the original ordering of the signature parameters, but in order to be able to preserve that, we'd need to parse the signature into some internal variable or something.

@WindSoilder

Copy link
Copy Markdown
Contributor Author

Thanks for your detailed instruction :-) There is a little question left: are [...-rest] and [...rest] mutually exclusive?

@kubouch

kubouch commented Dec 14, 2022

Copy link
Copy Markdown
Contributor

Yeah, I'd say so. I'm not sure what would be the output if you had both.

@kubouch

kubouch commented Dec 15, 2022

Copy link
Copy Markdown
Contributor

Actually, after chatting about it a bit, we realized the ...-rest is not necessary. It would be enabled automatically for all extern signatures and the exec command. So basically what I described: Parsing all unknown parameters as string positionals, but without the need to specify ...-rest.

@WindSoilder

WindSoilder commented Dec 16, 2022 •

Copy link
Copy Markdown
Contributor Author

Parsing all unknown parameters as string positionals, but without the need to specify ...-rest.

Hi, should we enable this feature only if we specify ...rest? I can imagine some user might need something like restrict mode, that's how current nushell works

@kubouch

kubouch commented Dec 17, 2022

Copy link
Copy Markdown
Contributor

Let's just enable it by default when the signature is marked as a known external without requiring ...rest. Requiring it would make ...rest work differently in extern and def signatures and we'd better use ...-rest. But I think it's OK to do it without any special syntax markup. At least for now, we'll see how it works.

@WindSoilder
WindSoilder marked this pull request as ready for review December 17, 2022 12:35
@WindSoilder

Copy link
Copy Markdown
Contributor Author

Sorry for the relative large diff, I've updated About the change session in the description.

@kubouch

kubouch commented Dec 17, 2022

Copy link
Copy Markdown
Contributor

Cool, I'll look into it when I have the time.

In the meanwhile, could you also enable it for the exec command? You might want to make a new method on the Signature struct, similar to extra_usage(), that sets the is_known_external to true and call the method in the exec.rs. Also, is_known_external might not be the most suitable name for the flag because exec is not an external. It might be better called allows_unknown_flags or something like that.

@WindSoilder

Copy link
Copy Markdown
Contributor Author

Sure, I'll take a look how exec works at first

@WindSoilder

WindSoilder commented Dec 18, 2022 •

Copy link
Copy Markdown
Contributor Author

exec supports unknown flags fow now :-D

But here is a limitation: we can't pass --help(-h) flag as argument, because it's unknown flag to exec, and I don't think we need to remove help message from the command.

So here is the result:

❯ exec nu --testbin nonu aaa
aaa

Still show help message of exec command, but I think it's fine.


❯ exec nu -h
Execute a command, replacing the current process.

Currently supported only on Unix-based systems.

Usage:
  > exec <command>
...

…me from parse_known_extern_call to parse_internal_call_loosely
@kubouch

kubouch commented Dec 18, 2022 •

Copy link
Copy Markdown
Contributor

I tried the PR and it does not seem to be working correctly. On Linux, running

exec echo foo -b -as --abc -Dxmy=AKOO -- bar -

returns

foo -b -a -s --abc -D -x -m -y -= -A -K -O -O -- bar -

while the output of

^exec echo foo -b -as --abc -Dxmy=AKOO -- bar -

is

foo -b -as --abc -Dxmy=AKOO -- bar -

To me, the logic seems too complicated. I think the the unknown flags shouldn't be parsed as flags at all and re-creating them like you do also seems too fragile. An example how the logic could be is:

  • Go through all the parameters (flags and positionals)
    • If the parameter matches the signature (for example, sig.get_long_flag(&long_name) returns Some(flag)): Follow the existing code.
    • If the parameter does not match the signature (for example, sig.get_long_flag(&long_name) returns None): Parse the single span as a value of a string type and push it to the call as a positional

You should be able to weave this into the existing code without creating a new parse_internal_call() function because doing so creates a lot of duplicate code that needs to be maintained.

@WindSoilder

WindSoilder commented Dec 19, 2022 •

Copy link
Copy Markdown
Contributor Author

Thanks for your feedback! It's a really good test case :-D

If the parameter does not match the signature (for example, sig.get_long_flag(&long_name) returns None): Parse the single span as a value of a string type and push it to the call as a positional
You should be able to weave this into the existing code without creating a new parse_internal_call() function because doing so creates a lot of duplicate code that needs to be maintained.

I've tried to do with this way firstly, but sadly found that it breaks auto-completion for known extern, because push it to the call as a positional doesn't match the auto-completion's requirement....And I have no idea how to fix it in the auto-completion side...

And if I make it into current parse_internal_call, it needs to have a flag to indicate if it's parse for auto-completion or not, then it'd make the function's implementation itself too complicated

@WindSoilder

WindSoilder commented Dec 19, 2022 •

Copy link
Copy Markdown
Contributor Author

the issue of exec should be fixed :-)

Removes some duplicate code in parse_internal_call_loosely and parse_realcomp_internal_call, so we can review the difference between them easier.

Sorry for that I have no idea how to make them into one parse_internal_call :-(

@kubouch

kubouch commented Dec 19, 2022

Copy link
Copy Markdown
Contributor

Could you give an example how the completion failed with the method I described?

I'm still hesitant landing it because of the added complexity to the parser for only a relatively minor change. I'll try some experiments myself and see if I can make it simpler.

@WindSoilder

WindSoilder commented Dec 19, 2022 •

Copy link
Copy Markdown
Contributor Author

If the parameter does not match the signature (for example, sig.get_long_flag(&long_name) returns None): Parse the single span as a value of a string type and push it to the call as a positional

Oh, sorry, after apply this change, I think completion works for current design again...I'll revert some changes back again and see if it's actually working

@WindSoilder
WindSoilder marked this pull request as draft December 19, 2022 10:22
@kubouch

kubouch commented Dec 19, 2022

Copy link
Copy Markdown
Contributor

@WindSoilder I managed to find a relatively simple solution, see the linked PR above. It mostly works, except the exec needs a little tuning.

@WindSoilder

Copy link
Copy Markdown
Contributor Author

Thanks! I'll take a look there

@WindSoilder

Copy link
Copy Markdown
Contributor Author

Closing this infavor of #7527

@WindSoilder
WindSoilder deleted the extern_flags branch December 24, 2022 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:breaking-changes Noted in Breaking Changes section

Projects

None yet

3 participants