Skip to content

x/tools/gopls: "diagnostic location is not among files of package: %s" assertion is unjustified for Diagnostic.Related #70791

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
adonovan opened this issue Dec 11, 2024 · 6 comments
Assignees
Labels
gopls/telemetry-wins gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@adonovan
Copy link
Member

adonovan commented Dec 11, 2024

#!stacks
"bug.Errorf" && "cache.(*action).exec.func2:+48"

Issue created by stacks.

if mapper == nil {
			// The start position was not among the package's parsed
			// Go files, indicating that the analyzer added new files
			// to the FileSet.
			//
			// For example, the cgocall analyzer re-parses and
			// type-checks some of the files in a special environment;
			// and asmdecl and other low-level runtime analyzers call
			// ReadFile to parse non-Go files.
			// (This is a supported feature, documented at go/analysis.)
			//
			// In principle these files could be:
			//
			// - OtherFiles (non-Go files such as asm).
			//   However, we set Pass.OtherFiles=[] because
			//   gopls won't service "diagnose" requests
			//   for non-Go files, so there's no point
			//   reporting diagnostics in them.
			//
			// - IgnoredFiles (files tagged for other configs).
			//   However, we set Pass.IgnoredFiles=[] because,
			//   in most cases, zero-config gopls should create
			//   another view that covers these files.
			//
			// - Referents of //line directives, as in cgo packages.
			//   The file names in this case are not known a priori.
			//   gopls generally tries to avoid honoring line directives,
			//   but analyzers such as cgocall may honor them.
			//
			// In short, it's unclear how this can be reached
			// other than due to an analyzer bug.
			return protocol.Location{}, bug.Errorf("diagnostic location is not among files of package: %s", tokFile.Name())
}

The analyzer that reported the diagnostic is staticcheck's SA4023, but all its calls to Report look fine.

This stack dlmz3w was reported by telemetry:

golang.org/x/tools/[email protected] go1.23.2 linux/amd64 vscode (1)

Dups: ARI9LQ xuqd0A UM4FBg

@adonovan adonovan added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository. gopls/telemetry-wins labels Dec 11, 2024
@gopherbot gopherbot added this to the Unreleased milestone Dec 11, 2024
@adonovan
Copy link
Member Author

adonovan commented Apr 23, 2025

Ah! The stack frame in toGobDiagnostic clearly implicates the Related field of Diagnostic, and sure enough, SA4023 supplies Related information, one element of which uses the position of a *types.Func from a dependency, so it is unsurprising that the assertion fails to find the location among the source files of the current package. I think SA4023 is unimpeachable: the go/analysis API doesn't say Related positions must be in the current package (as it does for SuggestedFixes), and it is natural that they would not be. So I think the assertion in gopls is simply wrong.

@adonovan adonovan changed the title x/tools/gopls: "diagnostic location is not among files of package: %s" bug (with analyzer SA4023) x/tools/gopls: "diagnostic location is not among files of package: %s" assertion is unjustified for Diagnostic.Related Apr 23, 2025
@adonovan adonovan self-assigned this Apr 23, 2025
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/667517 mentions this issue: gopls/internal/cache: fix bug in toGobDiagnostic(Related)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls/telemetry-wins gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants