I am using Sublime Text 4 and I have a problem trying to set up a Build-System for running Python programs on the cmd console. I think it is caused by an space in the name of one of the directories in my path, I have spent hours looking for solutions on many different sites (I know very little about how the Build-System logic works in general), and I am not able to fix it. I've tried using the (^), (*), and (') symbols to indicate the space character, and also to quote the hole path (which worked pasting it on de console, but not specifiying it from the Build-System), but none of them has worked for me.
The code is the following (I copied it from a youtube tutorial, and it worked fine without spaces in the python path):
{
"shell_cmd": "start cmd /C \"(C:/Program Files/Python/python.exe \"$file\" || set /p = Failed execution. Press Enter to exit...) && set /p = Successful execution. Press Enter to exit...\"",
"selector": "source.python",
"working_dir": "$file_dir"}
And the cmd console returned this (translated from Spanish):
"C:/Program" it is not recognized as an internal or external command executable program or batch file. Failed execution. Press Enter to exit...
Any idea if there is anyway I can fix this?
The solution is using the string:
The usage of
shell_cmdresults in starting in background without a visible console windowcmd.exewith option/cand the command line specified in next string using the Windows kernel library function CreateProcess with an appropriate filled STARTUPINFO structure.How the Windows command processor
cmd.exeinterprets the argument string(s) after the option/cis explained by the help output on runningcmd /?in a command prompt window. There are five conditions which define the general behavior on interpreting the argument string(s) after option/c(or option/k) and the first one is the usage of option/Sleft to option/Cor/K.How the Windows command processor instance is started by Sublime Text 4 on using
shell_cmdcannot be controlled by the user. In this case is executed in the background:$fileis replaced by the file name string by Sublime Text 4 before starting the Windows command processor.For that reason the command line is coded in a manner to work with
cmd.exe /cas used by Sublime Text 4. The free Sysinternals (Microsoft) tool Process Monitor can be used to see what happens in background when an executable starts another executable. After double clicking on a process in log of Process Monitor and selecting tab Process the Command Line can be seen used to start the process.A visible console window is wanted in this case which is the reason why the internal command
startis used to start one more instance ofcmd.exe. The usage ofstartresults in using again the functionCreateProcess, but this time by thecmdinstance started by Sublime Text 4, with using aSTARTUPINFOfilled to open a new console window byCreateProcesson starting the executable.The executable to start is
cmd.exespecified with its full qualified file name by referencing the value of the predefined Windows environment variableComSpecwith%ComSpec%which is expanded by first startedcmd.exeinstance already toC:\Windows\System32\cmd.exeon Windows being installed inC:\Windowsbefore executing commandstart.ComSpecis a system environment variable defined by default with%SystemRoot%\System32\cmd.exe. So it would be also possible to replace%ComSpec%by%SystemRoot%\\System32\\cmd.exein the command line string.The second instance of
cmdshould be started with ignoring theAutoRunregistry string value which does not exist by default in Windows registry. That is for security and makes the starting process of secondcmdinstance a little bit faster. More information about option/Dand theAutoRunregistry string value can be read in help of Windows command processor output on runningcmd /?in a command prompt window.The option
/Sis used to explicitly define how the secondcmdinstance should interpret the argument strings after the next option/Cwhich is for executing a command line and closingcmdafter finishing the command line execution.The command line to execute contains the conditional operators
&&and||as explained in detail by single line with multiple commands using Windows batch file and twice an equal sign. For that reason it is highly recommended to enclose the entire command line string in"to get these operators and the equal signs interpreted as literal characters by firstcmdinstance started by Sublime Text 4 when it parses the argument strings after option/c, i.e. the entire coded command line string.The second
cmdinstance removes the first and the last"from the argument string after option/Cbecause of option/Sand interprets the remaining command line for execution.In can be read in help of
cmdthat a file name (or any other argument string) containing a space or one of these characters&()[]{}^=;!'+,`~(and also<>|not possible in a file name) must be enclosed in"to be interpreted as literal character.C:\Program Files\Python\python.execontains a space character which is interpreted as argument string separator on not being inside a double quoted argument string. Therefore the full qualified file name of Python executable must be enclosed in"to be interpreted as one argument string and not as the two argument stringsC:\ProgramandFiles\Python\python.exe.The usage of
||and&&makes it additionally necessary that the execution of Python with the commandsetexecuted onpython.exeterminating itself with an exit value not equal 0 are combined in a command block by using a pair of(and)to get thesetcommand after&&executed only ifpython.exeexited with value 0 for successful execution of the Python script.For the special requirements caused by using
||and&&see:So the second started
cmdinstance with a visible console window runs the command line:The
cmdinstance started first by Sublime Text 4 closes itself already after successful start of secondcmdinstance by internal commandstart. In other words the secondcmdexecutespython.exewithCreateProcesswith aSTARTUPINFOto use the console window and the input, output and error streams of secondcmdwhile firstcmdexited itself already which means for Sublime Text 4 that the execution of the tool finished already although in real secondcmd.exeandpython.exeare still running.