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

No .ps1 extension added when pwsh.exe is started via third-party tool #2253

Closed
old-pigeon opened this issue Jul 15, 2024 · 6 comments · Fixed by #2256
Closed

No .ps1 extension added when pwsh.exe is started via third-party tool #2253

old-pigeon opened this issue Jul 15, 2024 · 6 comments · Fixed by #2256

Comments

@old-pigeon
Copy link

I am required to execute multiple commands running as administrator under Windows where the return value is ignored.

I use a third-party tool called gsudo.

justfile:

set windows-shell := ["pwsh", "-NoLogo", "-NoProfileLoadTime", "-NoProfile", "-Command"]

stop-drivers:
    #!gsudo pwsh
    sc.exe stop mydriver1
    sc.exe stop mydriver2
    sc.exe stop mydriver3

Output:

PS C:\> just stop-drivers
Processing -File 'C:\Users\Admin\AppData\Local\Temp\just-yQLBL2\stop-drivers' failed because the file does not have a '.ps1' extension. Specify a valid PowerShell script file name, and then try again.
error: Recipe `stop-drivers` failed with exit code 1

The same stuff "works" (it adds the .ps1 as required) when I remove the gsudo from the shebang line, but then my script-logic doesn't work anymore because it's not running as admin.

Conclusion: just seems to check if pwsh.exe is the executable to be started in the shebang and only then add .ps1.

Ideas for workarounds:

  • allow the user to manually specify the extension for the temp script
  • enhance the heuristic to detect PowerShell scripts
@casey
Copy link
Owner

casey commented Jul 15, 2024

I think both workarounds are reasonable. I lean towards adding an attribute which allows setting the shebang script extension:

[extension: '.ps1']
stop-drivers:
    #!gsudo pwsh

Extending the heuristic seems error prone, since there are probably a bunch of ways that you might indirectly invoke a powershell script, but detecting gsudo pswsh would be reasonable and not too hard.

@old-pigeon
Copy link
Author

I am not sure if this should be its own issue, but I'll post it here for now as I think it touches the same "logic" (maybe, sorry if not). When I want to execute stuff in a pwsh.exe shebang with arguments, I can't find a way of passing the arguments.

I tried these shebangs:

#!pwsh -NoLogo -NoProfileLoadTime -NoProfile -Command
#!pwsh -NoLogo -NoProfileLoadTime -NoProfile -Command -File
#!pwsh "-NoLogo -NoProfileLoadTime -NoProfile -Command"
#!pwsh "-NoLogo -NoProfileLoadTime -NoProfile -Command" -File

All of them end up with an error message similar to this:

The argument '-NoLogo -NoProfileLoadTime -NoProfile -Command' is not recognized as the name of a script file. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

@laniakea64
Copy link
Contributor

I lean towards adding an attribute which allows setting the shebang script extension:

[extension: '.ps1']
stop-drivers:
    #!gsudo pwsh

+1, also customizing shebang script extension has been brought up before - #1624

@casey
Copy link
Owner

casey commented Jul 15, 2024

It is done!

@old-pigeon
Copy link
Author

@casey awesome, you're so quick 😄

Is this also included in the fix (#2253 (comment)) or shall I create a new issue for it?

@casey
Copy link
Owner

casey commented Jul 16, 2024

You're most welcome! The issue you're running into is the way that the #! is split on windows. It's split into only two pieces at the first space, so everything after pwsh is a single argument, instead of multiple arguments. This is an oversight on my part, but probably something I can't change now, since it would break scripts which have a shebang line like this #!foo bar baz which are currently relying on the behavior that bar baz is passed as a single argument.

Shebang handling on Windows is super hacky, so I think the eventual fix will be #1479, which would let you do:

[script("pwsh", "-NoLogo", "-NoProfileLoadTime", "-NoProfile", "-Command")]
foo:

So you can dictate exactly how the arguments are split

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

Successfully merging a pull request may close this issue.

3 participants