Skip to content

Commit 8c7a99a

Browse files
fix(shims): resolve Windows batch file syntax error in generated shims
Fixes #555 The Windows .bat shim template contained problematic syntax that broke CMD batch file parsing: - goto/label (:found_file) inside parenthesized if (...) block - Direct %* expansion inside nested for (...) do (...) block These caused 'was unexpected at this time' errors when running any Go command through the shims on Windows. Solution: - Move file argument detection to a safe subroutine (:detect_file_arg) - Call subroutine using 'call' instead of inline goto/label - Add explicit exit code propagation with exit /b %ERRORLEVEL% This matches the safer approach suggested in the issue report and ensures shims work correctly in both cmd.exe and PowerShell.
1 parent 48da144 commit 8c7a99a

1 file changed

Lines changed: 16 additions & 13 deletions

File tree

internal/shims/manager.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -237,27 +237,19 @@ func (s *ShimManager) createWindowsShim(binaryName string) error {
237237
shimPath := filepath.Join(s.config.ShimsDir(), binaryName+".bat")
238238

239239
// Create the batch file shim
240+
// Note: Uses a subroutine for file arg detection to avoid goto/label issues
241+
// inside parenthesized blocks, which break CMD batch file parsing.
240242
shimContent := fmt.Sprintf(`@echo off
241243
REM goenv shim for %s
242244
setlocal
243245
244-
if "%%GOENV_DEBUG%%"=="1" (
245-
echo on
246-
)
246+
if "%%GOENV_DEBUG%%"=="1" echo on
247247
248248
REM Get the script name without path
249249
for %%%%I in ("%%~f0") do set "program=%%%%~nI"
250250
251-
REM For go commands, detect file arguments (simplified for batch)
252-
if "%%program:~0,2%%"=="go" (
253-
for %%%%a in (%%*) do (
254-
if exist "%%%%a" (
255-
set "GOENV_FILE_ARG=%%%%a"
256-
goto :found_file
257-
)
258-
)
259-
:found_file
260-
)
251+
REM For go commands, detect file arguments using safe subroutine call
252+
if "%%program:~0,2%%"=="go" call :detect_file_arg %%*
261253
262254
if "%%program%%"=="goenv" (
263255
if "%%1"=="" (
@@ -268,6 +260,17 @@ if "%%program%%"=="goenv" (
268260
) else (
269261
goenv exec "%%program%%" %%*
270262
)
263+
264+
exit /b %%ERRORLEVEL%%
265+
266+
:detect_file_arg
267+
if "%%~1"=="" exit /b 0
268+
if exist "%%~1" (
269+
set "GOENV_FILE_ARG=%%~1"
270+
exit /b 0
271+
)
272+
shift
273+
goto :detect_file_arg
271274
`, binaryName)
272275

273276
if err := utils.WriteFileWithContext(shimPath, []byte(shimContent), 0666, "write shim file"); err != nil {

0 commit comments

Comments
 (0)