Skip to content

Fix Support for PowerShell under Windows with VIM 8+#1326

Merged
junegunn merged 1 commit into
junegunn:masterfrom
tiamat18:master
May 17, 2026
Merged

Fix Support for PowerShell under Windows with VIM 8+#1326
junegunn merged 1 commit into
junegunn:masterfrom
tiamat18:master

Conversation

@tiamat18
Copy link
Copy Markdown
Contributor

The following 'vim-plug' commands would fail and/or display errors when executed on by VIM 8+ on a Windows platform with the 'shell' option set to 'powershell' or 'pwsh':

  • PlugInstall
  • PlugUpdate
  • PlugClean
  • PlugStatus
  • PlugDiff

There were two causes for these errors:

  • A bug in VIM itself (resolved by patch 9.2.6) with how compound PowerShell commands are handled by the 'system()' vimscript function.
  • A bug in the 's:vim8' branch of the private 'vim-plug' function 's:spawn()' where the actual command to be executed was escaped for 'cmd.exe' but the 'cd' prefix added by 's:with_cd()" was escaped for PowerShell.

Describe the details of your PR ...

@HaaiHenkie
Copy link
Copy Markdown

Nice fix — making the shell explicit in s:with_cd is definitely the right approach. It solved all my vim 8+ problems with PowerShell under Windows. I hope it will be implemented soon.

For completeness and consistency, consider passing {'shell': 'cmd.exe'} to s:with_cd() in s:bang() as well, though I don't think it is really necessary. This is because I don't know whether this function is ever used under Windows and, whether it will work anyway, taking into account the FIXME remark. Since in s:bang() the command is executed via a batch file under Windows, the command shell will be cmd.exe even when executed under PowerShell. So to prevent PowerShell type commands in a batch file, I would suggest the following change on line 1000:

let cmd = a:0 ? s:with_cd(a:cmd, a:1, {'shell': s:is_win ? 'cmd.exe' : &shell}) : a:cmd

@tiamat18
Copy link
Copy Markdown
Contributor Author

I have made your suggested update, but I'm not sure how to test it. Would you know? Or are you ok with accepting the change without testing?

@HaaiHenkie
Copy link
Copy Markdown

Sorry, there seems to be some merge problem now. Has the master changed in the meantime?

I do not know how to test this. I had fixed the same issues as you did, but discovered your solutions are much better. Since the API of s:with_cd() changed, it seemed necessary to check the use in s:bang() as well. I find it very difficult to trace back the exact conditions under which s:with_cd is used here. Maybe the developer knows. But it is clear that it leads to errors when used in a PowerShell context.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes vim-plug command failures (PlugInstall/Update/Clean/Status/Diff) on Windows when &shell is set to PowerShell under Vim 8+. The issue had two roots: a bug in Vim's system() for compound PowerShell commands (resolved by patch-9.2.6), and a mismatch in s:spawn's vim8 branch where the actual command was escaped for cmd.exe while s:with_cd's cd prefix was escaped for PowerShell.

Changes:

  • Refactored s:with_cd to accept an options dict (shell, script) instead of a single script boolean, allowing callers to specify which shell the resulting compound command will run in.
  • Added a & { ... } block wrapper around PowerShell compound commands when the running Vim lacks patch-9.2.6, to work around the system() bug.
  • Updated callers (s:bang, s:spawn vim8 branch, s:system) to pass the correct shell for the context (e.g. cmd.exe on Windows for s:bang since it goes through a .bat file, and for s:spawn since it invokes cmd /s /c).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@junegunn junegunn added windows shell:powershell User's shell set to powershell.exe (Windows) or pwsh (Unix) labels May 15, 2026
@HaaiHenkie
Copy link
Copy Markdown

HaaiHenkie commented May 15, 2026

It occurred to me that in the function s:system() there is also the possibility that the command is run in a batchfile, just after s:with_cd is called. This is de PR code from line 2381 onwards:

    if a:0 > 0
      let cmd = s:with_cd(cmd, a:1, {'script': type(a:cmd) != s:TYPE.list})
    endif
    if s:is_win && type(a:cmd) != s:TYPE.list
      let [batchfile, cmd] = s:batchfile(cmd)
    endif

To prevent that a PowerShell style command would get into a batch file, you could for example do it like this:

    if a:0 > 0 && s:is_win && type(a:cmd) != s:TYPE.list
      let cmd = s:with_cd(cmd, a:1, {'shell': 'cmd.exe'})
    elseif a:0 > 0
      let cmd = s:with_cd(cmd, a:1, {'script': type(a:cmd) != s:TYPE.list})
    endif
    if s:is_win && type(a:cmd) != s:TYPE.list
      let [batchfile, cmd] = s:batchfile(cmd)
    endif

The following 'vim-plug' commands would fail and/or display errors
when executed by VIM 8+ on a Windows platform with the 'shell' option
set to 'powershell' or 'pwsh':
  - PlugInstall
  - PlugUpdate
  - PlugClean
  - PlugStatus
  - PlugDiff

There were two causes for these errors:
  - A bug in VIM itself (resolved by patch 9.2.6) with how compound
    PowerShell commands are handled by the 'system()' vimscript
    function.
  - A bug in the 's:vim8' branch of the private 'vim-plug' function
    's:spawn()' where the actual command to be executed was escaped
    for 'cmd.exe' but the 'cd' prefix added by 's:with_cd()" was escaped
    for PowerShell.
@tiamat18
Copy link
Copy Markdown
Contributor Author

I have fixed the issue you identified with s:system() and verified the fix by running the vimscript code echo <SNR>X_system('echo yes && echo no', 'C:\') in VIM under PowerShell on Windows.

I also verified my previous s:bang() fix with the vimscript code echo <SNR>X_bang('echo yes && echo no', 'C:\').

Are you still have merge problems? I have been amending my commit in place and force-pushing the changes. So you may need to do a fresh checkout of my branch rather than simply pulling it.

@junegunn
Copy link
Copy Markdown
Owner

I'm not a Windows user so I cannot reproduce the problem and verify the fix, but the patch itself looks solid. Let me know if you think it's ready for merge.

@tiamat18
Copy link
Copy Markdown
Contributor Author

The patch is ready for merging. Thanks.

@junegunn junegunn merged commit d7db1b6 into junegunn:master May 17, 2026
1 check passed
@junegunn
Copy link
Copy Markdown
Owner

Merged, thanks!

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

Labels

shell:powershell User's shell set to powershell.exe (Windows) or pwsh (Unix) windows

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants