v icon indicating copy to clipboard operation
v copied to clipboard

Can't use os.execvp to start an executable with space in path

Open rhoellwarth opened this issue 2 years ago • 6 comments

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.

rhoellwarth avatar Sep 24 '22 16:09 rhoellwarth

on macos, windows 10, I didn't find this problem.

shove70 avatar Sep 25 '22 14:09 shove70

OK. "my" v ist build with tcc. Perhaps that's the difference? Where else could I look for an error?

rhoellwarth avatar Sep 28 '22 09:09 rhoellwarth

I can't reproduce it too. Can you check again and share your reproducible test case?

felipensp avatar Dec 27 '22 12:12 felipensp

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

rhoellwarth avatar Mar 17 '23 15:03 rhoellwarth

Make sure you have the latest V 0.3.3 6e1e406

JalonSolov avatar Mar 17 '23 16:03 JalonSolov

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"

Thybar avatar Mar 28 '23 13:03 Thybar

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').

Dialga avatar Jul 01 '23 11:07 Dialga