twpayne / chezmoi

Manage your dotfiles across multiple diverse machines, securely.
https://www.chezmoi.io/
MIT License
12.85k stars 477 forks source link

`powershell` called to run `ps1` script even though I changed interpreter to `pwsh` #2589

Closed alexvy86 closed 1 year ago

alexvy86 commented 1 year ago

Describe the bug

It seems that powershell is being used to run one of my ps1 scripts even though I changed the interpreter for ps1 files to pwsh. I think this is actually powershell being called from within pwsh because the error my script is reporting is this one (Get-FileHash : The term 'Get-FileHash' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.), and this comment from that linked issue shows that it occurs when calling powershell from a pwsh instance.

To reproduce

Haven't had a chance to try to reproduce it in other machines, but the output included below should show why I think this is what's happening. I can reproduce it every time on my machine though, so can help to troubleshoot.

Expected behavior

powershell is never called anywehre, if my interpreter for ps1 files is set to pwsh.

Output of command with the --verbose flag

My config file uses pwsh.exe as interpreter for ps1 files:

PS C:\Users\alejandrovi\.local\share\chezmoi> chezmoi cat-config   
edit:
  command: "code"
  args: ["-r", "--wait"]
"interpreters.ps1":
  command: "pwsh.exe"
  args: ["-NoLogo"]

sourceDir: "C:/Users/alejandrovi/.local/share/chezmoi/windows"

data:
  codespaces: false
  win_tools_path: "C:/Users/alejandrovi/bin"

During chezmoi apply --debug, I see powershell as the first entry in args right after the (failed) execution of my script:

<more logs>
<my script runs and fails>
2022-11-21T11:20:34-06:00 INF Run args=["powershell","-NoLogo","C:\\Users\\ALEJAN~1\\AppData\\Local\\Temp\\583280716.04-install-scoop-apps.ps1"] dir=C:/Users/alejandrovi duration=5.4679569s env=["ALLUSERSPROFILE=C:\\ProgramData", [...]
<more logs>

Output of chezmoi doctor

PS C:\Users\alejandrovi\.local\share\chezmoi> chezmoi doctor    
RESULT    CHECK                MESSAGE
ok        version              v2.27.1, commit b6039e787dfffe970fec4f9165ec9ebe1b3ceaa6, built at 2022-11-13T20:30:23Z, built by goreleaser
ok        latest-version       v2.27.1
ok        os-arch              windows/amd64
ok        systeminfo           Microsoft Windows 11 Enterprise (10.0.22621 N/A Build 22621)
ok        go-version           go1.19.3 (gc)
ok        executable           ~/bin/chezmoi.exe
ok        upgrade-method       replace-executable
ok        config-file          ~/.config/chezmoi/chezmoi.yaml, last modified 2022-11-21T11:20:09-06:00
ok        source-dir           ~/.local/share/chezmoi/windows is a directory
ok        suspicious-entries   no suspicious entries
warning   working-tree         ~/.local/share/chezmoi is a git working tree (dirty)
ok        dest-dir             ~ is a directory
ok        cd-command           found C:/WINDOWS/system32/cmd.exe
ok        cd-args              'C:\\WINDOWS\\system32\\cmd.exe'
info      diff-command         not set
ok        edit-command         found ~/AppData/Local/Programs/Microsoft VS Code/bin/code.cmd
ok        edit-args            code -r --wait
ok        git-command          found C:/Program Files/Git/cmd/git.exe, version 2.38.1
warning   merge-command        vimdiff not found in $PATH
ok        shell-command        found C:/WINDOWS/system32/cmd.exe
ok        shell-args           'C:\\WINDOWS\\system32\\cmd.exe'
info      age-command          age not found in $PATH
info      gpg-command          gpg not found in $PATH
info      pinentry-command     not set
info      1password-command    op not found in $PATH
info      bitwarden-command    bw not found in $PATH
info      gopass-command       gopass not found in $PATH
info      keepassxc-command    keepassxc-cli not found in $PATH
info      keepassxc-db         not set
info      keeper-command       keeper not found in $PATH
info      lastpass-command     lpass not found in $PATH
info      pass-command         pass not found in $PATH
info      passhole-command     ph not found in $PATH
info      vault-command        vault not found in $PATH
info      secret-command       not set

Additional context

Running the script myself in pwsh succeeds.

bradenhilton commented 1 year ago

What is the output with the following script?

run_hello.ps1:

Write-Output "Hello from $(Split-Path -Path ([System.Environment]::GetCommandLineArgs()[0]) -Leaf)"
alexvy86 commented 1 year ago

powershell indeed:

PS C:\Users\alejandrovi\.local\share\chezmoi> cat .\windows\.chezmoiscripts\run_hello.ps1                             
Write-Output "Hello from $(Split-Path -Path ([System.Environment]::GetCommandLineArgs()[0]) -Leaf)"

PS C:\Users\alejandrovi\.local\share\chezmoi> chezmoi apply
Hello from powershell

I skimmed this repo to see where the issue might be but didn't find anything obvious. Disclaimer: I'm not familiar enough with Go to be able to easily trace calls just by browsing the repo in Github :).

alexvy86 commented 1 year ago

For completeness, my pwsh and powershell versions:

PS C:\Users\alejandrovi\.local\share\chezmoi> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.3.0
PSEdition                      Core
GitCommitId                    7.3.0
OS                             Microsoft Windows 10.0.22621
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

PS C:\Users\alejandrovi\.local\share\chezmoi> powershell
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows

PS C:\Users\alejandrovi\.local\share\chezmoi> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.22621.608
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.22621.608
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
bradenhilton commented 1 year ago

The default .ps1 interpreter is set to powershell, so I don't think the config setting is applying properly.

I don't use YAML for my config, so I'm just guessing here, but you might need to separate the keys/properties. Something like

interpreters:
    ps1:
        command: pwsh.exe
        args: -NoLogo
alexvy86 commented 1 year ago

Thanks @bradenhilton ! That was indeed the problem. I remember having several issues when I first tried to set up this section of the config file and thought I had left it working correctly, but evidently not. I'll make a PR to make the docs around this more explicit for the YAML case.