Skip to content
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

positional-arguments does not work with set shell? #1559

Closed
Cologler opened this issue Mar 1, 2023 · 13 comments
Closed

positional-arguments does not work with set shell? #1559

Cologler opened this issue Mar 1, 2023 · 13 comments

Comments

@Cologler
Copy link

Cologler commented Mar 1, 2023

For

set positional-arguments

run args:
    @echo $@

Execute just run 2023 print:

2023

For

set positional-arguments
set shell := ["pwsh", "-c"]

run args:
    @echo $@

Execute just run 2023 print:

$@
run
2023

For

set positional-arguments
set shell := ["nu", "-c"]

run args:
    @echo $@

Execute just run 2023 print:

Error: nu::parser::variable_not_found (link)

  × Variable not found.
   ╭─[source:1:1]
 1 │ echo $@
   ·      ─┬
   ·       ╰── variable not found
   ╰────

error: Recipe `run` failed on line 5 with exit code 1

Test on just 1.13.0.

@runeimp
Copy link

runeimp commented Mar 4, 2023

This seems to be a duplicate of #1554

But also Error: nu::parser::variable_not_found (link) looks like Nu shell instead PowerShell. Is your default shell Nu? If so this might be another part of the same issue of simply using set positional-arguments with PowerShell Core. What operating system are you using?

@Cologler
Copy link
Author

Cologler commented Mar 4, 2023

This seems to be a duplicate of #1554

But also Error: nu::parser::variable_not_found (link) looks like Nu shell instead PowerShell. Is your default shell Nu? If so this might be another part of the same issue of simply using set positional-arguments with PowerShell Core. What operating system are you using?

My mistake. I updated the last example.

The arguments should expand by just.exe, whatever my default shell and OS are.

@runeimp
Copy link

runeimp commented Mar 6, 2023

Thanks for the clarification. So the pwsh example is definitely the same as I was experiencing in #1554. Nu shell is similar to PowerShell in that they are both object based shells instead of string based like the majority of other shells in existence. So might have something to do with a possible change in Just v1.13.0. As this was not an issue I'd seen prior using Just with PowerShell. Though it may have existed prior and I simply hadn't tried using set positional-arguments with PowerShell before. I seem to remember a little oddness with Nu shell but It's been a while since I played with that Shell and Just. Any thoughts @casey ?

@cspotcode
Copy link

cspotcode commented May 2, 2023

Pretty sure nu does not allow access to positionals via $@, $args, et al, you have to wrap your script in a function declaration: https://www.nushell.sh/book/scripts.html#parameterizing-scripts

@runeimp
Copy link

runeimp commented May 3, 2023

@cspotcode As I understand it using set positional-arguments is an enhancement that preprocesses the $@, $1, $2, etc. tokens before handoff to the the shell. If set positional-arguments was not enabled then those tokens would absolutely have to be handled by the shell itself.

@cspotcode
Copy link

No, set positional-arguments appends them to the argv array when invoking the shell, it doesn't do any preprocessing.

My writeup in #1592 goes into greater detail.

But you can experiment with the following justfile to understand the exact argv array that is being received by the target executable.

# Try running `just default hello world` and carefully inspect the output
set shell := ["python", "-c", "import sys; print(sys.argv)"]
set positional-arguments
default *args:
    foo bar baz "$@" "$1"
$ just default hello world
foo bar baz "$@" "$1"
['-c', 'foo bar baz "$@" "$1"', 'default', 'hello', 'world']

This proves that just is not preprocessing, it is passing the characters $ and @ to the shell within the argv array of strings.

@runeimp
Copy link

runeimp commented May 3, 2023

Ah, that's a bummer ☹️

@Cologler
Copy link
Author

Cologler commented Oct 1, 2024

Work on PowerShell Core 7.4 with -cwa options:

set positional-arguments
set shell := ["pwsh", "-cwa"]

run *args:
    python -c "import sys; print(sys.argv)" @($args[1..($args.Count - 1)])

Execute just run 1 '2 3' print:

['-c', '1', '2 3']

@casey
Copy link
Owner

casey commented Oct 1, 2024

@Cologler That's super nice! This has been a longstanding limitation. Is -cwa a new option? If -cwa is widely available, we should probably update the readme to suggest using -CommandWithArgs instead of `-Command.

@Cologler
Copy link
Author

Cologler commented Oct 1, 2024

@casey CommandWithArgs is an experimental feature added in 7.4.

Source: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_pwsh?view=powershell-7.4

@casey
Copy link
Owner

casey commented Oct 1, 2024

Gotcha. I added a note to the readme in #2404. I think this is "fixed" and can be closed, since there's a good workaround.

@casey casey closed this as completed Oct 1, 2024
@Cologler
Copy link
Author

Cologler commented Oct 1, 2024

@casey Unlike $@ in sh, powershell must use @($args[1..($args.Count - 1)]) to skip the first argument pass from just.

@casey
Copy link
Owner

casey commented Oct 1, 2024

@Cologler Thanks! Updating the example in the readme to be more complete in #2405.

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

No branches or pull requests

4 participants