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: Allow go list command to accept file contents from STDIN #28809

Closed
marwan-at-work opened this issue Nov 15, 2018 · 5 comments
Closed
Labels
FrozenDueToAge GoCommand cmd/go NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.

Comments

@marwan-at-work
Copy link
Contributor

marwan-at-work commented Nov 15, 2018

Summary

Allow go list to read a file from STDIN instead of the actual file on disk so that we can get information on new imports that are not yet persisted to disk.

Reasoning

x/go/packages (which uses go list) is the basis for all new tooling (go-to-def, autocomplete, etc).

When a user is editing a file on their editor by importing a new package, none of these tools can work with the new import because go list cannot recognize the new import statement since it is in the editor's buffer and not yet persisted back to the file on disk.

The only workaround is persisting the file, which comes with its own drawbacks: every time you include a new import you have to use the package incorrectly, get a compile error, just so that you can get autocomplete and gotodef working again. Not a productive user experience. Even worse, GOPACKAGESDRIVER tools that implement caching of go list responses end up caching broken trees.

What version of Go are you using (go version)?

$ go version
go version go1.11 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

Say I have this file:

package main

func main() {

}

Then I start editing it like so:

package main

import "github.com/pkg/errors"

func main() {
  errors. // <-- cursor is at the dot
}

Note this file is not yet persisted to disk.

What did you expect to see?

I expect to see an autocomplete list of all of pkg/errors' public API, and if I pick one, I should be able to "GoToDef"

What did you see instead?

It doesn't work unless I persist and try again.

@marwan-at-work marwan-at-work added the GoCommand cmd/go label Nov 15, 2018
@mvdan
Copy link
Member

mvdan commented Nov 15, 2018

How would this relate to Overlay map[string][]byte and ParseFile func(fset *token.FileSet, filename string, src []byte) (*ast.File, error), both in https://godoc.org/golang.org/x/tools/go/packages#Config?

@marwan-at-work
Copy link
Contributor Author

@mvdan I imagine go/packages will pass the Overlay map to go list in one form or another. Note that field's documentation is exactly what this issue is about:

// The Package.Imports map may not include packages that are imported only
// by the alternative file contents provided by Overlay. This may cause
// type-checking to fail.

@mvdan mvdan added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Nov 15, 2018
@bcmills
Copy link
Contributor

bcmills commented Nov 15, 2018

CC @ianthehat @matloob

@rogpeppe
Copy link
Contributor

Given that running go list can change the contents of the current go.mod file, how would that work with transitory file contents like this?

@bcmills
Copy link
Contributor

bcmills commented Jan 17, 2019

The main problem with reading from stdin is that we can only do it once: unlike all of the other files in the build, we would have to spool it into memory (or spool it to the filesystem ourselves, in which case it seems simpler to have the editor do that in the first place).

It may be more feasible to allow path-based file substitutions. If that would be helpful, please file a separate issue.

@bcmills bcmills closed this as completed Jan 17, 2019
@golang golang locked and limited conversation to collaborators Jan 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge GoCommand cmd/go NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

5 participants