| 1 | // Copyright 2018 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 | package packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/ |
| 6 | |
| 7 | // This file defines the protocol that enables an external "driver" |
| 8 | // tool to supply package metadata in place of 'go list'. |
| 9 | |
| 10 | import ( |
| 11 | "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/" |
| 12 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/" |
| 13 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 14 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 15 | "os/execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/" |
| 16 | "slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/" |
| 17 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 18 | ) |
| 19 | |
| 20 | // DriverRequest defines the schema of a request for package metadata |
| 21 | // from an external driver program. The JSON-encoded DriverRequest |
| 22 | // message is provided to the driver program's standard input. The |
| 23 | // query patterns are provided as command-line arguments. |
| 24 | // |
| 25 | // See the package documentation for an overview. |
| 26 | type DriverRequestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/DriverRequest# struct { |
| 27 | Modescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/DriverRequest#Mode. LoadModescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/LoadMode# `json:"mode"` |
| 28 | |
| 29 | // Env specifies the environment the underlying build system should be run in. |
| 30 | Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/DriverRequest#Env. []string `json:"env"` |
| 31 | |
| 32 | // BuildFlags are flags that should be passed to the underlying build system. |
| 33 | BuildFlagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/DriverRequest#BuildFlags. []string `json:"build_flags"` |
| 34 | |
| 35 | // Tests specifies whether the patterns should also return test packages. |
| 36 | Testsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/DriverRequest#Tests. bool `json:"tests"` |
| 37 | |
| 38 | // Overlay maps file paths (relative to the driver's working directory) |
| 39 | // to the contents of overlay files (see Config.Overlay). |
| 40 | Overlayscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/DriverRequest#Overlay. map[string][]byte `json:"overlay"` |
| 41 | } |
| 42 | |
| 43 | // DriverResponse defines the schema of a response from an external |
| 44 | // driver program, providing the results of a query for package |
| 45 | // metadata. The driver program must write a JSON-encoded |
| 46 | // DriverResponse message to its standard output. |
| 47 | // |
| 48 | // See the package documentation for an overview. |
| 49 | type DriverResponsescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/DriverResponse# struct { |
| 50 | // NotHandled is returned if the request can't be handled by the current |
| 51 | // driver. If an external driver returns a response with NotHandled, the |
| 52 | // rest of the DriverResponse is ignored, and go/packages will fallback |
| 53 | // to the next driver. If go/packages is extended in the future to support |
| 54 | // lists of multiple drivers, go/packages will fall back to the next driver. |
| 55 | NotHandledscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/DriverResponse#NotHandled. bool |
| 56 | |
| 57 | // Compiler and Arch are the arguments pass of types.SizesFor |
| 58 | // to get a types.Sizes to use when type checking. |
| 59 | Compilerscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/DriverResponse#Compiler. string |
| 60 | Archscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/DriverResponse#Arch. string |
| 61 | |
| 62 | // Roots is the set of package IDs that make up the root packages. |
| 63 | // We have to encode this separately because when we encode a single package |
| 64 | // we cannot know if it is one of the roots as that requires knowledge of the |
| 65 | // graph it is part of. |
| 66 | Rootsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/DriverResponse#Roots. []string `json:",omitempty"` |
| 67 | |
| 68 | // Packages is the full set of packages in the graph. |
| 69 | // The packages are not connected into a graph. |
| 70 | // The Imports if populated will be stubs that only have their ID set. |
| 71 | // Imports will be connected and then type and syntax information added in a |
| 72 | // later pass (see refine). |
| 73 | Packagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/DriverResponse#Packages. []*Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Package# |
| 74 | |
| 75 | // GoVersion is the minor version number used by the driver |
| 76 | // (e.g. the go command on the PATH) when selecting .go files. |
| 77 | // Zero means unknown. |
| 78 | GoVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/DriverResponse#GoVersion. int |
| 79 | } |
| 80 | |
| 81 | // driver is the type for functions that query the build system for the |
| 82 | // packages named by the patterns. |
| 83 | type driverscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/driver# func(cfg *Configscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#, patterns []string) (*DriverResponsescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/DriverResponse#, error) |
| 84 | |
| 85 | // findExternalDriver returns the file path of a tool that supplies |
| 86 | // the build system package structure, or "" if not found. |
| 87 | // If GOPACKAGESDRIVER is set in the environment findExternalTool returns its |
| 88 | // value, otherwise it searches for a binary named gopackagesdriver on the PATH. |
| 89 | func findExternalDriverscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/findExternalDriver().(cfg *Configscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#) driverscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/driver# { |
| 90 | const toolPrefix = "GOPACKAGESDRIVER=" |
| 91 | tool := "" |
| 92 | for _, env := range cfg.Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Env. { |
| 93 | if val, ok := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.CutPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/CutPrefix().(env, toolPrefix); ok { |
| 94 | tool = val |
| 95 | } |
| 96 | } |
| 97 | if tool != "" && tool == "off" { |
| 98 | return nil |
| 99 | } |
| 100 | if tool == "" { |
| 101 | var err error |
| 102 | tool, err = execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.LookPathscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/LookPath().("gopackagesdriver") |
| 103 | if err != nil { |
| 104 | return nil |
| 105 | } |
| 106 | } |
| 107 | return func(cfg *Configscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#, patterns []string) (*DriverResponsescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/DriverResponse#, error) { |
| 108 | req, err := jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Marshal().(DriverRequestscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/DriverRequest#{ |
| 109 | Modescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/DriverRequest#Mode.: cfg.Modescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Mode., |
| 110 | Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/DriverRequest#Env.: cfg.Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Env., |
| 111 | BuildFlagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/DriverRequest#BuildFlags.: cfg.BuildFlagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#BuildFlags., |
| 112 | Testsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/DriverRequest#Tests.: cfg.Testsscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Tests., |
| 113 | Overlayscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/DriverRequest#Overlay.: cfg.Overlayscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Overlay., |
| 114 | }) |
| 115 | if err != nil { |
| 116 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("failed to encode message to driver tool: %v", err) |
| 117 | } |
| 118 | |
| 119 | buf := new(bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#) |
| 120 | stderr := new(bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#) |
| 121 | cmd := execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.CommandContextscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/CommandContext().(cfg.Contextscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Context., tool, patterns...) |
| 122 | cmd.Dirscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Dir. = cfg.Dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Dir. |
| 123 | // The cwd gets resolved to the real path. On Darwin, where |
| 124 | // /tmp is a symlink, this breaks anything that expects the |
| 125 | // working directory to keep the original path, including the |
| 126 | // go command when dealing with modules. |
| 127 | // |
| 128 | // os.Getwd stdlib has a special feature where if the |
| 129 | // cwd and the PWD are the same node then it trusts |
| 130 | // the PWD, so by setting it in the env for the child |
| 131 | // process we fix up all the paths returned by the go |
| 132 | // command. |
| 133 | // |
| 134 | // (See similar trick in Invocation.run in ../../internal/gocommand/invoke.go) |
| 135 | cmd.Envscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Env. = append(slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Clipscip-go gomod github.com/golang/go/src go1.25.0 slices/Clip().(cfg.Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Env.), "PWD="+cfg.Dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Dir.) |
| 136 | cmd.Stdinscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Stdin. = bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.NewReaderscip-go gomod github.com/golang/go/src go1.25.0 bytes/NewReader().(req) |
| 137 | cmd.Stdoutscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Stdout. = buf |
| 138 | cmd.Stderrscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Stderr. = stderr |
| 139 | |
| 140 | if err := cmd.Runscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Run().(); err != nil { |
| 141 | return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("%v: %v: %s", tool, err, cmd.Stderrscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Stderr.) |
| 142 | } |
| 143 | if len(stderr.Bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#Bytes().()) != 0 && osscip-go gomod github.com/golang/go/src go1.25.0 os/.Getenvscip-go gomod github.com/golang/go/src go1.25.0 os/Getenv().("GOPACKAGESPRINTDRIVERERRORS") != "" { |
| 144 | fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(osscip-go gomod github.com/golang/go/src go1.25.0 os/.Stderrscip-go gomod github.com/golang/go/src go1.25.0 os/Stderr., "%s stderr: <<%s>>\n", cmdDebugStrscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/cmdDebugStr().(cmd), stderr) |
| 145 | } |
| 146 | |
| 147 | var response DriverResponsescip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/DriverResponse# |
| 148 | if err := jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Unmarshal().(buf.Bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#Bytes().(), &response); err != nil { |
| 149 | return nil, err |
| 150 | } |
| 151 | return &response, nil |
| 152 | } |
| 153 | } |