Skip to content

"Run/Debug Test At Cursor" generates incorrect -run regex for deeply nested t.Run subtests #3871

@tbhaxor

Description

@tbhaxor

What version of Go, VS Code & VS Code Go extension are you using?

Version Information
  • Run go version to get version of Go from the VS Code integrated terminal.
    • go1.25.1 darwin/arm64
  • Run gopls -v version to get version of Gopls from the VS Code integrated terminal.
    • v0.20.0
  • Run code -v or code-insiders -v to get version of VS Code or VS Code Insiders.
    • 1.104
  • Check your installed extensions to get the version of the VS Code Go extension
    • 0.50.0
  • Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > Go: Locate Configured Go Tools command.
# Tools Configuration


## Environment

GOBIN: undefined
toolsGopath: 
gopath: /Users/terabyte/go
GOROOT: /opt/homebrew/Cellar/go/1.25.1/libexec
PATH: /opt/homebrew/opt/node@22/bin:/opt/homebrew/opt/node@20/bin:/opt/homebrew/share/google-cloud-sdk/bin:/opt/clang+llvm-18.1.8-arm64-apple-macos11/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/usr/local/MacGPG2/bin:/Applications/Wireshark.app/Contents/MacOS:/Applications/VMware Fusion Tech Preview.app/Contents/Public:/usr/local/share/dotnet:~/.dotnet/tools:/usr/local/go/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Users/terabyte/.cargo/bin:/Users/terabyte/Library/pnpm

## Tools

	go:	/opt/homebrew/bin/go: go version go1.25.1 darwin/arm64

	gopls:	/Users/terabyte/go/bin/gopls	(version: v0.20.0 built with go: go1.25.1)
	gotests:	/Users/terabyte/go/bin/gotests	(version: v1.6.0 built with go: go1.25.1)
	impl:	/Users/terabyte/go/bin/impl	(version: v1.4.0 built with go: go1.25.1)
	goplay:	/Users/terabyte/go/bin/goplay	(version: v1.0.0 built with go: go1.25.1)
	dlv:	/Users/terabyte/go/bin/dlv	(version: v1.25.2 built with go: go1.25.1)
	staticcheck:	/Users/terabyte/go/bin/staticcheck	(version: v0.6.1 built with go: go1.25.1)

## Go env

Workspace Folder (gofsx): /Users/terabyte/Desktop/gofsx

	AR='ar'
	CC='cc'
	CGO_CFLAGS='-O2 -g'
	CGO_CPPFLAGS=''
	CGO_CXXFLAGS='-O2 -g'
	CGO_ENABLED='1'
	CGO_FFLAGS='-O2 -g'
	CGO_LDFLAGS='-O2 -g'
	CXX='c++'
	GCCGO='gccgo'
	GO111MODULE=''
	GOARCH='arm64'
	GOARM64='v8.0'
	GOAUTH='netrc'
	GOBIN=''
	GOCACHE='/Users/terabyte/Library/Caches/go-build'
	GOCACHEPROG=''
	GODEBUG=''
	GOENV='/Users/terabyte/Library/Application Support/go/env'
	GOEXE=''
	GOEXPERIMENT=''
	GOFIPS140='off'
	GOFLAGS=''
	GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/p3/4mkwjl_93v54vgvhsdjg4_kh0000gn/T/go-build24019865=/tmp/go-build -gno-record-gcc-switches -fno-common'
	GOHOSTARCH='arm64'
	GOHOSTOS='darwin'
	GOINSECURE=''
	GOMOD='/Users/terabyte/Desktop/gofsx/go.mod'
	GOMODCACHE='/Users/terabyte/go/pkg/mod'
	GONOPROXY=''
	GONOSUMDB=''
	GOOS='darwin'
	GOPATH='/Users/terabyte/go'
	GOPRIVATE=''
	GOPROXY='https://proxy.golang.org,direct'
	GOROOT='/opt/homebrew/Cellar/go/1.25.1/libexec'
	GOSUMDB='sum.golang.org'
	GOTELEMETRY='local'
	GOTELEMETRYDIR='/Users/terabyte/Library/Application Support/go/telemetry'
	GOTMPDIR=''
	GOTOOLCHAIN='auto'
	GOTOOLDIR='/opt/homebrew/Cellar/go/1.25.1/libexec/pkg/tool/darwin_arm64'
	GOVCS=''
	GOVERSION='go1.25.1'
	GOWORK=''
	PKG_CONFIG='pkg-config'
	

Share the Go related settings you have added/edited

Run Preferences: Open Settings (JSON) command to open your settings.json file.
Share all the settings with the go. or ["go"] or gopls prefixes.

"go.testFlags": [
    "-v"
  ]

Describe the bug

When using the t.Run feature to nest tests three levels deep, both the CodeLens links ("run test" | "debug test") and the Command Palette option ("Go: Run Test At Cursor") fail to generate the correct -run regular expression.

The command either generates a malformed regex or, more commonly, completely ignores the nested path and attempts to run the wrong top-level test function from the file.

Steps to reproduce the behavior:

  1. Create a new Go project and add a single file named adapters_test.go.
  2. Paste the following minimal, reproducible code into the file:
package adapters_test

import "testing"

// This is the test function we actually want to run a sub-test from.
func TestS3Module(t *testing.T) {
	t.Run("KeyExists", func(t *testing.T) {

		t.Run("should return ok for existing key", func(t *testing.T) {
			// ... sample logic
		})

		// --- THIS IS THE TARGET TEST ---
		// Place cursor inside this block
		t.Run("should return error for non existing key", func(t *testing.T) {
			t.Log("This is the test that should be running!")
			if false {
				t.Errorf("test failed")
			}
		})
		// ---------------------------------

	})
}
  1. Place your text cursor inside the t.Run("should return error for non existing key", ...) block.
  2. Open the Command Palette (Cmd+Shift+P or Ctrl+Shift+P) and select "Go: Run Test At Cursor".

Screenshots or recordings

Image

Note

The correct argument to -run command should be ^TestS3Module/KeyExists/should_return_error_for_non_existing_key$.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions