Skip to content
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

go/packages: line directives not treatead properly in the exported file #71080

Open
mateusz834 opened this issue Jan 1, 2025 · 2 comments
Open
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@mateusz834
Copy link
Member

Reproducer (using the x/tools/go/packages testing API):

func TestReproducer(t *testing.T) {
	exported := packagestest.Export(t, packagestest.Modules, []packagestest.Module{
		{
			Name: "test",
			Files: map[string]any{
				"test.go": `package test; import "test/other"; var _ = other.Test;`,
				"other/file.go": `package other

//line other.go:1:1
var Test int
`,
			},
		},
	})
	t.Cleanup(exported.Cleanup)

	exported.Config.Mode = packages.LoadSyntax
	exported.Config.Fset = token.NewFileSet()
	pkgs, err := packages.Load(exported.Config, "test")
	if err != nil {
		t.Fatal(err)
	}
	packages.PrintErrors(pkgs)

	p := pkgs[0].Imports["test/other"]
	pos := p.Types.Scope().Lookup("Test").Pos()
	t.Log(exported.Config.Fset.Position(pos)) // other.go:4:1
}

The Mode is set to packages.LoadSyntax, thus go/packages is going to load test/other from the ExportFile.

$ go test ./go/packages/ -run TestReproducer -v
=== RUN   TestReproducer
    (....)
    packages_test.go:3455: other.go:4:1
--- PASS: TestReproducer (0.15s)

The file name (other.go) comes from the line directive, but the line/col (4:1) information does not.

I think that the issue is somewhere in cmd/go, while debugging, i saw the same values here (i.e. other.go:4:1):

https://github.com/golang/tools/blob/4f820dbaf9859eaafa01a17d133583f4d8c5a73c/internal/gcimporter/ureader_yes.go#L201-L205

@gabyhelp
Copy link

gabyhelp commented Jan 1, 2025

Related Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@mateusz834 mateusz834 added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jan 1, 2025
@mateusz834 mateusz834 changed the title cmd/go: line directives not treatead proberly in the exported file cmd/go: line directives not treatead properly in the exported file Jan 1, 2025
@ianlancetaylor ianlancetaylor changed the title cmd/go: line directives not treatead properly in the exported file go/packages: line directives not treatead properly in the exported file Jan 2, 2025
@ianlancetaylor
Copy link
Member

CC @griesemer @adonovan @findleyr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

3 participants