Skip to content

Commit e8535a8

Browse files
authored
* Work around Windows bug microsoft/terminal#15212 where %~dp0 expansion is broken, by attempting to look up emcc first in EMSCRIPTEN, and then in PATH if it cannot be expanded to via %~dp0. Add a new test other.test_windows_batch_file_dp0_expansion_bug to check for this scenario. * Update bat scripts to avoid ':' inside if() blocks.
1 parent f651079 commit e8535a8

27 files changed

+708
-95
lines changed

em++.bat

+26-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
:: To make modifications to this file, edit `tools/run_python_compiler.bat` and
66
:: then run `tools/create_entry_points.py`
77

8+
:: N.b. In Windows .bat scripts, the ':' character cannot appear inside any if () blocks,
9+
:: or there will be a parsing error.
10+
811
:: All env. vars specified in this file are to be local only to this script.
912
@setlocal
1013
:: -E will not ignore _PYTHON_SYSCONFIGDATA_NAME an internal
@@ -15,13 +18,34 @@
1518
set EM_PY=python
1619
)
1720

21+
:: Work around Windows bug https://github.com/microsoft/terminal/issues/15212 : If this
22+
:: script is invoked via enclosing the invocation in quotes via PATH lookup, then %~f0 and
23+
:: %~dp0 expansions will not work.
24+
:: So first try if %~dp0 might work, and if not, manually look up this script from PATH.
25+
@if exist %~f0 (
26+
set MYDIR=%~dp0
27+
goto FOUND_MYDIR
28+
)
29+
@for %%I in (%~n0.bat) do (
30+
@if exist %%~$PATH:I (
31+
set MYDIR=%%~dp$PATH:I
32+
) else (
33+
echo Fatal Error! Due to a Windows bug, we are unable to locate the path to %~n0.bat.
34+
echo To help this issue, try removing unnecessary quotes in the invocation of emcc,
35+
echo or add Emscripten directory to PATH.
36+
echo See github.com/microsoft/terminal/issues/15212 and
37+
echo github.com/emscripten-core/emscripten/issues/19207 for more details.
38+
)
39+
)
40+
:FOUND_MYDIR
41+
1842
:: If _EMCC_CCACHE is not set, do a regular invocation of the python compiler driver.
1943
:: Otherwise remove the ccache env. var, and then reinvoke this script with ccache enabled.
2044
@if "%_EMCC_CCACHE%"=="" (
21-
set CMD="%EM_PY%" -E "%~dp0\%~n0.py"
45+
set CMD="%EM_PY%" -E "%MYDIR%%~n0.py"
2246
) else (
2347
set _EMCC_CCACHE=
24-
set CMD=ccache "%~dp0\%~n0.bat"
48+
set CMD=ccache "%MYDIR%%~n0.bat"
2549
)
2650

2751
:: Python Windows bug https://bugs.python.org/issue34780: If this script was invoked via a

em-config.bat

+28-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
:: To make modifications to this file, edit `tools/run_python.bat` and then run
66
:: `tools/create_entry_points.py`
77

8+
:: N.b. In Windows .bat scripts, the ':' character cannot appear inside any if () blocks,
9+
:: or there will be a parsing error.
10+
811
:: All env. vars specified in this file are to be local only to this script.
912
@setlocal
1013
:: -E will not ignore _PYTHON_SYSCONFIGDATA_NAME an internal
@@ -15,6 +18,27 @@
1518
set EM_PY=python
1619
)
1720

21+
:: Work around Windows bug https://github.com/microsoft/terminal/issues/15212 : If this
22+
:: script is invoked via enclosing the invocation in quotes via PATH lookup, then %~f0 and
23+
:: %~dp0 expansions will not work.
24+
:: So first try if %~dp0 might work, and if not, manually look up this script from PATH.
25+
@if exist %~f0 (
26+
set MYDIR=%~dp0
27+
goto FOUND_MYDIR
28+
)
29+
@for %%I in (%~n0.bat) do (
30+
@if exist %%~$PATH:I (
31+
set MYDIR=%%~dp$PATH:I
32+
) else (
33+
echo Fatal Error! Due to a Windows bug, we are unable to locate the path to %~n0.bat.
34+
echo To help this issue, try removing unnecessary quotes in the invocation of emcc,
35+
echo or add Emscripten directory to PATH.
36+
echo See github.com/microsoft/terminal/issues/15212 and
37+
echo github.com/emscripten-core/emscripten/issues/19207 for more details.
38+
)
39+
)
40+
:FOUND_MYDIR
41+
1842
:: Python Windows bug https://bugs.python.org/issue34780: If this script was invoked via a
1943
:: shared stdin handle from the parent process, and that parent process stdin handle is in
2044
:: a certain state, running python.exe might hang here. To work around this, if
@@ -47,16 +71,16 @@
4771
)
4872

4973
:NORMAL_EXIT
50-
@"%EM_PY%" -E "%~dp0\%~n0.py" %*
74+
@"%EM_PY%" -E "%MYDIR%%~n0.py" %*
5175
@exit %ERRORLEVEL%
5276

5377
:MUTE_STDIN
54-
@"%EM_PY%" -E "%~dp0\%~n0.py" %* < NUL
78+
@"%EM_PY%" -E "%MYDIR%%~n0.py" %* < NUL
5579
@exit /b %ERRORLEVEL%
5680

5781
:MUTE_STDIN_EXIT
58-
@"%EM_PY%" -E "%~dp0\%~n0.py" %* < NUL
82+
@"%EM_PY%" -E "%MYDIR%%~n0.py" %* < NUL
5983
@exit %ERRORLEVEL%
6084

6185
:NORMAL
62-
@"%EM_PY%" -E "%~dp0\%~n0.py" %*
86+
@"%EM_PY%" -E "%MYDIR%%~n0.py" %*

emar.bat

+28-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
:: To make modifications to this file, edit `tools/run_python.bat` and then run
66
:: `tools/create_entry_points.py`
77

8+
:: N.b. In Windows .bat scripts, the ':' character cannot appear inside any if () blocks,
9+
:: or there will be a parsing error.
10+
811
:: All env. vars specified in this file are to be local only to this script.
912
@setlocal
1013
:: -E will not ignore _PYTHON_SYSCONFIGDATA_NAME an internal
@@ -15,6 +18,27 @@
1518
set EM_PY=python
1619
)
1720

21+
:: Work around Windows bug https://github.com/microsoft/terminal/issues/15212 : If this
22+
:: script is invoked via enclosing the invocation in quotes via PATH lookup, then %~f0 and
23+
:: %~dp0 expansions will not work.
24+
:: So first try if %~dp0 might work, and if not, manually look up this script from PATH.
25+
@if exist %~f0 (
26+
set MYDIR=%~dp0
27+
goto FOUND_MYDIR
28+
)
29+
@for %%I in (%~n0.bat) do (
30+
@if exist %%~$PATH:I (
31+
set MYDIR=%%~dp$PATH:I
32+
) else (
33+
echo Fatal Error! Due to a Windows bug, we are unable to locate the path to %~n0.bat.
34+
echo To help this issue, try removing unnecessary quotes in the invocation of emcc,
35+
echo or add Emscripten directory to PATH.
36+
echo See github.com/microsoft/terminal/issues/15212 and
37+
echo github.com/emscripten-core/emscripten/issues/19207 for more details.
38+
)
39+
)
40+
:FOUND_MYDIR
41+
1842
:: Python Windows bug https://bugs.python.org/issue34780: If this script was invoked via a
1943
:: shared stdin handle from the parent process, and that parent process stdin handle is in
2044
:: a certain state, running python.exe might hang here. To work around this, if
@@ -47,16 +71,16 @@
4771
)
4872

4973
:NORMAL_EXIT
50-
@"%EM_PY%" -E "%~dp0\%~n0.py" %*
74+
@"%EM_PY%" -E "%MYDIR%%~n0.py" %*
5175
@exit %ERRORLEVEL%
5276

5377
:MUTE_STDIN
54-
@"%EM_PY%" -E "%~dp0\%~n0.py" %* < NUL
78+
@"%EM_PY%" -E "%MYDIR%%~n0.py" %* < NUL
5579
@exit /b %ERRORLEVEL%
5680

5781
:MUTE_STDIN_EXIT
58-
@"%EM_PY%" -E "%~dp0\%~n0.py" %* < NUL
82+
@"%EM_PY%" -E "%MYDIR%%~n0.py" %* < NUL
5983
@exit %ERRORLEVEL%
6084

6185
:NORMAL
62-
@"%EM_PY%" -E "%~dp0\%~n0.py" %*
86+
@"%EM_PY%" -E "%MYDIR%%~n0.py" %*

embuilder.bat

+28-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
:: To make modifications to this file, edit `tools/run_python.bat` and then run
66
:: `tools/create_entry_points.py`
77

8+
:: N.b. In Windows .bat scripts, the ':' character cannot appear inside any if () blocks,
9+
:: or there will be a parsing error.
10+
811
:: All env. vars specified in this file are to be local only to this script.
912
@setlocal
1013
:: -E will not ignore _PYTHON_SYSCONFIGDATA_NAME an internal
@@ -15,6 +18,27 @@
1518
set EM_PY=python
1619
)
1720

21+
:: Work around Windows bug https://github.com/microsoft/terminal/issues/15212 : If this
22+
:: script is invoked via enclosing the invocation in quotes via PATH lookup, then %~f0 and
23+
:: %~dp0 expansions will not work.
24+
:: So first try if %~dp0 might work, and if not, manually look up this script from PATH.
25+
@if exist %~f0 (
26+
set MYDIR=%~dp0
27+
goto FOUND_MYDIR
28+
)
29+
@for %%I in (%~n0.bat) do (
30+
@if exist %%~$PATH:I (
31+
set MYDIR=%%~dp$PATH:I
32+
) else (
33+
echo Fatal Error! Due to a Windows bug, we are unable to locate the path to %~n0.bat.
34+
echo To help this issue, try removing unnecessary quotes in the invocation of emcc,
35+
echo or add Emscripten directory to PATH.
36+
echo See github.com/microsoft/terminal/issues/15212 and
37+
echo github.com/emscripten-core/emscripten/issues/19207 for more details.
38+
)
39+
)
40+
:FOUND_MYDIR
41+
1842
:: Python Windows bug https://bugs.python.org/issue34780: If this script was invoked via a
1943
:: shared stdin handle from the parent process, and that parent process stdin handle is in
2044
:: a certain state, running python.exe might hang here. To work around this, if
@@ -47,16 +71,16 @@
4771
)
4872

4973
:NORMAL_EXIT
50-
@"%EM_PY%" -E "%~dp0\%~n0.py" %*
74+
@"%EM_PY%" -E "%MYDIR%%~n0.py" %*
5175
@exit %ERRORLEVEL%
5276

5377
:MUTE_STDIN
54-
@"%EM_PY%" -E "%~dp0\%~n0.py" %* < NUL
78+
@"%EM_PY%" -E "%MYDIR%%~n0.py" %* < NUL
5579
@exit /b %ERRORLEVEL%
5680

5781
:MUTE_STDIN_EXIT
58-
@"%EM_PY%" -E "%~dp0\%~n0.py" %* < NUL
82+
@"%EM_PY%" -E "%MYDIR%%~n0.py" %* < NUL
5983
@exit %ERRORLEVEL%
6084

6185
:NORMAL
62-
@"%EM_PY%" -E "%~dp0\%~n0.py" %*
86+
@"%EM_PY%" -E "%MYDIR%%~n0.py" %*

emcc.bat

+26-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
:: To make modifications to this file, edit `tools/run_python_compiler.bat` and
66
:: then run `tools/create_entry_points.py`
77

8+
:: N.b. In Windows .bat scripts, the ':' character cannot appear inside any if () blocks,
9+
:: or there will be a parsing error.
10+
811
:: All env. vars specified in this file are to be local only to this script.
912
@setlocal
1013
:: -E will not ignore _PYTHON_SYSCONFIGDATA_NAME an internal
@@ -15,13 +18,34 @@
1518
set EM_PY=python
1619
)
1720

21+
:: Work around Windows bug https://github.com/microsoft/terminal/issues/15212 : If this
22+
:: script is invoked via enclosing the invocation in quotes via PATH lookup, then %~f0 and
23+
:: %~dp0 expansions will not work.
24+
:: So first try if %~dp0 might work, and if not, manually look up this script from PATH.
25+
@if exist %~f0 (
26+
set MYDIR=%~dp0
27+
goto FOUND_MYDIR
28+
)
29+
@for %%I in (%~n0.bat) do (
30+
@if exist %%~$PATH:I (
31+
set MYDIR=%%~dp$PATH:I
32+
) else (
33+
echo Fatal Error! Due to a Windows bug, we are unable to locate the path to %~n0.bat.
34+
echo To help this issue, try removing unnecessary quotes in the invocation of emcc,
35+
echo or add Emscripten directory to PATH.
36+
echo See github.com/microsoft/terminal/issues/15212 and
37+
echo github.com/emscripten-core/emscripten/issues/19207 for more details.
38+
)
39+
)
40+
:FOUND_MYDIR
41+
1842
:: If _EMCC_CCACHE is not set, do a regular invocation of the python compiler driver.
1943
:: Otherwise remove the ccache env. var, and then reinvoke this script with ccache enabled.
2044
@if "%_EMCC_CCACHE%"=="" (
21-
set CMD="%EM_PY%" -E "%~dp0\%~n0.py"
45+
set CMD="%EM_PY%" -E "%MYDIR%%~n0.py"
2246
) else (
2347
set _EMCC_CCACHE=
24-
set CMD=ccache "%~dp0\%~n0.bat"
48+
set CMD=ccache "%MYDIR%%~n0.bat"
2549
)
2650

2751
:: Python Windows bug https://bugs.python.org/issue34780: If this script was invoked via a

emcmake.bat

+28-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
:: To make modifications to this file, edit `tools/run_python.bat` and then run
66
:: `tools/create_entry_points.py`
77

8+
:: N.b. In Windows .bat scripts, the ':' character cannot appear inside any if () blocks,
9+
:: or there will be a parsing error.
10+
811
:: All env. vars specified in this file are to be local only to this script.
912
@setlocal
1013
:: -E will not ignore _PYTHON_SYSCONFIGDATA_NAME an internal
@@ -15,6 +18,27 @@
1518
set EM_PY=python
1619
)
1720

21+
:: Work around Windows bug https://github.com/microsoft/terminal/issues/15212 : If this
22+
:: script is invoked via enclosing the invocation in quotes via PATH lookup, then %~f0 and
23+
:: %~dp0 expansions will not work.
24+
:: So first try if %~dp0 might work, and if not, manually look up this script from PATH.
25+
@if exist %~f0 (
26+
set MYDIR=%~dp0
27+
goto FOUND_MYDIR
28+
)
29+
@for %%I in (%~n0.bat) do (
30+
@if exist %%~$PATH:I (
31+
set MYDIR=%%~dp$PATH:I
32+
) else (
33+
echo Fatal Error! Due to a Windows bug, we are unable to locate the path to %~n0.bat.
34+
echo To help this issue, try removing unnecessary quotes in the invocation of emcc,
35+
echo or add Emscripten directory to PATH.
36+
echo See github.com/microsoft/terminal/issues/15212 and
37+
echo github.com/emscripten-core/emscripten/issues/19207 for more details.
38+
)
39+
)
40+
:FOUND_MYDIR
41+
1842
:: Python Windows bug https://bugs.python.org/issue34780: If this script was invoked via a
1943
:: shared stdin handle from the parent process, and that parent process stdin handle is in
2044
:: a certain state, running python.exe might hang here. To work around this, if
@@ -47,16 +71,16 @@
4771
)
4872

4973
:NORMAL_EXIT
50-
@"%EM_PY%" -E "%~dp0\%~n0.py" %*
74+
@"%EM_PY%" -E "%MYDIR%%~n0.py" %*
5175
@exit %ERRORLEVEL%
5276

5377
:MUTE_STDIN
54-
@"%EM_PY%" -E "%~dp0\%~n0.py" %* < NUL
78+
@"%EM_PY%" -E "%MYDIR%%~n0.py" %* < NUL
5579
@exit /b %ERRORLEVEL%
5680

5781
:MUTE_STDIN_EXIT
58-
@"%EM_PY%" -E "%~dp0\%~n0.py" %* < NUL
82+
@"%EM_PY%" -E "%MYDIR%%~n0.py" %* < NUL
5983
@exit %ERRORLEVEL%
6084

6185
:NORMAL
62-
@"%EM_PY%" -E "%~dp0\%~n0.py" %*
86+
@"%EM_PY%" -E "%MYDIR%%~n0.py" %*

emconfigure.bat

+28-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
:: To make modifications to this file, edit `tools/run_python.bat` and then run
66
:: `tools/create_entry_points.py`
77

8+
:: N.b. In Windows .bat scripts, the ':' character cannot appear inside any if () blocks,
9+
:: or there will be a parsing error.
10+
811
:: All env. vars specified in this file are to be local only to this script.
912
@setlocal
1013
:: -E will not ignore _PYTHON_SYSCONFIGDATA_NAME an internal
@@ -15,6 +18,27 @@
1518
set EM_PY=python
1619
)
1720

21+
:: Work around Windows bug https://github.com/microsoft/terminal/issues/15212 : If this
22+
:: script is invoked via enclosing the invocation in quotes via PATH lookup, then %~f0 and
23+
:: %~dp0 expansions will not work.
24+
:: So first try if %~dp0 might work, and if not, manually look up this script from PATH.
25+
@if exist %~f0 (
26+
set MYDIR=%~dp0
27+
goto FOUND_MYDIR
28+
)
29+
@for %%I in (%~n0.bat) do (
30+
@if exist %%~$PATH:I (
31+
set MYDIR=%%~dp$PATH:I
32+
) else (
33+
echo Fatal Error! Due to a Windows bug, we are unable to locate the path to %~n0.bat.
34+
echo To help this issue, try removing unnecessary quotes in the invocation of emcc,
35+
echo or add Emscripten directory to PATH.
36+
echo See github.com/microsoft/terminal/issues/15212 and
37+
echo github.com/emscripten-core/emscripten/issues/19207 for more details.
38+
)
39+
)
40+
:FOUND_MYDIR
41+
1842
:: Python Windows bug https://bugs.python.org/issue34780: If this script was invoked via a
1943
:: shared stdin handle from the parent process, and that parent process stdin handle is in
2044
:: a certain state, running python.exe might hang here. To work around this, if
@@ -47,16 +71,16 @@
4771
)
4872

4973
:NORMAL_EXIT
50-
@"%EM_PY%" -E "%~dp0\%~n0.py" %*
74+
@"%EM_PY%" -E "%MYDIR%%~n0.py" %*
5175
@exit %ERRORLEVEL%
5276

5377
:MUTE_STDIN
54-
@"%EM_PY%" -E "%~dp0\%~n0.py" %* < NUL
78+
@"%EM_PY%" -E "%MYDIR%%~n0.py" %* < NUL
5579
@exit /b %ERRORLEVEL%
5680

5781
:MUTE_STDIN_EXIT
58-
@"%EM_PY%" -E "%~dp0\%~n0.py" %* < NUL
82+
@"%EM_PY%" -E "%MYDIR%%~n0.py" %* < NUL
5983
@exit %ERRORLEVEL%
6084

6185
:NORMAL
62-
@"%EM_PY%" -E "%~dp0\%~n0.py" %*
86+
@"%EM_PY%" -E "%MYDIR%%~n0.py" %*

0 commit comments

Comments
 (0)