Skip to content

[v3.1.1 Windows] generated .bat shims fail with ") was unexpected at this time" #555

Description

@Linkverba

Environment

  • OS: Windows 10

  • Shell: cmd.exe, PowerShell 7.6.2

  • goenv version: 3.1.1

Summary & Reproduction

On Windows, goenv .bat shims fail before invoking the selected Go binary.
Even though goenv current and where go resolve correctly, running any Go command (e.g., go version) fails with this CMD parser error:

) was unexpected at this time.

Generated go.bat

Here is the exact generated shim causing the issue:

@echo off
REM goenv shim for go
setlocal

if "%GOENV_DEBUG%"=="1" (
  echo on
)

REM Get the script name without path
for %%I in ("%~f0") do set "program=%%~nI"

REM For go commands, detect file arguments (simplified for batch)
if "%program:~0,2%"=="go" (
  for %%a in (%*) do (
    if exist "%%a" (
      set "GOENV_FILE_ARG=%%a"
      goto :found_file
    )
  )
  :found_file
)

if "%program%"=="goenv" (
  if "%1"=="" (
    echo goenv: no command specified >&2
    exit /b 1
  )
  goenv exec %*
) else (
  goenv exec "%program%" %*
)

Root Cause

The generated Windows shim script contains syntax that breaks CMD batch file parsing. Specifically, there are two issues in the if "%program:~0,2%"=="go" block:

  1. Placing a goto command and a label (:found_file) inside a parenthesized if (...) block.

  2. Direct raw %* expansion inside a nested for (...) do (...) block.

These practices are highly fragile in Windows batch files and immediately crash the script.

Suggested Fix

Avoid using goto/labels and expanding raw %* inside parenthesized batch blocks.

If GOENV_FILE_ARG detection is required, it should be implemented via a safe subroutine call (call :detect_file_arg) or moved entirely into goenv exec to keep the batch shims minimal.

Here is a safer minimal Windows shim example:

@echo off
setlocal

if "%GOENV_DEBUG%"=="1" echo on

for %%I in ("%~f0") do set "program=%%~nI"

if "%program:~0,2%"=="go" call :detect_file_arg %*

if "%program%"=="goenv" (
  if "%1"=="" (
    echo goenv: no command specified >&2
    exit /b 1
  )
  goenv exec %*
) else (
  goenv exec "%program%" %*
)

exit /b %ERRORLEVEL%

:detect_file_arg
if "%~1"=="" exit /b 0
if exist "%~1" (
  set "GOENV_FILE_ARG=%~1"
  exit /b 0
)
shift
goto :detect_file_arg

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions