| 1 | // Copyright 2024 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // The drivertest package provides a fake implementation of the go/packages |
| 6 | // driver protocol that delegates to the go list driver. It may be used to test |
| 7 | // programs such as gopls that specialize behavior when a go/packages driver is |
| 8 | // in use. |
| 9 | // |
| 10 | // The driver is run as a child of the current process, by calling [RunIfChild] |
| 11 | // at process start, and running go/packages with the environment variables set |
| 12 | // by [Env]. |
| 13 | package drivertest |
| 14 | |
| 15 | import ( |
| 16 | "encoding/json" |
| 17 | "flag" |
| 18 | "log" |
| 19 | "os" |
| 20 | |
| 21 | "golang.org/x/tools/go/packages" |
| 22 | ) |
| 23 | |
| 24 | const runAsDriverEnv = "DRIVERTEST_RUN_AS_DRIVER" |
| 25 | |
| 26 | // RunIfChild runs the current process as a go/packages driver, if configured |
| 27 | // to do so by the current environment (see [Env]). |
| 28 | // |
| 29 | // Otherwise, RunIfChild is a no op. |
| 30 | func RunIfChild() { |
| 31 | if os.Getenv(runAsDriverEnv) != "" { |
| 32 | main() |
| 33 | os.Exit(0) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // Env returns additional environment variables for use in [packages.Config] |
| 38 | // to enable the use of drivertest as the driver. |
| 39 | // |
| 40 | // t abstracts a *testing.T or log.Default(). |
| 41 | func Env(t interface{ Fatal(...any) }) []string { |
| 42 | exe, err := os.Executable() |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | return []string{"GOPACKAGESDRIVER=" + exe, runAsDriverEnv + "=1"} |
| 47 | } |
| 48 | |
| 49 | func main() { |
| 50 | flag.Parse() |
| 51 | |
| 52 | dec := json.NewDecoder(os.Stdin) |
| 53 | var request packages.DriverRequest |
| 54 | if err := dec.Decode(&request); err != nil { |
| 55 | log.Fatalf("decoding request: %v", err) |
| 56 | } |
| 57 | |
| 58 | config := packages.Config{ |
| 59 | Mode: request.Mode, |
| 60 | Env: append(request.Env, "GOPACKAGESDRIVER=off"), // avoid recursive invocation |
| 61 | BuildFlags: request.BuildFlags, |
| 62 | Tests: request.Tests, |
| 63 | Overlay: request.Overlay, |
| 64 | } |
| 65 | pkgs, err := packages.Load(&config, flag.Args()...) |
| 66 | if err != nil { |
| 67 | log.Fatalf("load failed: %v", err) |
| 68 | } |
| 69 | |
| 70 | var roots []string |
| 71 | for _, pkg := range pkgs { |
| 72 | roots = append(roots, pkg.ID) |
| 73 | } |
| 74 | var allPackages []*packages.Package |
| 75 | packages.Visit(pkgs, nil, func(pkg *packages.Package) { |
| 76 | newImports := make(map[string]*packages.Package) |
| 77 | for path, imp := range pkg.Imports { |
| 78 | newImports[path] = &packages.Package{ID: imp.ID} |
| 79 | } |
| 80 | pkg.Imports = newImports |
| 81 | allPackages = append(allPackages, pkg) |
| 82 | }) |
| 83 | |
| 84 | enc := json.NewEncoder(os.Stdout) |
| 85 | response := packages.DriverResponse{ |
| 86 | Roots: roots, |
| 87 | Packages: allPackages, |
| 88 | } |
| 89 | if err := enc.Encode(response); err != nil { |
| 90 | log.Fatalf("encoding response: %v", err) |
| 91 | } |
| 92 | } |