Got An Error When Run Golangci-lint In Workspace Root Dir

by ADMIN 58 views

Welcome

We are glad you are reaching out for help with the golangci-lint issue. Before we dive into the problem, please make sure you have:

  • Installed the latest binary release of golangci-lint from the official GitHub releases page.
  • Searched for similar issues on the GitHub page and didn't find any.
  • Read the typecheck section of the FAQ page.
  • Tried running the standalone linter if available (e.g., gocritic, go vet, etc.).

How did you install golangci-lint?

You installed golangci-lint using Scoop.

Description of the problem

When you run golangci-lint run in the workspace root directory, you encounter the following error:

PS F:\go_projects\workspace_demo> golangci-lint run
level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain modules listed in go.work or their selected dependencies"
0 issues.

However, if you run golangci-lint run module_a/..., the command executes successfully.

Version of golangci-lint

You are using version 2.1.6 of golangci-lint, which was built with go1.24.2 from eabc2638 on 2025-05-04T15:41:19Z.

Configuration

You are not using any configuration files.

Go environment

Your Go environment is set up as follows:

PS F:\go_projects\workspace_demo> go version
go version go1.24.2 windows/amd64
PS F:\go_projects\workspace_demo> go env
set AR=ar
set CC=gcc
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_ENABLED=0
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set CXX=g++
set GCCGO=gccgo
set GO111MODULE=
set GOAMD64=v1
set GOARCH=amd64
set GOAUTH=netrc
set GOBIN=
set GOCACHE=C:\Users\Wu Jiayang\AppData\Local\go-build
set GOCACHEPROG=
set GODEBUG=
set GOENV=C:\Users\Wu Jiayang\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFIPS140=off
set GOFLAGS=
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\WUJIAY~1\AppData\Local\Temp\go-build2472287614=/tmp/go-build -gno-record-gcc-switches
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMOD=NUL
set GOMODCACHE=C:\Users\Wu Jiayang\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windowsset GOPATH=C:\Users\Wu Jiayang\go
set GOPRIVATE=
set GOPROXY=https://goproxy.io,direct
set GOROOT=F:\Go
set GOSUMDB=sum.golang.org
set GOTELEMETRY=local
set GOTELEMETRYDIR=C:\Users\Wu Jiayang\AppData\Roaming\go\telemetry
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=F:\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.24.2
set GOWORK=F:\go_projects\workspace_demo\go.work
set PKG_CONFIG=pkg-config

Verbose output of running

The verbose output of running golangci-lint run -v is as follows:

PS F:\go_projects\workspace_demo> golangci-lint cache clean
PS F:\go_projects\workspace_demo> golangci-lint run -v
level=info msg="golangci-lint has version 2.1.6 built with go1.24.2 from eabc2638 on 2025-05-04T15:41:19Z"
level=info msg="[config_reader] Config search paths: [./ F:\\go_projects\\workspace_demo F:\\go_projects F:\\ C:\\Users\\Wu Jiayang]"
level=info msg="maxprocs: Leaving GOMAXPROCS=12: CPU quota undefined"
level=info msg="[goenv] Read go env for 133.0517ms: map[string]string{\"GOCACHE\":\"C:\\\\Users\\\\Wu Jiayang\\\\AppData\\\\Local\\\\go-build\", \"GOROOT\":\"F:\\\\Go\"}"
level=info msg="[lintersdb] Active 5 linters: [errcheck govet ineffassign staticcheck unused]"
level=info msg="[loader] Go packages loading at mode 8767 (files|imports|compiled_files|deps|name|types_sizes|exports_file) took 386.0732ms"
level=info msg="[runner/filename_unadjuster] Pre-built 0 adjustments in 0s"
level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain modules listed in go.work or their selected dependencies"
level=info msg="[linters_context/goanalysis] analyzers took 2.105ms with top 10 stages: buildir: 1.0525ms, fact_deprecated: 1.0525ms, directives: 0s, SA5004: 0s, SA3001: 0s, ST1001: 0s, ineffassign: 0s, SA5012: 0s, SA1025: 0s, SA4006: 0s"
level=info msg="[runner] processing took 0s with stages: invalid_issue: 0s, path_relativity: 0s, severity-rules: 0s, exclusion_paths: 0s, nolint_filter: 0s, max_per_file_from_linter: 0s, max_same_issues: 0s, source_code: 0s, sort_results: 0s, path_absoluter: 0s, filename_unadjuster: 0s, generated_file_filter: 0s, fixer: 0s, max_from_linter 0s, path_shortener: 0s, path_prettifier: 0s, exclusion_rules: 0s, diff: 0s, uniq_by_line: 0s, cgo: 0s"
level=info msg="[runner] linters took 2.627ms with stages: goanalysis_metalinter: 2.627ms"
0 issues.
level=info msg="File cache stats: 0 entries of total size 0B"
level=info msg="Memory: 7 samples, avg is 36.4MB, max is 43.5MB"
level=info msg="Execution took 522.8042ms"

A minimal reproducible example or link to a public repository

You can find a minimal reproducible example in the following repository:

https://github.com/Wu-Jiayang/workspace_demo

Validation

You have included all the necessary information above.

Supporter

You are not a sponsor or backer of the golangci-lint project.

Possible Solutions

Based on the provided information, it seems that the issue is related to the way golangci-lint is handling the go.work file. The error message suggests that the directory prefix . does not contain modules listed in the go.work file or their selected dependencies.

Here are a few possible solutions to this issue:

  1. Check the go.work file: Make sure that the go.work file is correctly configured and that it includes all the modules that are present in the workspace.
  2. Use a different pattern: Instead of using the ./... pattern, try using a different pattern that includes all the modules in the workspace. For example, you can use module_a/... module_b/... to include all the modules in the module_a and module_b directories.
  3. Disable type checking: If you are not concerned about type checking, you can disable it by adding the --no-typecheck flag when running golangci-lint. This will skip the type checking step and may resolve the issue.
  4. Update golangci-lint: Make sure that you are using the latest version of golangci-lint. You can update it by running go get -u github.com/golangci/golangci-lint/cmd/golangci-lint.

Conclusion

Q: What is the error message I'm seeing when I run golangci-lint in the workspace root dir?

A: The error message you're seeing is:

level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain modules listed in go.work or their selected dependencies"
0 issues.

Q: What does this error message mean?

A: This error message means that golangci-lint is unable to find the modules listed in the go.work file or their selected dependencies in the current directory. The go.work file is used to manage multiple Go modules in a single workspace.

Q: How can I resolve this issue?

A: There are several possible solutions to this issue:

  1. Check the go.work file: Make sure that the go.work file is correctly configured and that it includes all the modules that are present in the workspace.
  2. Use a different pattern: Instead of using the ./... pattern, try using a different pattern that includes all the modules in the workspace. For example, you can use module_a/... module_b/... to include all the modules in the module_a and module_b directories.
  3. Disable type checking: If you are not concerned about type checking, you can disable it by adding the --no-typecheck flag when running golangci-lint. This will skip the type checking step and may resolve the issue.
  4. Update golangci-lint: Make sure that you are using the latest version of golangci-lint. You can update it by running go get -u github.com/golangci/golangci-lint/cmd/golangci-lint.

Q: What is the go.work file?

A: The go.work file is a file that is used to manage multiple Go modules in a single workspace. It is used to specify the modules that are included in the workspace and their dependencies.

Q: How do I create a go.work file?

A: To create a go.work file, you can use the following command:

go work init

This will create a new go.work file in the current directory.

Q: What are the benefits of using a go.work file?

A: Using a go.work file has several benefits, including:

  • Improved code organization: A go.work file helps to organize your code by specifying the modules that are included in the workspace and their dependencies.
  • Easier dependency management: A go.work file makes it easier to manage dependencies by specifying the modules that are included in the workspace and their dependencies.
  • Improved build performance: A go.work file can improve build performance by reducing the number of modules that need to be built.

Q: How do I specify modules in a go.work file?

A: To specify modules in a go.work file, you can use the following syntax:

go1.17
require (
    module_a v1.0.0
    module_b v2.0.0
)

This specifies that the module_a and module_b modules are included in the workspace and their versions are v1.0.0 and v2.0.0, respectively.

Q: How do I specify dependencies in a go.work file?

A: To specify dependencies in a go.work file, you can use the following syntax:

go 1.17
require (
    module_a v1.0.0
    module_b v2.0.0
    module_c v3.0.0
)

This specifies that the module_a, module_b, and module_c modules are included in the workspace and their versions are v1.0.0, v2.0.0, and v3.0.0, respectively.

Q: How do I update a go.work file?

A: To update a go.work file, you can use the following command:

go work edit

This will open the go.work file in the default editor.

Q: How do I delete a go.work file?

A: To delete a go.work file, you can use the following command:

go work delete

This will delete the go.work file.

Q: What are the best practices for using a go.work file?

A: Here are some best practices for using a go.work file:

  • Use a consistent naming convention: Use a consistent naming convention for your modules and dependencies.
  • Use a consistent versioning scheme: Use a consistent versioning scheme for your modules and dependencies.
  • Use a go.work file for each workspace: Use a go.work file for each workspace to manage multiple Go modules.
  • Use a go.work file to specify dependencies: Use a go.work file to specify dependencies for your modules.
  • Use a go.work file to manage multiple Go modules: Use a go.work file to manage multiple Go modules in a single workspace.