mirror of
https://github.com/neovim/neovim.git
synced 2025-09-06 19:38:20 +00:00
fix: make_filter_cmd for :! powershell #15913
Problem: Nvim fails to create tempfile "…/nvim6UJx04/7" when 'shell' is set to pwsh (PowerShell Core). This breaks filtered shell commands ":{range}!". With shell set to cmd, it works. Solution: PowerShell doesn't use "<" for stdin redirection. Instead, use "-RedirectStandardInput". Closes #15913
This commit is contained in:

committed by
Christian Clason

parent
790f6089e9
commit
175892fa37
@@ -5324,9 +5324,9 @@ A jump table for the options with a short description can be found at |Q_op|.
|
||||
unescaping, so to keep yourself sane use |:let-&| like shown above.
|
||||
*shell-powershell*
|
||||
To use PowerShell: >
|
||||
let &shell = has('win32') ? 'powershell' : 'pwsh'
|
||||
let &shell = executable('pwsh') ? 'pwsh' : 'powershell'
|
||||
let &shellcmdflag = '-NoLogo -NoProfile -ExecutionPolicy RemoteSigned -Command [Console]::InputEncoding=[Console]::OutputEncoding=[System.Text.Encoding]::UTF8;'
|
||||
let &shellredir = '2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode'
|
||||
let &shellredir = '-RedirectStandardOutput %s -NoNewWindow -Wait'
|
||||
let &shellpipe = '2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode'
|
||||
set shellquote= shellxquote=
|
||||
|
||||
|
@@ -1571,14 +1571,18 @@ char_u *make_filter_cmd(char_u *cmd, char_u *itmp, char_u *otmp)
|
||||
#else
|
||||
false;
|
||||
#endif
|
||||
bool is_pwsh = STRNCMP(invocation_path_tail(p_sh, NULL), "pwsh", 4) == 0
|
||||
|| STRNCMP(invocation_path_tail(p_sh, NULL), "powershell", 10) == 0;
|
||||
|
||||
size_t len = STRLEN(cmd) + 1; // At least enough space for cmd + NULL.
|
||||
|
||||
len += is_fish_shell ? sizeof("begin; " "; end") - 1
|
||||
: sizeof("(" ")") - 1;
|
||||
: is_pwsh ? STRLEN("Start-Process ")
|
||||
: sizeof("(" ")") - 1;
|
||||
|
||||
if (itmp != NULL) {
|
||||
len += STRLEN(itmp) + sizeof(" { " " < " " } ") - 1;
|
||||
len += is_pwsh ? STRLEN(itmp) + STRLEN(" -RedirectStandardInput ")
|
||||
: STRLEN(itmp) + sizeof(" { " " < " " } ") - 1;
|
||||
}
|
||||
if (otmp != NULL) {
|
||||
len += STRLEN(otmp) + STRLEN(p_srr) + 2; // two extra spaces (" "),
|
||||
@@ -1588,7 +1592,10 @@ char_u *make_filter_cmd(char_u *cmd, char_u *itmp, char_u *otmp)
|
||||
#if defined(UNIX)
|
||||
// Put delimiters around the command (for concatenated commands) when
|
||||
// redirecting input and/or output.
|
||||
if (itmp != NULL || otmp != NULL) {
|
||||
if (is_pwsh) {
|
||||
xstrlcpy(buf, "Start-Process ", len);
|
||||
xstrlcat(buf, (char *)cmd, len);
|
||||
} else if (itmp != NULL || otmp != NULL) {
|
||||
char *fmt = is_fish_shell ? "begin; %s; end"
|
||||
: "(%s)";
|
||||
vim_snprintf(buf, len, fmt, (char *)cmd);
|
||||
@@ -1597,13 +1604,22 @@ char_u *make_filter_cmd(char_u *cmd, char_u *itmp, char_u *otmp)
|
||||
}
|
||||
|
||||
if (itmp != NULL) {
|
||||
xstrlcat(buf, " < ", len - 1);
|
||||
if (is_pwsh) {
|
||||
xstrlcat(buf, " -RedirectStandardInput ", len - 1);
|
||||
} else {
|
||||
xstrlcat(buf, " < ", len - 1);
|
||||
}
|
||||
xstrlcat(buf, (const char *)itmp, len - 1);
|
||||
}
|
||||
#else
|
||||
// For shells that don't understand braces around commands, at least allow
|
||||
// the use of commands in a pipe.
|
||||
xstrlcpy(buf, (char *)cmd, len);
|
||||
if (is_pwsh) {
|
||||
xstrlcpy(buf, "Start-Process ", len);
|
||||
xstrlcat(buf, (char *)cmd, len);
|
||||
} else {
|
||||
xstrlcpy(buf, (char *)cmd, len);
|
||||
}
|
||||
if (itmp != NULL) {
|
||||
// If there is a pipe, we have to put the '<' in front of it.
|
||||
// Don't do this when 'shellquote' is not empty, otherwise the
|
||||
@@ -1614,7 +1630,11 @@ char_u *make_filter_cmd(char_u *cmd, char_u *itmp, char_u *otmp)
|
||||
*p = NUL;
|
||||
}
|
||||
}
|
||||
xstrlcat(buf, " < ", len);
|
||||
if (is_pwsh) {
|
||||
xstrlcat(buf, " -RedirectStandardInput ", len);
|
||||
} else {
|
||||
xstrlcat(buf, " < ", len);
|
||||
}
|
||||
xstrlcat(buf, (const char *)itmp, len);
|
||||
if (*p_shq == NUL) {
|
||||
const char *const p = find_pipe((const char *)cmd);
|
||||
|
@@ -511,9 +511,9 @@ function module.set_shell_powershell()
|
||||
module.exec([[
|
||||
let &shell = ']]..shell..[['
|
||||
set shellquote= shellxquote=
|
||||
let &shellpipe = '2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode'
|
||||
let &shellredir = '2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode'
|
||||
let &shellcmdflag = '-NoLogo -NoProfile -ExecutionPolicy RemoteSigned -Command ]]..cmd..[['
|
||||
let &shellpipe = '2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode'
|
||||
let &shellredir = '-RedirectStandardOutput %s -NoNewWindow -Wait'
|
||||
]])
|
||||
end
|
||||
|
||||
|
@@ -6,6 +6,8 @@ local eq, call, clear, eval, feed_command, feed, nvim =
|
||||
helpers.eq, helpers.call, helpers.clear, helpers.eval, helpers.feed_command,
|
||||
helpers.feed, helpers.nvim
|
||||
local command = helpers.command
|
||||
local insert = helpers.insert
|
||||
local expect = helpers.expect
|
||||
local exc_exec = helpers.exc_exec
|
||||
local iswin = helpers.iswin
|
||||
local os_kill = helpers.os_kill
|
||||
@@ -630,3 +632,29 @@ describe('systemlist()', function()
|
||||
end)
|
||||
|
||||
end)
|
||||
|
||||
it(':{range}! works with powershell for filter and redirection #16271', function()
|
||||
clear()
|
||||
if not helpers.has_powershell() then
|
||||
pending("not tested; powershell was not found", function() end)
|
||||
return
|
||||
end
|
||||
local screen = Screen.new(500, 8)
|
||||
screen:attach()
|
||||
helpers.set_shell_powershell()
|
||||
insert([[
|
||||
3
|
||||
1
|
||||
4
|
||||
2]])
|
||||
feed(':4verbose %!sort<cr>')
|
||||
screen:expect{
|
||||
any=[[Executing command: "Start%-Process sort %-RedirectStandardInput .* %-RedirectStandardOutput .* %-NoNewWindow %-Wait".*4 lines filtered.*Press ENTER or type command to continue]]
|
||||
}
|
||||
feed('<CR>')
|
||||
expect([[
|
||||
1
|
||||
2
|
||||
3
|
||||
4]])
|
||||
end)
|
||||
|
Reference in New Issue
Block a user