Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
juj authored Apr 22, 2023
1 parent f651079 commit e8535a8
Show file tree
Hide file tree
Showing 27 changed files with 708 additions and 95 deletions.
28 changes: 26 additions & 2 deletions em++.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
:: To make modifications to this file, edit `tools/run_python_compiler.bat` and
:: then run `tools/create_entry_points.py`

:: N.b. In Windows .bat scripts, the ':' character cannot appear inside any if () blocks,
:: or there will be a parsing error.

:: All env. vars specified in this file are to be local only to this script.
@setlocal
:: -E will not ignore _PYTHON_SYSCONFIGDATA_NAME an internal
Expand All @@ -15,13 +18,34 @@
set EM_PY=python
)

:: Work around Windows bug https://github.com/microsoft/terminal/issues/15212 : If this
:: script is invoked via enclosing the invocation in quotes via PATH lookup, then %~f0 and
:: %~dp0 expansions will not work.
:: So first try if %~dp0 might work, and if not, manually look up this script from PATH.
@if exist %~f0 (
set MYDIR=%~dp0
goto FOUND_MYDIR
)
@for %%I in (%~n0.bat) do (
@if exist %%~$PATH:I (
set MYDIR=%%~dp$PATH:I
) else (
echo Fatal Error! Due to a Windows bug, we are unable to locate the path to %~n0.bat.
echo To help this issue, try removing unnecessary quotes in the invocation of emcc,
echo or add Emscripten directory to PATH.
echo See github.com/microsoft/terminal/issues/15212 and
echo github.com/emscripten-core/emscripten/issues/19207 for more details.
)
)
:FOUND_MYDIR

:: If _EMCC_CCACHE is not set, do a regular invocation of the python compiler driver.
:: Otherwise remove the ccache env. var, and then reinvoke this script with ccache enabled.
@if "%_EMCC_CCACHE%"=="" (
set CMD="%EM_PY%" -E "%~dp0\%~n0.py"
set CMD="%EM_PY%" -E "%MYDIR%%~n0.py"
) else (
set _EMCC_CCACHE=
set CMD=ccache "%~dp0\%~n0.bat"
set CMD=ccache "%MYDIR%%~n0.bat"
)

:: Python Windows bug https://bugs.python.org/issue34780: If this script was invoked via a
Expand Down
32 changes: 28 additions & 4 deletions em-config.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
:: To make modifications to this file, edit `tools/run_python.bat` and then run
:: `tools/create_entry_points.py`

:: N.b. In Windows .bat scripts, the ':' character cannot appear inside any if () blocks,
:: or there will be a parsing error.

:: All env. vars specified in this file are to be local only to this script.
@setlocal
:: -E will not ignore _PYTHON_SYSCONFIGDATA_NAME an internal
Expand All @@ -15,6 +18,27 @@
set EM_PY=python
)

:: Work around Windows bug https://github.com/microsoft/terminal/issues/15212 : If this
:: script is invoked via enclosing the invocation in quotes via PATH lookup, then %~f0 and
:: %~dp0 expansions will not work.
:: So first try if %~dp0 might work, and if not, manually look up this script from PATH.
@if exist %~f0 (
set MYDIR=%~dp0
goto FOUND_MYDIR
)
@for %%I in (%~n0.bat) do (
@if exist %%~$PATH:I (
set MYDIR=%%~dp$PATH:I
) else (
echo Fatal Error! Due to a Windows bug, we are unable to locate the path to %~n0.bat.
echo To help this issue, try removing unnecessary quotes in the invocation of emcc,
echo or add Emscripten directory to PATH.
echo See github.com/microsoft/terminal/issues/15212 and
echo github.com/emscripten-core/emscripten/issues/19207 for more details.
)
)
:FOUND_MYDIR

:: Python Windows bug https://bugs.python.org/issue34780: If this script was invoked via a
:: shared stdin handle from the parent process, and that parent process stdin handle is in
:: a certain state, running python.exe might hang here. To work around this, if
Expand Down Expand Up @@ -47,16 +71,16 @@
)

:NORMAL_EXIT
@"%EM_PY%" -E "%~dp0\%~n0.py" %*
@"%EM_PY%" -E "%MYDIR%%~n0.py" %*
@exit %ERRORLEVEL%

:MUTE_STDIN
@"%EM_PY%" -E "%~dp0\%~n0.py" %* < NUL
@"%EM_PY%" -E "%MYDIR%%~n0.py" %* < NUL
@exit /b %ERRORLEVEL%

:MUTE_STDIN_EXIT
@"%EM_PY%" -E "%~dp0\%~n0.py" %* < NUL
@"%EM_PY%" -E "%MYDIR%%~n0.py" %* < NUL
@exit %ERRORLEVEL%

:NORMAL
@"%EM_PY%" -E "%~dp0\%~n0.py" %*
@"%EM_PY%" -E "%MYDIR%%~n0.py" %*
32 changes: 28 additions & 4 deletions emar.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
:: To make modifications to this file, edit `tools/run_python.bat` and then run
:: `tools/create_entry_points.py`

:: N.b. In Windows .bat scripts, the ':' character cannot appear inside any if () blocks,
:: or there will be a parsing error.

:: All env. vars specified in this file are to be local only to this script.
@setlocal
:: -E will not ignore _PYTHON_SYSCONFIGDATA_NAME an internal
Expand All @@ -15,6 +18,27 @@
set EM_PY=python
)

:: Work around Windows bug https://github.com/microsoft/terminal/issues/15212 : If this
:: script is invoked via enclosing the invocation in quotes via PATH lookup, then %~f0 and
:: %~dp0 expansions will not work.
:: So first try if %~dp0 might work, and if not, manually look up this script from PATH.
@if exist %~f0 (
set MYDIR=%~dp0
goto FOUND_MYDIR
)
@for %%I in (%~n0.bat) do (
@if exist %%~$PATH:I (
set MYDIR=%%~dp$PATH:I
) else (
echo Fatal Error! Due to a Windows bug, we are unable to locate the path to %~n0.bat.
echo To help this issue, try removing unnecessary quotes in the invocation of emcc,
echo or add Emscripten directory to PATH.
echo See github.com/microsoft/terminal/issues/15212 and
echo github.com/emscripten-core/emscripten/issues/19207 for more details.
)
)
:FOUND_MYDIR

:: Python Windows bug https://bugs.python.org/issue34780: If this script was invoked via a
:: shared stdin handle from the parent process, and that parent process stdin handle is in
:: a certain state, running python.exe might hang here. To work around this, if
Expand Down Expand Up @@ -47,16 +71,16 @@
)

:NORMAL_EXIT
@"%EM_PY%" -E "%~dp0\%~n0.py" %*
@"%EM_PY%" -E "%MYDIR%%~n0.py" %*
@exit %ERRORLEVEL%

:MUTE_STDIN
@"%EM_PY%" -E "%~dp0\%~n0.py" %* < NUL
@"%EM_PY%" -E "%MYDIR%%~n0.py" %* < NUL
@exit /b %ERRORLEVEL%

:MUTE_STDIN_EXIT
@"%EM_PY%" -E "%~dp0\%~n0.py" %* < NUL
@"%EM_PY%" -E "%MYDIR%%~n0.py" %* < NUL
@exit %ERRORLEVEL%

:NORMAL
@"%EM_PY%" -E "%~dp0\%~n0.py" %*
@"%EM_PY%" -E "%MYDIR%%~n0.py" %*
32 changes: 28 additions & 4 deletions embuilder.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
:: To make modifications to this file, edit `tools/run_python.bat` and then run
:: `tools/create_entry_points.py`

:: N.b. In Windows .bat scripts, the ':' character cannot appear inside any if () blocks,
:: or there will be a parsing error.

:: All env. vars specified in this file are to be local only to this script.
@setlocal
:: -E will not ignore _PYTHON_SYSCONFIGDATA_NAME an internal
Expand All @@ -15,6 +18,27 @@
set EM_PY=python
)

:: Work around Windows bug https://github.com/microsoft/terminal/issues/15212 : If this
:: script is invoked via enclosing the invocation in quotes via PATH lookup, then %~f0 and
:: %~dp0 expansions will not work.
:: So first try if %~dp0 might work, and if not, manually look up this script from PATH.
@if exist %~f0 (
set MYDIR=%~dp0
goto FOUND_MYDIR
)
@for %%I in (%~n0.bat) do (
@if exist %%~$PATH:I (
set MYDIR=%%~dp$PATH:I
) else (
echo Fatal Error! Due to a Windows bug, we are unable to locate the path to %~n0.bat.
echo To help this issue, try removing unnecessary quotes in the invocation of emcc,
echo or add Emscripten directory to PATH.
echo See github.com/microsoft/terminal/issues/15212 and
echo github.com/emscripten-core/emscripten/issues/19207 for more details.
)
)
:FOUND_MYDIR

:: Python Windows bug https://bugs.python.org/issue34780: If this script was invoked via a
:: shared stdin handle from the parent process, and that parent process stdin handle is in
:: a certain state, running python.exe might hang here. To work around this, if
Expand Down Expand Up @@ -47,16 +71,16 @@
)

:NORMAL_EXIT
@"%EM_PY%" -E "%~dp0\%~n0.py" %*
@"%EM_PY%" -E "%MYDIR%%~n0.py" %*
@exit %ERRORLEVEL%

:MUTE_STDIN
@"%EM_PY%" -E "%~dp0\%~n0.py" %* < NUL
@"%EM_PY%" -E "%MYDIR%%~n0.py" %* < NUL
@exit /b %ERRORLEVEL%

:MUTE_STDIN_EXIT
@"%EM_PY%" -E "%~dp0\%~n0.py" %* < NUL
@"%EM_PY%" -E "%MYDIR%%~n0.py" %* < NUL
@exit %ERRORLEVEL%

:NORMAL
@"%EM_PY%" -E "%~dp0\%~n0.py" %*
@"%EM_PY%" -E "%MYDIR%%~n0.py" %*
28 changes: 26 additions & 2 deletions emcc.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
:: To make modifications to this file, edit `tools/run_python_compiler.bat` and
:: then run `tools/create_entry_points.py`

:: N.b. In Windows .bat scripts, the ':' character cannot appear inside any if () blocks,
:: or there will be a parsing error.

:: All env. vars specified in this file are to be local only to this script.
@setlocal
:: -E will not ignore _PYTHON_SYSCONFIGDATA_NAME an internal
Expand All @@ -15,13 +18,34 @@
set EM_PY=python
)

:: Work around Windows bug https://github.com/microsoft/terminal/issues/15212 : If this
:: script is invoked via enclosing the invocation in quotes via PATH lookup, then %~f0 and
:: %~dp0 expansions will not work.
:: So first try if %~dp0 might work, and if not, manually look up this script from PATH.
@if exist %~f0 (
set MYDIR=%~dp0
goto FOUND_MYDIR
)
@for %%I in (%~n0.bat) do (
@if exist %%~$PATH:I (
set MYDIR=%%~dp$PATH:I
) else (
echo Fatal Error! Due to a Windows bug, we are unable to locate the path to %~n0.bat.
echo To help this issue, try removing unnecessary quotes in the invocation of emcc,
echo or add Emscripten directory to PATH.
echo See github.com/microsoft/terminal/issues/15212 and
echo github.com/emscripten-core/emscripten/issues/19207 for more details.
)
)
:FOUND_MYDIR

:: If _EMCC_CCACHE is not set, do a regular invocation of the python compiler driver.
:: Otherwise remove the ccache env. var, and then reinvoke this script with ccache enabled.
@if "%_EMCC_CCACHE%"=="" (
set CMD="%EM_PY%" -E "%~dp0\%~n0.py"
set CMD="%EM_PY%" -E "%MYDIR%%~n0.py"
) else (
set _EMCC_CCACHE=
set CMD=ccache "%~dp0\%~n0.bat"
set CMD=ccache "%MYDIR%%~n0.bat"
)

:: Python Windows bug https://bugs.python.org/issue34780: If this script was invoked via a
Expand Down
32 changes: 28 additions & 4 deletions emcmake.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
:: To make modifications to this file, edit `tools/run_python.bat` and then run
:: `tools/create_entry_points.py`

:: N.b. In Windows .bat scripts, the ':' character cannot appear inside any if () blocks,
:: or there will be a parsing error.

:: All env. vars specified in this file are to be local only to this script.
@setlocal
:: -E will not ignore _PYTHON_SYSCONFIGDATA_NAME an internal
Expand All @@ -15,6 +18,27 @@
set EM_PY=python
)

:: Work around Windows bug https://github.com/microsoft/terminal/issues/15212 : If this
:: script is invoked via enclosing the invocation in quotes via PATH lookup, then %~f0 and
:: %~dp0 expansions will not work.
:: So first try if %~dp0 might work, and if not, manually look up this script from PATH.
@if exist %~f0 (
set MYDIR=%~dp0
goto FOUND_MYDIR
)
@for %%I in (%~n0.bat) do (
@if exist %%~$PATH:I (
set MYDIR=%%~dp$PATH:I
) else (
echo Fatal Error! Due to a Windows bug, we are unable to locate the path to %~n0.bat.
echo To help this issue, try removing unnecessary quotes in the invocation of emcc,
echo or add Emscripten directory to PATH.
echo See github.com/microsoft/terminal/issues/15212 and
echo github.com/emscripten-core/emscripten/issues/19207 for more details.
)
)
:FOUND_MYDIR

:: Python Windows bug https://bugs.python.org/issue34780: If this script was invoked via a
:: shared stdin handle from the parent process, and that parent process stdin handle is in
:: a certain state, running python.exe might hang here. To work around this, if
Expand Down Expand Up @@ -47,16 +71,16 @@
)

:NORMAL_EXIT
@"%EM_PY%" -E "%~dp0\%~n0.py" %*
@"%EM_PY%" -E "%MYDIR%%~n0.py" %*
@exit %ERRORLEVEL%

:MUTE_STDIN
@"%EM_PY%" -E "%~dp0\%~n0.py" %* < NUL
@"%EM_PY%" -E "%MYDIR%%~n0.py" %* < NUL
@exit /b %ERRORLEVEL%

:MUTE_STDIN_EXIT
@"%EM_PY%" -E "%~dp0\%~n0.py" %* < NUL
@"%EM_PY%" -E "%MYDIR%%~n0.py" %* < NUL
@exit %ERRORLEVEL%

:NORMAL
@"%EM_PY%" -E "%~dp0\%~n0.py" %*
@"%EM_PY%" -E "%MYDIR%%~n0.py" %*
32 changes: 28 additions & 4 deletions emconfigure.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
:: To make modifications to this file, edit `tools/run_python.bat` and then run
:: `tools/create_entry_points.py`

:: N.b. In Windows .bat scripts, the ':' character cannot appear inside any if () blocks,
:: or there will be a parsing error.

:: All env. vars specified in this file are to be local only to this script.
@setlocal
:: -E will not ignore _PYTHON_SYSCONFIGDATA_NAME an internal
Expand All @@ -15,6 +18,27 @@
set EM_PY=python
)

:: Work around Windows bug https://github.com/microsoft/terminal/issues/15212 : If this
:: script is invoked via enclosing the invocation in quotes via PATH lookup, then %~f0 and
:: %~dp0 expansions will not work.
:: So first try if %~dp0 might work, and if not, manually look up this script from PATH.
@if exist %~f0 (
set MYDIR=%~dp0
goto FOUND_MYDIR
)
@for %%I in (%~n0.bat) do (
@if exist %%~$PATH:I (
set MYDIR=%%~dp$PATH:I
) else (
echo Fatal Error! Due to a Windows bug, we are unable to locate the path to %~n0.bat.
echo To help this issue, try removing unnecessary quotes in the invocation of emcc,
echo or add Emscripten directory to PATH.
echo See github.com/microsoft/terminal/issues/15212 and
echo github.com/emscripten-core/emscripten/issues/19207 for more details.
)
)
:FOUND_MYDIR

:: Python Windows bug https://bugs.python.org/issue34780: If this script was invoked via a
:: shared stdin handle from the parent process, and that parent process stdin handle is in
:: a certain state, running python.exe might hang here. To work around this, if
Expand Down Expand Up @@ -47,16 +71,16 @@
)

:NORMAL_EXIT
@"%EM_PY%" -E "%~dp0\%~n0.py" %*
@"%EM_PY%" -E "%MYDIR%%~n0.py" %*
@exit %ERRORLEVEL%

:MUTE_STDIN
@"%EM_PY%" -E "%~dp0\%~n0.py" %* < NUL
@"%EM_PY%" -E "%MYDIR%%~n0.py" %* < NUL
@exit /b %ERRORLEVEL%

:MUTE_STDIN_EXIT
@"%EM_PY%" -E "%~dp0\%~n0.py" %* < NUL
@"%EM_PY%" -E "%MYDIR%%~n0.py" %* < NUL
@exit %ERRORLEVEL%

:NORMAL
@"%EM_PY%" -E "%~dp0\%~n0.py" %*
@"%EM_PY%" -E "%MYDIR%%~n0.py" %*
Loading

0 comments on commit e8535a8

Please sign in to comment.