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

cmd/go: 'go run x.go' should not be able to import internal packages #10478

Closed
rsc opened this issue Apr 16, 2015 · 6 comments
Closed

cmd/go: 'go run x.go' should not be able to import internal packages #10478

rsc opened this issue Apr 16, 2015 · 6 comments
Milestone

Comments

@rsc
Copy link
Contributor

rsc commented Apr 16, 2015

Commands built or run by listing Go source file names on the command line are treated as a separate workspace from $GOROOT and $GOPATH. The handling of /internal/ is not doing that, meaning that if you put an x.go in, say, $GOROOT/src/http and run 'go run x.go', it can import http/internal. That's a mistake.

@rsc rsc added this to the Go1.5 milestone Apr 16, 2015
@mdempsky
Copy link
Member

It seems like this change would make it impossible for one-off generator code to use internal packages. Supposing that this is fixed as you suggest but also that we did want to add a compress/flate/internal/huffman package, how could we implement compress/flate/gen.go instead?

@minux
Copy link
Member

minux commented Apr 16, 2015 via email

@rsc
Copy link
Contributor Author

rsc commented Apr 19, 2015

I'm sorry, but the fact that people use the bug does not make it less of a bug. Internal means internal.

@dominikh
Copy link
Member

Russ, on the one hand you want go run to behave more like other tools, to the point that it should be able to run an entire package. But on the other hand, go run should ignore the package a file is part of? go build x.go doesn't behave that way.

Personally, I thought that the "a separate workspace" aspect was an implementation detail, not the public interface, and that go run was supposed to be a shorthand for go build + ./a.out, not for cp foo.go /tmp/...

@mdempsky
Copy link
Member

@rsc I agree that "go run" and "go build" need to be made internal-aware. It's certainly a bug that "go run $GOPATH/src/foo/x.go" currently works even if foo/x.go imports internal packages from the standard library like net/http/internal.

It's just not obvious to me that "go run $GOROOT/src/net/http/x.go" should fail if net/http/x.go imports net/http/internal, because it would complicate some seemingly reasonable use cases for mixing "internal" and "go generate".

A simple concrete proposal would be that if the files passed to "go build" or "go run" are all within the same directory, then they should be allowed to import internal packages just as normal package code within that same directory could. For example, $GOROOT/src/net/http/x.go could import net/http/internal, but not $GOROOT/src/cmd/internal/gc.

The proposal could be extended to files spread across multiple directories too, but I'm not sure there's a need.

@rsc
Copy link
Contributor Author

rsc commented Jul 14, 2015

Will let this stand unless more evidence accumulates that this is a mistake.

@rsc rsc closed this as completed Jul 14, 2015
@golang golang locked and limited conversation to collaborators Jul 13, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants