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

x/tools/go/packages: overlay does not cause ExportFile to be cleared #71098

Open
mateusz834 opened this issue Jan 2, 2025 · 2 comments
Open
Labels
Bug Issues describing a bug in the Go implementation. 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

@mateusz834
Copy link
Member

In the x/tools/go/pacakges there is a comment, mentioning that overlay might invalidate export data, see:

https://github.com/golang/tools/blob/b93274bf6492f1e1ab72ee5def8da5dfbc9e0e43/go/packages/packages.go#L800-L802

It was introduced in CL 151999.

If that is still true, then we should probably also clear the ExportFile field, this is currently not being done. But looking at the exported file, it seems to include the Test2 symbol defined in the overlay, so it seems to work. The comment mentions that it can so maybe this might happen only in some other corner-cases (that i am not aware of), if so we should probably clear that ExportFile.

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

func TestReproducer(t *testing.T) {
	log.SetFlags(log.Lshortfile)
	exported := packagestest.Export(t, packagestest.Modules, []packagestest.Module{
		{
			Name: "test",
			Files: map[string]any{
				"test.go":       `package test; import "test/other"; func test() { other.Test2() }`,
				"other/file.go": `package other; func Test() {}`,
			},
			Overlay: map[string][]byte{
				`other/file.go`: []byte(`package other; func Test2() {}`),
			},
		},
	})
	t.Cleanup(exported.Cleanup)

	exported.Config.Mode = packages.LoadSyntax | packages.NeedExportFile
	pkgs, err := packages.Load(exported.Config, "test")
	if err != nil {
		t.Error(err)
	}
	packages.PrintErrors(pkgs)

	ef := pkgs[0].Imports["test/other"].ExportFile
	if ef != "" {
		t.Error("ExportFile for package text/other is not empty") // fails
	}

	f, err := os.Open(ef)
	if err != nil {
		t.Fatal(err)
	}

	r, err := gcexportdata.NewReader(f)
	if err != nil {
		t.Fatal(err)
	}

	fset := token.NewFileSet()
	pkg, err := gcexportdata.Read(r, fset, make(map[string]*types.Package), "test/other")
	if err != nil {
		t.Fatal(err)
	}

	t.Log(pkg.Scope().Lookup("Test2") != nil) // true
}

CC @matloob @adonovan

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Jan 2, 2025
@gopherbot gopherbot added this to the Unreleased milestone Jan 2, 2025
@mateusz834 mateusz834 added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. and removed Tools This label describes issues relating to any tools in the x/tools repository. labels Jan 2, 2025
@gabyhelp
Copy link

gabyhelp commented Jan 2, 2025

Related Issues

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

@mateusz834 mateusz834 added the Tools This label describes issues relating to any tools in the x/tools repository. label Jan 2, 2025
@gabyhelp gabyhelp added the Bug Issues describing a bug in the Go implementation. label Jan 2, 2025
@matloob
Copy link
Contributor

matloob commented Jan 6, 2025

As I understand it, the comment isn't true anymore. We now use the go command's overlay support (which was added after the referenced code was written) to handle overlays, so the export data should take overlays into account.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Issues describing a bug in the Go implementation. 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