|
| 1 | +package source |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "golang.org/x/tools/internal/span" |
| 13 | +) |
| 14 | + |
| 15 | +func checkCommonErrors(ctx context.Context, view View, uri span.URI) (string, error) { |
| 16 | + // Some cases we should be able to detect: |
| 17 | + // |
| 18 | + // 1. The user is in GOPATH mode and is working outside their GOPATH |
| 19 | + // 2. The user is in module mode and has opened a subdirectory of their module |
| 20 | + // |
| 21 | + gopath := os.Getenv("GOPATH") |
| 22 | + cfg := view.Config(ctx) |
| 23 | + |
| 24 | + // Invoke `go env GOMOD` inside of the directory of the file. |
| 25 | + fdir := filepath.Dir(uri.Filename()) |
| 26 | + b, err := invokeGo(ctx, fdir, cfg.Env, "env", "GOMOD") |
| 27 | + if err != nil { |
| 28 | + return "", err |
| 29 | + } |
| 30 | + modFile := strings.TrimSpace(b.String()) |
| 31 | + if modFile == filepath.FromSlash("/dev/null") { |
| 32 | + modFile = "" |
| 33 | + } |
| 34 | + |
| 35 | + // Not inside of a module. |
| 36 | + inAModule := modFile != "" |
| 37 | + inGopath := strings.HasPrefix(uri.Filename(), filepath.Join(gopath, "src")) |
| 38 | + moduleMode := os.Getenv("GO111MODULE") |
| 39 | + |
| 40 | + var msg string |
| 41 | + // The user is in a module. |
| 42 | + if inAModule { |
| 43 | + // The workspace root is open to a directory different from the module root. |
| 44 | + if modRoot := filepath.Dir(modFile); cfg.Dir != filepath.Dir(modFile) { |
| 45 | + msg = fmt.Sprintf("Your workspace root is %s, but your module root is %s. Please add %s as a workspace folder.", cfg.Dir, modRoot, modRoot) |
| 46 | + } |
| 47 | + } else if inGopath { |
| 48 | + if moduleMode == "on" { |
| 49 | + msg = "You are in module mode, but you are not inside of a module. Please create a module." |
| 50 | + } |
| 51 | + } else { |
| 52 | + msg = "You are neither in a module nor in your GOPATH. Please see X for information on how to set up your Go project." |
| 53 | + } |
| 54 | + return msg, nil |
| 55 | +} |
| 56 | + |
| 57 | +// invokeGo returns the stdout of a go command invocation. |
| 58 | +// Borrowed from golang.org/x/tools/go/packages/golist.go. |
| 59 | +func invokeGo(ctx context.Context, dir string, env []string, args ...string) (*bytes.Buffer, error) { |
| 60 | + stdout := new(bytes.Buffer) |
| 61 | + stderr := new(bytes.Buffer) |
| 62 | + cmd := exec.CommandContext(ctx, "go", args...) |
| 63 | + // On darwin the cwd gets resolved to the real path, which breaks anything that |
| 64 | + // expects the working directory to keep the original path, including the |
| 65 | + // go command when dealing with modules. |
| 66 | + // The Go stdlib has a special feature where if the cwd and the PWD are the |
| 67 | + // same node then it trusts the PWD, so by setting it in the env for the child |
| 68 | + // process we fix up all the paths returned by the go command. |
| 69 | + cmd.Env = append(append([]string{}, env...), "PWD="+dir) |
| 70 | + cmd.Dir = dir |
| 71 | + cmd.Stdout = stdout |
| 72 | + cmd.Stderr = stderr |
| 73 | + |
| 74 | + if err := cmd.Run(); err != nil { |
| 75 | + // Check for 'go' executable not being found. |
| 76 | + if ee, ok := err.(*exec.Error); ok && ee.Err == exec.ErrNotFound { |
| 77 | + return nil, fmt.Errorf("'gopls requires 'go', but %s", exec.ErrNotFound) |
| 78 | + } |
| 79 | + if _, ok := err.(*exec.ExitError); !ok { |
| 80 | + // Catastrophic error: |
| 81 | + // - context cancellation |
| 82 | + return nil, fmt.Errorf("couldn't exec 'go %v': %s %T", args, err, err) |
| 83 | + } |
| 84 | + } |
| 85 | + return stdout, nil |
| 86 | +} |
0 commit comments