v
v copied to clipboard
Can't use os.execvp to start an executable with space in path
V version: V 0.3.1 862d91e.f338dec OS: windows, Microsoft Windows 10 Pro v19044 64-bit
What did you do? // pseudocode - just copy i.e. notepad.exe to a freshly created path with spaces import os p:='Path with spaces\some.exe' empty:=[]string{} os.execvp(p,empty) or {panic(err)}
What did you expect to see? The Executable should be started.
What did you see instead? Error from the system, telling me that "Path" is no valid executable (i.e. the space after "Path" was treated as argument separator instead of a part of the path to the exe.) I tried also to manually quote ("") and use os.quoted_path - both ended in "V panic: Invalid argument; code: 22".
I found the workaround to first os.chdir(os.dir(p)) and then os.execvp(os.base(p),empty) - that worked.
on macos, windows 10, I didn't find this problem.
OK. "my" v ist build with tcc. Perhaps that's the difference? Where else could I look for an error?
I can't reproduce it too. Can you check again and share your reproducible test case?
Here is a - for me - working example. I'm running it inside a Powershell (7.3.2) which is itself running inside a Windows-Terminal (1.16.10261.0) Window:
PS D:\With spaces> cp C:\Windows\notepad.exe .
PS D:\With spaces> v version
V 0.3.3 6e1e406
PS D:\With spaces> cat .\test.v
import os
p := 'D:\With Spaces\notepad.exe'
empty := []string{}
os.execvp(p, empty) or { panic(err) }
PS D:\With spaces> v run test.v
V panic: Invalid argument; code: 22
v hash: 6e1e406
C:/Users/rhoel/AppData/Local/Temp/v_0/test.5037899193432941379.tmp.c:7971: at _v_panic: Backtrace
C:/Users/rhoel/AppData/Local/Temp/v_0/test.5037899193432941379.tmp.c:18084: by main__main
C:/Users/rhoel/AppData/Local/Temp/v_0/test.5037899193432941379.tmp.c:18471: by wmain
00495880 : by ???
004959e3 : by ???
7ffd432b7614 : by ???
OS: Microsoft Windows 10 Pro Version 22H2, Build 19045.2673, 64-bit
Make sure you have the latest V 0.3.3 6e1e406
V version: V 0.3.3 6b4fb0f OS: windows 10 Pro 64-bit
When i run this command: os.execvp('C:/Program Files (x86)/Mozilla Firefox/firefox.exe', [])!
Firefox opens 3 tabs: 1 - "https://www.files.com" 2 - "https://www.(x86).com/Mozilla" 3 - "https://www.firefox.com/firefox.exe"
os.execvp() calls _execvp() on Windows and the _execv() family of functions will execute the command path exactly as you've called it.
It is up to the user to quote the path to the executable.
See https://learn.microsoft.com/en-us/cpp/c-runtime-library/exec-wexec-functions?view=msvc-170#:~:text=Spaces%20embedded%20in%20strings%20may%20cause%20unexpected%20behavior for more information.
So either use '"D:\With Spaces\notepad.exe"'
or os.real_path('D:\With Spaces\notepad.exe')
.