Skip to content

Conversation

akriese
Copy link
Contributor

@akriese akriese commented Nov 24, 2022

Fixes #21045.

This takes part of vim patch v8.2.3071 to make pwsh and powershell return the same exepath()s (including extensions) as cmd.exe.

@akriese akriese force-pushed the exepath_pwsh_fix branch 2 times, most recently from bb65cc7 to 49f9cb1 Compare November 24, 2022 14:00
@akriese akriese changed the title treat powershell as not-Unix-like in Windows fix(windows): treat powershell as not-Unix-like in Windows Nov 24, 2022
@justinmk
Copy link
Member

justinmk commented Dec 9, 2022

cc @3N4N

@3N4N
Copy link
Contributor

3N4N commented Dec 12, 2022

Do we not want extensionless files to be found with :h exepath()?

After this patch, the binary files 'text.exe', 'test.bat', 'test.com' will be found, but the file 'test' (without any extension) will not be found. But both where and Get-Command find the extensionless 'test' file.

But neovim with ':set shell=cmd', doesn't find 'test' either (without applying this patch), even though where test returns correct path in cmd. So I don't know what should be the expected behavior.

Edit: where is a program to find files in general, not just executable files. And Get-Command returning extensionless (and thus non-executable) file is puzzling even to Chris Dent, the resident PowerShell expert (I asked him on Discord). So I say exepath() should not find extensionless files, even if it doesn't coincide with what where and Get-Command do.

@akriese
Copy link
Contributor Author

akriese commented Dec 20, 2022

So, as I understand it, if exepath() is supposed to only return executables and Windows does not define extension-less files as executable, this would be the desired behavior, right? Even if the result diverges from Get-Command...

@3N4N
Copy link
Contributor

3N4N commented Dec 20, 2022

this would be the desired behavior, right?

Right. As it stands now, exepath in this PR behaves correctly.

@musjj
Copy link

musjj commented Dec 28, 2022

Would it be a good idea if bash.exe (MSYS2, Git Bash, etc.) also receive the same kind of treatment?
My use-case is that I'm using exepath in order to find *.bat/*.cmd files, so that I can pass it to vim.loop.spawn. But with shell set to bash.exe, the correct path isn't returned.
Actually, why does exepath depend on your shell option in the first place? Why not just query the user's OS?

@justinmk
Copy link
Member

But with shell set to bash.exe, the correct path isn't returned.
Actually, why does exepath depend on your shell option in the first place? Why not just query the user's OS?

in Git bash on windows, files without ".exe" suffix can be executable, right?

After this patch, the binary files 'text.exe', 'test.bat', 'test.com' will be found, but the file 'test' (without any extension) will not be found. But both where and Get-Command find the extensionless 'test' file.

But neovim with ':set shell=cmd', doesn't find 'test' either (without applying this patch), even though where test returns correct path in cmd. So I don't know what should be the expected behavior.

Edit: where is a program to find files in general, not just executable files. And Get-Command returning extensionless (and thus non-executable) file is puzzling even to Chris Dent, the resident PowerShell expert (I asked him on Discord). So I say exepath() should not find extensionless files, even if it doesn't coincide with what where and Get-Command do.

Great info, thanks! So what is the 3-line conclusion here? Please include it in the commit message.

@musjj
Copy link

musjj commented Dec 31, 2022

In Git bash on windows, files without ".exe" suffix can be executable, right?

Not completely sure of the specs, but I think only script (bash, sh, etc.) executables can have no extension in Git Bash. Binary executables require .exe just like usual.

@3N4N

This comment was marked as resolved.

@3N4N

This comment was marked as off-topic.

@akriese
Copy link
Contributor Author

akriese commented Jan 9, 2023

Oh, yeah I misunderstood that 😅

@justinmk
Copy link
Member

justinmk commented Jan 16, 2023

ported vim-patch 8.2.3071 and related patches to avoid needing to revisit this in the future. 8.2.3071 is mostly N/A because all the magic special-case bullshit (changing slash behavior depending on shell, changing option defaults depending on x, y, z and bloating the docs) is not something we want.

I was hoping 8.2.3071 added test coverage for what this PR fixes, but doesn't look like it.

We need a test case in test/functional/vimscript/exepath_spec.lua .

@zeertzjq
Copy link
Member

Some other related patches: #19313

akriese and others added 5 commits January 16, 2023 11:26
PROBLEM:
exepath("test") should prefer ".bat" when shell=powershell.
Current behavior differs from Vim 8.2.3071.

TEST CASE:
1. in a folder which is in $PATH, create files "test" "test.bat".
   - "(Get-Command test).Path test" returns "test.bat".
2. compare nvim:
     nvim --clean
     :set shell=powershell
     :echo exepath("test")
3. should returns "path\to\test.bat" (before this patch it returns "path\to\test").

SOLUTION:
After this patch, the binary files "text.exe", "test.bat", "test.com"
will be found, but the file "test" (without any extension) will not be
found (matches Vim 8.2.3071). But powershell's `where` and
`Get-Command` _do_ find the extensionless 'test' file.

But Nvim with ":set shell=cmd.exe", doesn't find "test" either (before
and after this patch), even though `where test` returns correct path in
cmd.

- `where` is a program to find files in general, not just executable files.
-`Get-Command` returning extensionless (and thus non-executable) file is
  puzzling even to Chris Dent, [PowerShell expert][1] (asked on
  Discord).

[1]: https://www.amazon.com/Mastering-PowerShell-Scripting-Automate-environment-ebook/dp/B0971MG88X

Co-authored-by: Enan Ajmain <3nan.ajmain@gmail.com>
Helped-by: erw7 <erw7.github@gmail.com>

Fixes neovim#21045
… out

Problem:    Testing the shell option is incomplete and spread out.
Solution:   Move shell tests to one file and increase coverage. (Yegappan
            Lakshmanan, closes vim/vim#8464)

vim/vim@054794c

Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Problem:    Not enough testing for shell use.
Solution:   Add a bit more testing. (Yegappan Lakshmanan, closes vim/vim#8469)

vim/vim@ffec6dd

Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
(Most of this patch is intentionally dropped, it adds far too much
special-cases for almost zero purpose: ":help shell-powershell" makes it
easy to choose powershell without spreading special-cases throughout the
codebase, randomly changing slash behavior, etc.)

Problem:    Shell options are not set properly for PowerShell.
Solution:   Use better option defaults. (Mike Willams, closes vim/vim#8459)

vim/vim@1279502

Co-authored-by: Mike Williams <mikew@globalgraphics.com>
'shellxquote' Nvim default was adjusted in: 131aad9
The use of "/s" is different than Vim, and may avoid the need for `shellxquote="&|<>()@^`.

For the other shells, Nvim intentionally does not fiddle with the
various "shell*" options if 'shell' is set by the user: if the user sets
'shell', they are expected to set other "shell*" options correctly.
@justinmk justinmk merged commit 2773685 into neovim:master Jan 16, 2023
@justinmk
Copy link
Member

We need a test case in test/functional/vimscript/exepath_spec.lua .

Merged to unblock, but it would still be helpful to have this test @akriese

akriese added a commit to akriese/neovim that referenced this pull request Jan 19, 2023
windows shells

This test covers the changes from neovim#21175 where exepath() is set to
prefer file extensions in powershell.exe aswell as in cmd.exe.

In both shells, the file with a valid extension should be returned
instead of the extensionless file.
@akriese
Copy link
Contributor Author

akriese commented Jan 19, 2023

I just created this test. First time writing a test for neovim, so forgive me, if I did something wrong. Unfortunately, I don't know how to run the test on Windows with is_os('win') evaluating to true. I am running with mingw32-make functionaltest and it fails because it thinks, that I am on a not-Windows machine.

Edit: Also, the test for set shell=pwsh.exe will probably fail, if that version of Powershell is not installed on the system (don't know if it is installed by default)

@3N4N
Copy link
Contributor

3N4N commented Jan 19, 2023

I am running with mingw32-make functionaltest and it fails because it thinks, that I am on a not-Windows machine.

I suggest using cmake for everything. Neovim is a cmake project; the makefile provided with it is only for convenience, and I don't think it's catered too much to Windows devs. What I do is use something like these:

PS> cmake -E env DEPS_BUILD_DIR=E:/projects/neovim/deps cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=C:/apps/nvim -DCMAKE_C_COMPILER=clang -DCMAKE_C_FLAGS=-w -B .\build\debug

PS> cmake --build .\build\debug

PS> cmake -E env TEST_FILE=.\test\functional\path\to\your\test\file.lua cmake --build .\build\debug --target functionaltest

I'm not gonna explain what's happening in the commands above. You should be able to figure out with a little bit of time.

Edit: Also, the test for set shell=pwsh.exe will probably fail, if that version of Powershell is not installed on the system (don't know if it is installed by default)

There is a helper function in the test suite called set_shell_powershell. Grep and see how it's used.

@3N4N
Copy link
Contributor

3N4N commented Jan 19, 2023

Also, @akriese you should do this in a new PR.

akriese added a commit to akriese/neovim that referenced this pull request Jan 20, 2023
Windows shells

This test covers the changes from neovim#21175 where exepath() is set to
prefer file extensions in powershell.exe aswell as in cmd.exe.

In both shells, the file with a valid extension should be returned
instead of the extensionless file.
akriese added a commit to akriese/neovim that referenced this pull request Jan 20, 2023
Windows shells

This test covers the changes from neovim#21175 where exepath() is set to
prefer file extensions in powershell.exe aswell as in cmd.exe.

In both shells, the file with a valid extension should be returned
instead of the extensionless file.
@justinmk
Copy link
Member

What I do is use something like these:

PS> cmake -E env DEPS_BUILD_DIR=E:/projects/neovim/deps cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=C:/apps/nvim -DCMAKE_C_COMPILER=clang -DCMAKE_C_FLAGS=-w -B .\build\debug

PS> cmake --build .\build\debug

PS> cmake -E env TEST_FILE=.\test\functional\path\to\your\test\file.lua cmake --build .\build\debug --target functionaltest

see also https://github.com/neovim/neovim/wiki/Building-Neovim#windows--msvc-powershell (@3N4N any improvements to make there?)

@akriese
Copy link
Contributor Author

akriese commented Jan 23, 2023

see also https://github.com/neovim/neovim/wiki/Building-Neovim#windows--msvc-powershell (@3N4N any improvements to make there?)

I used the instructions in the README aswell as @3N4N's commands. Didn't work out of the box, maybe my environment has some issue. It eventually worked out for me when explicitly passing the LUAJIT_LIB path to the cmake command (my build script)

akriese added a commit to akriese/neovim that referenced this pull request Jan 23, 2023
Windows shells

This test covers the changes from neovim#21175 where exepath() is set to
prefer file extensions in powershell.exe aswell as in cmd.exe.

In both shells, the file with a valid extension should be returned
instead of the extensionless file.
akriese added a commit to akriese/neovim that referenced this pull request Jan 23, 2023
Windows shells

This test covers the changes from neovim#21175 where exepath() is set to
prefer file extensions in powershell.exe aswell as in cmd.exe.

In both shells, the file with a valid extension should be returned
instead of the extensionless file.
justinmk pushed a commit that referenced this pull request Jan 26, 2023
test(exepath): test if exepath returns correct path with multiple
Windows shells

This test covers the changes from #21175 where exepath() is set to
prefer file extensions in powershell.exe aswell as in cmd.exe.

In both shells, the file with a valid extension should be returned
instead of the extensionless file.
yesean pushed a commit to yesean/neovim that referenced this pull request Mar 25, 2023
…21928

test(exepath): test if exepath returns correct path with multiple
Windows shells

This test covers the changes from neovim#21175 where exepath() is set to
prefer file extensions in powershell.exe aswell as in cmd.exe.

In both shells, the file with a valid extension should be returned
instead of the extensionless file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Windows: exepath() misses extension in powershell & pwsh

5 participants