Skip to content

cmd.WithContext undefined error on Go 1.21+, 1.23+, 1.24+ across multiple environments #73520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
yilingwoo opened this issue Apr 28, 2025 · 2 comments
Labels
BugReport Issues describing a possible bug in the Go implementation.

Comments

@yilingwoo
Copy link

yilingwoo commented Apr 28, 2025

Go version

Go 1.21+, 1.23+, 1.24+ across multiple environments

Output of go env in your module/workspace:

Environment 1 (Windows 10):

OS: Windows 10 Insider Preview Build 10.0.26120.2705
Go Version(s) Tested: go1.24.2 windows/amd64 (MSI install, zip extract), go1.21.1 windows/amd64 (installation method not explicitly stated, likely MSI)
Relevant go env for Go 1.24.2 (MSI default path): (Please include your go env output for the Go 1.24.2 MSI install if you still have it, otherwise the 1.21.1 is below)
Relevant go env for Go 1.21.1:
set GO111MODULE=on
set GOARCH=amd64
... (other variables) ...
set GOROOT=D:\Go
... (other variables) ...
set GOVERSION=go1.21.1
... (other variables) ...
where go output (for Go 1.24.2 MSI): C:\Program Files\Go\bin\go.exe
where go output (for Go 1.21.1): (Please provide if different from the 1.24.2 path)
Environment 2 (Archlinux):

OS: Archlinux (specific version/kernel not provided)
Go Version Tested: go1.23.4 linux/amd64 (installation method not explicitly stated, likely package manager or manual)
Relevant go env:
GOARCH='amd64'
... (other variables) ...
GOROOT='/home/enbo/tools/go'
... (other variables) ...
GOOS='linux'
... (other variables) ...
GOVERSION='go1.23.4'
... (other variables) ...
which go output: (Please provide if available)
Environment 3 (Google Cloud Linux):

OS: Google Cloud Linux instance (specific distribution/version/kernel not provided)
Go Version Tested: go1.24.2 linux/amd64 (installation method not explicitly stated, likely standard cloud image/package)
Relevant go env:
GOARCH='amd64'
... (other variables) ...
GOROOT='/usr/local/go'
... (other variables) ...
GOOS='linux'
... (other variables) ...
GOVERSION='go1.24.2'
... (other variables) ...
which go output: (Please provide if available)

What did you do?

I am encountering a persistent and unusual compilation error: cmd2.WithContext undefined (type *exec.Cmd has no field or method WithContext) when building a simple Go program that uses exec.CommandContext and cmd.WithContext. This error occurs despite using Go versions 1.21.1, 1.23.4, and 1.24.2, all of which should include the WithContext method (introduced in Go 1.7).

The error appears consistently across three different environments:

Windows 10 Insider Preview Build (10.0.26120.2705)
Archlinux
Google Cloud Linux instance
This issue persists even after extensive troubleshooting, suggesting a problem outside of standard Go configuration.

Expected Behavior:

The test_context.go program should compile successfully on Go versions 1.7 and later, as exec.CommandContext and cmd.WithContext are standard features.

Actual Behavior:

Compilation fails with the error cmd2.WithContext undefined (type *exec.Cmd has no field or method WithContext).

Steps to Reproduce:

Save the following Go code as test_context.go:

Go

package main

import (
	"context"
	"errors"
	"log"
	"os/exec"
	"time"
)

func main() {
	log.Println("Testing exec.CommandContext...")

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	cmd := exec.CommandContext(ctx, "echo", "hello from CommandContext")

	output, err := cmd.CombinedOutput()

	if err != nil {
		if errors.Is(ctx.Err(), context.DeadlineExceeded) {
			log.Printf("Command timed out: %v", err)
		} else {
			log.Printf("Command failed: %v", err)
		}
        if len(output) > 0 {
             log.Printf("Command Output: %s", output)
        } else {
             log.Println("Command produced no output.")
        }
	} else {
		log.Printf("Command successful: %s", output)
	}


    log.Println("\nTesting explicit cmd.WithContext...")
    ctx2, cancel2 := context.WithTimeout(context.Background(), 5*time.Second)
    defer cancel2()

    cmd2 := exec.Command("echo", "hello from explicit context")
    cmd2 = cmd2.WithContext(ctx2) // <-- Error points near this line

    output2, err2 := cmd2.CombinedOutput()
    if err2 != nil {
         if errors.Is(ctx2.Err(), context.DeadlineExceeded) {
             log.Printf("Explicit context command timed out: %v", err2)
         } else {
             log.Printf("Explicit context command failed: %v", err2)
         }
          if len(output2) > 0 {
             log.Printf("Explicit Context Command Output: %s", output2)
          } else {
             log.Println("Explicit context command produced no output.")
          }
    } else {
        log.Printf("Explicit context command successful: %s", output2)
    }
}

Open a terminal in the same directory as test_context.go.

Run the build command: go build test_context.go

Observe the compilation error.

Environment Details:

The issue occurred on three different machines/environments:

Environment 1 (Windows 10):

OS: Windows 10 Insider Preview Build 10.0.26120.2705
Go Version(s) Tested: go1.24.2 windows/amd64 (MSI install, zip extract), go1.21.1 windows/amd64 (installation method not explicitly stated, likely MSI)
Relevant go env for Go 1.24.2 (MSI default path): (Please include your go env output for the Go 1.24.2 MSI install if you still have it, otherwise the 1.21.1 is below)
Relevant go env for Go 1.21.1:
set GO111MODULE=on
set GOARCH=amd64
... (other variables) ...
set GOROOT=D:\Go
... (other variables) ...
set GOVERSION=go1.21.1
... (other variables) ...
where go output (for Go 1.24.2 MSI): C:\Program Files\Go\bin\go.exe
where go output (for Go 1.21.1): (Please provide if different from the 1.24.2 path)

Environment 2 (Archlinux):

OS: Archlinux (specific version/kernel not provided)
Go Version Tested: go1.23.4 linux/amd64 (installation method not explicitly stated, likely package manager or manual)
Relevant go env:
GOARCH='amd64'
... (other variables) ...
GOROOT='/home/enbo/tools/go'
... (other variables) ...
GOOS='linux'
... (other variables) ...
GOVERSION='go1.23.4'
... (other variables) ...
which go output: (Please provide if available)

Environment 3 (Google Cloud Linux):

OS: Google Cloud Linux instance (specific distribution/version/kernel not provided)
Go Version Tested: go1.24.2 linux/amd64 (installation method not explicitly stated, likely standard cloud image/package)
Relevant go env:
GOARCH='amd64'
... (other variables) ...
GOROOT='/usr/local/go'
... (other variables) ...
GOOS='linux'
... (other variables) ...
GOVERSION='go1.24.2'
... (other variables) ...
which go output: (Please provide if available)

Troubleshooting Steps Performed:

Verified Go version using go version.
Verified Go executable path using where go (Windows) / which go (Linux).
Verified GOROOT and GOPATH environment variables using go env.
Attempted clean reinstallation of Go (MSI uninstall + manual folder deletion + MSI reinstall; MSI uninstall + manual folder deletion + zip extraction + manual path/GOROOT setup) on the Windows machine.
Tested compilation on two other distinct machines/OS environments (Archlinux, Google Cloud Linux).
Attempted caching cleanup (go clean -cache, go clean --modcache).
Tested compilation of a simpler program (minimal.go) which defines and calls a custom method, confirming basic method calls work. minimal.go compiled and ran successfully on the Windows machine.
Identified and corrected a syntax error in the test_context.go code, but the cmd.WithContext undefined error persisted after fixing the syntax.
Attempted compiling test_context.go after manually typing the code to rule out subtle character issues (result was the same error).
Compiler Output:

Please include the exact output of the go build test_context.go command for each environment/version tested where the error occurred. Example:

command-line-arguments

.\test_context.go:XX:YY: cmd2.WithContext undefined (type *exec.Cmd has no field or method WithContext)
(Replace XX and YY with the specific line/column numbers from the output).

Notes:

The consistent failure with this specific error across multiple, seemingly correctly configured, recent Go environments is highly unusual and difficult to explain with standard Go troubleshooting. It suggests a potential issue with the distribution files for os/exec and context, or a very specific interaction bug in the Go toolchain triggered by these packages, which is consistently present or triggered in these diverse environments.

What did you see happen?

command-line-arguments

.\test_context.go:XX:YY: cmd2.WithContext undefined (type *exec.Cmd has no field or method WithContext)
(Replace XX and YY with the specific line/column numbers from the output).

What did you expect to see?

command-line-arguments

.\test_context.go:XX:YY: cmd2.WithContext undefined (type *exec.Cmd has no field or method WithContext)
(Replace XX and YY with the specific line/column numbers from the output).

@seankhliao
Copy link
Member

exec.Cmd never had a WithContext method: https://pkg.go.dev/os/[email protected]#Cmd

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Apr 28, 2025
@gabyhelp gabyhelp added the BugReport Issues describing a possible bug in the Go implementation. label Apr 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BugReport Issues describing a possible bug in the Go implementation.
Projects
None yet
Development

No branches or pull requests

3 participants