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: "unsafe" should contain GoFiles=["unsafe/unsafe.go"] #59929

Closed
adonovan opened this issue May 2, 2023 · 5 comments
Closed
Assignees
Labels
Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@adonovan
Copy link
Member

adonovan commented May 2, 2023

Since its inception, go/packages has discarded the GoFiles field of the unsafe package. (I don't remember why I implemented it this way: perhaps I thought that GoFiles was for files that contain valid Go syntax but are de-selected from the build due to non-matching build tags, whereas it's debatable whether unsafe.go is valid Go. It can't be compiled without a .s file because it has missing function bodies, for example.) In any case, the unsafe package nearly always needs special handling by client applications, and it is inconvenient for some (e.g. gopls) to know the location of the unsafe.go file, so that it can be presented as documentation, and there is insufficient information to compute it from the packages.Package record for package "unsafe", which contains only the ID, Path, and Name fields, but no file names at all.

In hindsight I think it makes sense to include unsafe.go among the non-compiled GoFiles. I suspect the change is low risk because (as I mentioned) unsafe nearly always requires special handling anyway, and the GoFiles are never processed by the type checker.

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label May 2, 2023
@gopherbot gopherbot added this to the Unreleased milestone May 2, 2023
@adonovan adonovan self-assigned this May 2, 2023
@gopherbot
Copy link

Change https://go.dev/cl/491375 mentions this issue: go/packages: include "unsafe".GoFiles=["unsafe.go"]

@bcmills
Copy link
Contributor

bcmills commented May 4, 2023

On the cmd/go side, I see:

~$ gotip list -json unsafe
{
        "Dir": "/usr/local/google/home/bcmills/sdk/gotip/src/unsafe",
        "ImportPath": "unsafe",
        "Name": "unsafe",
        "Doc": "Package unsafe contains operations that step around the type safety of Go programs.",
        "Root": "/usr/local/google/home/bcmills/sdk/gotip",
        "Match": [
                "unsafe"
        ],
        "Goroot": true,
        "Standard": true,
        "GoFiles": [
                "unsafe.go"
        ]
}

But perhaps we should be reporting unsafe.go in IgnoredGoFiles instead, since we don't actually present that file to the compiler in order to build the package? (I'm not sure.)

@adonovan
Copy link
Member Author

adonovan commented May 4, 2023

But perhaps we should be reporting unsafe.go in IgnoredGoFiles instead, since we don't actually present that file to the compiler in order to build the package? (I'm not sure.)

I'm not sure either. IgnoredGoFiles says it's for "files that might be part of the package using other build configurations" and are thus valid syntax, whereas GoFiles (as a matter of fact---it's not documented) holds the cgo source files, which are not really valid Go syntax. So I think GoFiles may be a better fit.

@bcmills
Copy link
Contributor

bcmills commented May 4, 2023

IgnoredGoFiles is not necessarily valid syntax for the Go version in current use.

(For example, the files could contain //go:build go1.24 constraints, in which case they would be syntactically valid for Go 1.24 but not necessarily for Go 1.20.)

@adonovan
Copy link
Member Author

adonovan commented May 4, 2023

True. But the unsafe.go and builtin.go source files are never supposed to be seen by the compiler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

3 participants