fix(shell): on Windows :make does not echo #22728

Problem:
On Windows, :make does not display the output of the program it runs.
The cause is the default 'shellpipe'.  On Linux, nvim uses `tee` to redirect the
output to both stdout and the error file.  In Windows, for both cmd.exe and
powershell, the output is only redirected to the error file.

Solution:
- On Windows, change the 'shellpipe' default to "2>&1| tee".
    - Nvim includes `tee` in its Windows package.
- Document recommended defaults for powershell.

Fixes #12910
This commit is contained in:
Enan Ajmain 2023-03-20 03:25:12 +06:00 committed by GitHub
parent 5726f33e8c
commit ecc4d0e435
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 20 additions and 19 deletions

View File

@ -5235,9 +5235,9 @@ A jump table for the options with a short description can be found at |Q_op|.
*shell-powershell* *shell-powershell*
To use PowerShell: > To use PowerShell: >
let &shell = executable('pwsh') ? 'pwsh' : 'powershell' let &shell = executable('pwsh') ? 'pwsh' : 'powershell'
let &shellcmdflag = '-NoLogo -NoProfile -ExecutionPolicy RemoteSigned -Command [Console]::InputEncoding=[Console]::OutputEncoding=[System.Text.Encoding]::UTF8;' let &shellcmdflag = '-NoLogo -ExecutionPolicy RemoteSigned -Command [Console]::InputEncoding=[Console]::OutputEncoding=[System.Text.UTF8Encoding]::new();$PSDefaultParameterValues[''Out-File:Encoding'']=''utf8'';'
let &shellredir = '2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode' let &shellredir = '2>&1 | %%{ "$_" } | Out-File %s; exit $LastExitCode'
let &shellpipe = '2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode' let &shellpipe = '2>&1 | %%{ "$_" } | Tee-Object %s; exit $LastExitCode'
set shellquote= shellxquote= set shellquote= shellxquote=
< This option cannot be set from a |modeline| or in the |sandbox|, for < This option cannot be set from a |modeline| or in the |sandbox|, for
@ -5259,8 +5259,7 @@ A jump table for the options with a short description can be found at |Q_op|.
security reasons. security reasons.
*'shellpipe'* *'sp'* *'shellpipe'* *'sp'*
'shellpipe' 'sp' string (default ">", ">%s 2>&1", "| tee", "|& tee" or 'shellpipe' 'sp' string (default ">", "| tee", "|& tee" or "2>&1| tee")
"2>&1| tee")
global global
String to be used to put the output of the ":make" command in the String to be used to put the output of the ":make" command in the
error file. See also |:make_makeprg|. See |option-backslash| about error file. See also |:make_makeprg|. See |option-backslash| about
@ -5268,8 +5267,8 @@ A jump table for the options with a short description can be found at |Q_op|.
The name of the temporary file can be represented by "%s" if necessary The name of the temporary file can be represented by "%s" if necessary
(the file name is appended automatically if no %s appears in the value (the file name is appended automatically if no %s appears in the value
of this option). of this option).
For MS-Windows the default is ">%s 2>&1". The output is directly For MS-Windows the default is "2>&1| tee". The stdout and stderr are
saved in a file and not echoed to the screen. saved in a file and echoed to the screen.
For Unix the default is "| tee". The stdout of the compiler is saved For Unix the default is "| tee". The stdout of the compiler is saved
in a file and echoed to the screen. If the 'shell' option is "csh" or in a file and echoed to the screen. If the 'shell' option is "csh" or
"tcsh" after initializations, the default becomes "|& tee". If the "tcsh" after initializations, the default becomes "|& tee". If the

View File

@ -956,7 +956,7 @@ or simpler >
"$*" can be given multiple times, for example: > "$*" can be given multiple times, for example: >
:set makeprg=gcc\ -o\ $*\ $* :set makeprg=gcc\ -o\ $*\ $*
The 'shellpipe' option defaults to ">%s 2>&1" for Win32. The 'shellpipe' option defaults to "2>&1| tee" for Win32.
This means that the output of the compiler is saved in a file and not shown on This means that the output of the compiler is saved in a file and not shown on
the screen directly. For Unix "| tee" is used. The compiler output is shown the screen directly. For Unix "| tee" is used. The compiler output is shown
on the screen and saved in a file the same time. Depending on the shell used on the screen and saved in a file the same time. Depending on the shell used

View File

@ -2026,7 +2026,7 @@ return {
varname='p_sp', varname='p_sp',
defaults={ defaults={
condition='MSWIN', condition='MSWIN',
if_true=">%s 2>&1", if_true="2>&1| tee",
if_false="| tee", if_false="| tee",
} }
}, },

View File

@ -35,7 +35,7 @@ describe(':make', function()
local out = eval('execute("make")') local out = eval('execute("make")')
-- Ensure there are no "shell returned X" messages between -- Ensure there are no "shell returned X" messages between
-- command and last line (indicating zero exit) -- command and last line (indicating zero exit)
matches('LastExitCode%s+[(]', out) matches('LastExitCode%s+ready [$]%s+[(]', out)
matches('\n.*%: ready [$]', out) matches('\n.*%: ready [$]', out)
end) end)

View File

@ -553,16 +553,18 @@ function module.set_shell_powershell(fake)
assert(found) assert(found)
end end
local shell = found and (is_os('win') and 'powershell' or 'pwsh') or module.testprg('pwsh-test') local shell = found and (is_os('win') and 'powershell' or 'pwsh') or module.testprg('pwsh-test')
local set_encoding = '[Console]::InputEncoding=[Console]::OutputEncoding=[System.Text.UTF8Encoding]::new();' local cmd = 'Remove-Item -Force '..table.concat(is_os('win')
local cmd = set_encoding..'Remove-Item -Force '..table.concat(is_os('win')
and {'alias:cat', 'alias:echo', 'alias:sleep', 'alias:sort'} and {'alias:cat', 'alias:echo', 'alias:sleep', 'alias:sort'}
or {'alias:echo'}, ',')..';' or {'alias:echo'}, ',')..';'
module.exec([[ module.exec([[
let &shell = ']]..shell..[[' let &shell = ']]..shell..[['
set shellquote= shellxquote= set shellquote= shellxquote=
let &shellcmdflag = '-NoLogo -NoProfile -ExecutionPolicy RemoteSigned -Command ]]..cmd..[[' let &shellcmdflag = '-NoLogo -NoProfile -ExecutionPolicy RemoteSigned -Command '
let &shellpipe = '2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode' let &shellcmdflag .= '[Console]::InputEncoding=[Console]::OutputEncoding=[System.Text.UTF8Encoding]::new();'
let &shellredir = '2>&1 | Out-File -Encoding UTF8 %s; exit $LastExitCode' let &shellcmdflag .= '$PSDefaultParameterValues[''Out-File:Encoding'']=''utf8'';'
let &shellcmdflag .= ']]..cmd..[['
let &shellredir = '2>&1 | %%{ "$_" } | Out-File %s; exit $LastExitCode'
let &shellpipe = '2>&1 | %%{ "$_" } | Tee-Object %s; exit $LastExitCode'
]]) ]])
return found return found
end end

View File

@ -644,12 +644,12 @@ describe('shell :!', function()
if is_os('win') then if is_os('win') then
feed(':4verbose %!sort /R<cr>') feed(':4verbose %!sort /R<cr>')
screen:expect{ screen:expect{
any=[[Executing command: .?& { Get%-Content .* | & sort /R } 2>&1 | Out%-File %-Encoding UTF8 .*; exit $LastExitCode"]] any=[[Executing command: .?& { Get%-Content .* | & sort /R } 2>&1 | %%{ "$_" } | Out%-File .*; exit $LastExitCode"]]
} }
else else
feed(':4verbose %!sort -r<cr>') feed(':4verbose %!sort -r<cr>')
screen:expect{ screen:expect{
any=[[Executing command: .?& { Get%-Content .* | & sort %-r } 2>&1 | Out%-File %-Encoding UTF8 .*; exit $LastExitCode"]] any=[[Executing command: .?& { Get%-Content .* | & sort %-r } 2>&1 | %%{ "$_" } | Out%-File .*; exit $LastExitCode"]]
} }
end end
feed('<CR>') feed('<CR>')

View File

@ -22,7 +22,7 @@ func Test_shell_options()
\ ['tcsh', '-c', '|& tee', '', '>&', '', '']] \ ['tcsh', '-c', '|& tee', '', '>&', '', '']]
endif endif
if has('win32') if has('win32')
let shells += [['cmd', '/s /c', '>%s 2>&1', '', '>%s 2>&1', '', '"']] let shells += [['cmd', '/s /c', '2>&1| tee', '', '>%s 2>&1', '', '"']]
endif endif
" start a new Vim instance with 'shell' set to each of the supported shells " start a new Vim instance with 'shell' set to each of the supported shells