Skip to content

Completion definition prevent valid command from executing  #6020

Closed

Description

@chtenb

Describe the bug

When I use the completion definitions found in nu_scripts/custom-completions/npm/npm-completions.nu, that prevents me from running perfectly valid commands.

E.g. if I run npm that that gives a parse error

❯ npm
Error: nu::parser::missing_positional (link)

  × Missing required positional argument.
   ╭─[entry #1:1:1]
 1 │ npm
   ·    ▲
   ·    ╰── missing command
   ╰────
  help: Usage: npm <command>

while that is actually a valid command.

It also prevents operations like which to produce helpful output

❯ which npm
╭───┬─────┬──────────────────────────┬──────────╮
│ # │ arg │           path           │ built-in │
├───┼─────┼──────────────────────────┼──────────┤
│ 0 │ npm │ Nushell built-in command │ true     │

How to reproduce

Use the npm completion script and run npm

Expected behavior

Apparently the syntax definition prevents nu from parsing the command correctly. I would have expected that this information would only be used for completion/help/info generation. If I have typed a command I would have expected nu to obey it.

Screenshots

No response

Configuration

key value
version 0.63.0
branch
commit_hash 06cf3fa
build_os windows-x86_64
build_target x86_64-pc-windows-msvc
rust_version rustc 1.61.0 (fe5b13d68 2022-05-18)
rust_channel stable-x86_64-pc-windows-msvc
cargo_version cargo 1.61.0 (a028ae42f 2022-04-29)
pkg_version 0.63.0
build_time 2022-05-25 01:15:47 +00:00
build_rust_channel release
features database, dataframe, default, trash, which, zip
installed_plugins

Additional context

No response

Activity

  1. fdncred commented on Jul 12, 2022

    @fdncred
    Contributor

    Agreed. Custom Completions need to be tweaked a bit. My way around this, for now, is to do ^npm.

  2. added
    category:bug
    A:completions
    on Jul 12, 2022
  3. Kangaxx-0 commented on Jul 13, 2022

    @Kangaxx-0
    Contributor

    Seems like a lot of users feel confused about this. The reason why the command failed is that the npm is being treated as pre-defined internal command instead of external during parsing. This brings us to a question → Regarding the custom commands, by default, should they be internal or external: My understanding is they are external, the reason is simple - They are not built-in

  4. fdncred commented on Jul 13, 2022

    @fdncred
    Contributor

    I think they are their own category as is_custom but you may be right that they could be treated as external. Also custom completions kind of play in this arena too but don't have their own category.

  5. Kangaxx-0 commented on Jul 15, 2022

    @Kangaxx-0
    Contributor

    I did take a look at the parser, seems like there is a bug or missing part with regard to the custom command. The export extern <command> is a KnownExternal(trait object of Command), but the parser just handle it as Internal. It should be ok to just add some conditional checks without big changes. For the completion, i will take a look at them too

  6. CAD97 commented on Jul 24, 2022

    @CAD97
    Contributor

    Catching incorrect usage is nice if you can verify that the nu definition is accurate and complete.

    It may make sense to default to permissive, but the strict behavior should be available.

    A simple syntax option to get a permissive extern is to have a ... parameter with the semantics of allowing whatever captured content.

  7. kubouch commented on Dec 19, 2022

    @kubouch
    Contributor

    This is actually not a Nushell problem, but just the completion definition was written such that it requires the positional argument instead of making it optional. Changing

      command: string@"nu-complete npm"
    

    to

      command?: string@"nu-complete npm"
    

    in the npm definition should solve the problem. I'll close this issue as it does not seem to show a problem in Nushell itself.

  8. chtenb commented on Dec 20, 2022

    @chtenb
    Author

    The npm was an example, this issue is about the general problem as indicated in the title: the fact that completion syntaxes will not always reflect the syntax of all (versions of) a certain tool correctly and how nushell deals with that. Perhaps this issue should stay open until the general problem is addressed in a satisfactory manner?

  9. kubouch commented on Dec 20, 2022

    @kubouch
    Contributor

    The issue is worded such that it sounds npm-specific. #7527 will be merged in after the 0.73 release, you're welcome to try it and see if it helps. Unfortunately, "commands are not parsed correctly" and "general problem is addressed" are very generic statements that are not actionable. If you still find some problems, feel free to formulate them concretely, what solutions you'd imagine and open a new issue.

  10. chtenb commented on Dec 20, 2022

    @chtenb
    Author

    The issue is worded such that it sounds npm-specific.

    I did not intend it to be, so my apologies if I didn't formulate unambiguously enough.

    Unfortunately, "commands are not parsed correctly" and "general problem is addressed" are very generic statements that are not actionable. If you still find some problems, feel free to formulate them concretely, what solutions you'd imagine and open a new issue.

    I was referring to my expectations stated in the OP:

    I would have expected that this information would only be used for completion/help/info generation.

    It seemed undesirable to me that a completion script, designed to help the user by providing completions, would also prevent commands from executing if they don't meet the syntax form as described in the completion script. I felt that a user should be able to execute whatever they want and not be held back by a completion script. I don't use completion scripts at all for this reason currently. I know about the ^ workaround but it's too annoying.

    #7527 will be merged in after the 0.73 release

    This actually sounds like it exactly addresses the issue I described, if it applies all completion scripts by default, which I think it does because it applies to all extern definitions.

  11. chtenb commented on Mar 4, 2023

    @chtenb
    Author

    Today I updated my nushell installation and checked this out. I think the behavior has improved a lot! Thanks for that.

    However, the example given in the OP still does not work:

    ❯ npm
    Error: nu::parser::missing_positional (link)
    
      × Missing required positional argument.
       ╭─[entry #4:1:1]
     1 │ npm
       ╰────
      help: Usage: npm <command>
    

    Is this something that can be fixed in the completion definition here?
    https://github.com/nushell/nu_scripts/blob/main/custom-completions/npm/npm-completions.nu

    For the record, if I pass something rubbish, the command correctly falls through to npm:

    ❯ npm hello
    Unknown command: "hello"
    
    To see a list of supported npm commands, run:
      npm help
    
  12. kubouch commented on Mar 4, 2023

    @kubouch
    Contributor

    The command should be optional: command?.

  13. chtenb commented on Mar 5, 2023

    @chtenb
    Author

    I will send a pull request to patch it. However, it does seem like the fall-through mechanism introduced in #7527 could or should be extended to cover this case?

  14. fdncred commented on Mar 5, 2023

    @fdncred
    Contributor

    Indeed. We need to fix the fall through. I would love to see that improved.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A:completionscategory:bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions