| 1 | // Copyright 2020 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 gocommandscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ |
| 6 | |
| 7 | import ( |
| 8 | "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/" |
| 9 | "contextscip-go gomod github.com/golang/go/src go1.25.0 context/" |
| 10 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 11 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 12 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 13 | "regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/" |
| 14 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 15 | "timescip-go gomod github.com/golang/go/src go1.25.0 time/" |
| 16 | |
| 17 | "golang.org/x/mod/semverscip-go gomod golang.org/x/mod v0.35.0 `golang.org/x/mod/semver`/" |
| 18 | ) |
| 19 | |
| 20 | // ModuleJSON holds information about a module. |
| 21 | type ModuleJSONscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON# struct { |
| 22 | Pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#Path. string // module path |
| 23 | Versionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#Version. string // module version |
| 24 | Versionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#Versions. []string // available module versions (with -versions) |
| 25 | Replacescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#Replace. *ModuleJSONscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON# // replaced by this module |
| 26 | Timescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#Time. *timescip-go gomod github.com/golang/go/src go1.25.0 time/.Timescip-go gomod github.com/golang/go/src go1.25.0 time/Time# // time version was created |
| 27 | Updatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#Update. *ModuleJSONscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON# // available update, if any (with -u) |
| 28 | Mainscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#Main. bool // is this the main module? |
| 29 | Indirectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#Indirect. bool // is this module only an indirect dependency of main module? |
| 30 | Dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#Dir. string // directory holding files for this module, if any |
| 31 | GoModscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#GoMod. string // path to go.mod file used when loading this module, if any |
| 32 | GoVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#GoVersion. string // go version used in module |
| 33 | } |
| 34 | |
| 35 | var modFlagRegexpscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/modFlagRegexp. = regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.25.0 regexp/MustCompile().(`-mod[ =](\w+)`) |
| 36 | |
| 37 | // VendorEnabled reports whether vendoring is enabled. It takes a *Runner to execute Go commands |
| 38 | // with the supplied context.Context and Invocation. The Invocation can contain pre-defined fields, |
| 39 | // of which only Verb and Args are modified to run the appropriate Go command. |
| 40 | // Inspired by setDefaultBuildMod in modload/init.go |
| 41 | func VendorEnabledscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/VendorEnabled().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, inv Invocationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Invocation#, r *Runnerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Runner#) (bool, *ModuleJSONscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#, error) { |
| 42 | mainMod, go114, err := getMainModuleAnd114scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/getMainModuleAnd114().(ctx, inv, r) |
| 43 | if err != nil { |
| 44 | return false, nil, err |
| 45 | } |
| 46 | |
| 47 | // We check the GOFLAGS to see if there is anything overridden or not. |
| 48 | inv.Verbscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Invocation#Verb. = "env" |
| 49 | inv.Argsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Invocation#Args. = []string{"GOFLAGS"} |
| 50 | stdout, err := r.Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Runner#Run().(ctx, inv) |
| 51 | if err != nil { |
| 52 | return false, nil, err |
| 53 | } |
| 54 | goflags := string(bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 bytes/TrimSpace().(stdout.Bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#Bytes().())) |
| 55 | matches := modFlagRegexpscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/modFlagRegexp..FindStringSubmatchscip-go gomod github.com/golang/go/src go1.25.0 regexp/Regexp#FindStringSubmatch().(goflags) |
| 56 | var modFlag string |
| 57 | if len(matches) != 0 { |
| 58 | modFlag = matches[1] |
| 59 | } |
| 60 | // Don't override an explicit '-mod=' argument. |
| 61 | if modFlag == "vendor" { |
| 62 | return true, mainMod, nil |
| 63 | } else if modFlag != "" { |
| 64 | return false, nil, nil |
| 65 | } |
| 66 | if mainMod == nil || !go114 { |
| 67 | return false, nil, nil |
| 68 | } |
| 69 | // Check 1.14's automatic vendor mode. |
| 70 | if fi, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Statscip-go gomod github.com/golang/go/src go1.25.0 os/Stat().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(mainMod.Dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#Dir., "vendor")); err == nil && fi.IsDirscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileInfo#IsDir().() { |
| 71 | if mainMod.GoVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#GoVersion. != "" && semverscip-go gomod golang.org/x/mod v0.35.0 `golang.org/x/mod/semver`/.Comparescip-go gomod golang.org/x/mod v0.35.0 `golang.org/x/mod/semver`/Compare().("v"+mainMod.GoVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#GoVersion., "v1.14") >= 0 { |
| 72 | // The Go version is at least 1.14, and a vendor directory exists. |
| 73 | // Set -mod=vendor by default. |
| 74 | return true, mainMod, nil |
| 75 | } |
| 76 | } |
| 77 | return false, nil, nil |
| 78 | } |
| 79 | |
| 80 | // getMainModuleAnd114 gets one of the main modules' information and whether the |
| 81 | // go command in use is 1.14+. This is the information needed to figure out |
| 82 | // if vendoring should be enabled. |
| 83 | func getMainModuleAnd114scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/getMainModuleAnd114().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, inv Invocationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Invocation#, r *Runnerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Runner#) (*ModuleJSONscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#, bool, error) { |
| 84 | const format = `{{.Path}} |
| 85 | {{.Dir}} |
| 86 | {{.GoMod}} |
| 87 | {{.GoVersion}} |
| 88 | {{range context.ReleaseTags}}{{if eq . "go1.14"}}{{.}}{{end}}{{end}} |
| 89 | ` |
| 90 | inv.Verbscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Invocation#Verb. = "list" |
| 91 | inv.Argsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Invocation#Args. = []string{"-m", "-f", format} |
| 92 | stdout, err := r.Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Runner#Run().(ctx, inv) |
| 93 | if err != nil { |
| 94 | return nil, false, err |
| 95 | } |
| 96 | |
| 97 | lines := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Splitscip-go gomod github.com/golang/go/src go1.25.0 strings/Split().(stdout.Stringscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#String().(), "\n") |
| 98 | if len(lines) < 5 { |
| 99 | return nil, false, 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().("unexpected stdout: %q", stdout.Stringscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#String().()) |
| 100 | } |
| 101 | mod := &ModuleJSONscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#{ |
| 102 | Pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#Path.: lines[0], |
| 103 | Dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#Dir.: lines[1], |
| 104 | GoModscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#GoMod.: lines[2], |
| 105 | GoVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#GoVersion.: lines[3], |
| 106 | Mainscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#Main.: true, |
| 107 | } |
| 108 | return mod, lines[4] == "go1.14", nil |
| 109 | } |
| 110 | |
| 111 | // WorkspaceVendorEnabled reports whether workspace vendoring is enabled. It takes a *Runner to execute Go commands |
| 112 | // with the supplied context.Context and Invocation. The Invocation can contain pre-defined fields, |
| 113 | // of which only Verb and Args are modified to run the appropriate Go command. |
| 114 | // Inspired by setDefaultBuildMod in modload/init.go |
| 115 | func WorkspaceVendorEnabledscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/WorkspaceVendorEnabled().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, inv Invocationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Invocation#, r *Runnerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Runner#) (bool, []*ModuleJSONscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#, error) { |
| 116 | inv.Verbscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Invocation#Verb. = "env" |
| 117 | inv.Argsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Invocation#Args. = []string{"GOWORK"} |
| 118 | stdout, err := r.Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Runner#Run().(ctx, inv) |
| 119 | if err != nil { |
| 120 | return false, nil, err |
| 121 | } |
| 122 | goWork := string(bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 bytes/TrimSpace().(stdout.Bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#Bytes().())) |
| 123 | if fi, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Statscip-go gomod github.com/golang/go/src go1.25.0 os/Stat().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Dir().(goWork), "vendor")); err == nil && fi.IsDirscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileInfo#IsDir().() { |
| 124 | mainMods, err := getWorkspaceMainModulesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/getWorkspaceMainModules().(ctx, inv, r) |
| 125 | if err != nil { |
| 126 | return false, nil, err |
| 127 | } |
| 128 | return true, mainMods, nil |
| 129 | } |
| 130 | return false, nil, nil |
| 131 | } |
| 132 | |
| 133 | // getWorkspaceMainModules gets the main modules' information. |
| 134 | // This is the information needed to figure out if vendoring should be enabled. |
| 135 | func getWorkspaceMainModulesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/getWorkspaceMainModules().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, inv Invocationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Invocation#, r *Runnerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Runner#) ([]*ModuleJSONscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#, error) { |
| 136 | const format = `{{.Path}} |
| 137 | {{.Dir}} |
| 138 | {{.GoMod}} |
| 139 | {{.GoVersion}} |
| 140 | ` |
| 141 | inv.Verbscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Invocation#Verb. = "list" |
| 142 | inv.Argsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Invocation#Args. = []string{"-m", "-f", format} |
| 143 | stdout, err := r.Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Runner#Run().(ctx, inv) |
| 144 | if err != nil { |
| 145 | return nil, err |
| 146 | } |
| 147 | |
| 148 | lines := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Splitscip-go gomod github.com/golang/go/src go1.25.0 strings/Split().(stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSuffix().(stdout.Stringscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#String().(), "\n"), "\n") |
| 149 | if len(lines) < 4 { |
| 150 | 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().("unexpected stdout: %q", stdout.Stringscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#String().()) |
| 151 | } |
| 152 | mods := make([]*ModuleJSONscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#, 0, len(lines)/4) |
| 153 | for i := 0; i < len(lines); i += 4 { |
| 154 | mods = append(mods, &ModuleJSONscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#{ |
| 155 | Pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#Path.: lines[i], |
| 156 | Dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#Dir.: lines[i+1], |
| 157 | GoModscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#GoMod.: lines[i+2], |
| 158 | GoVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#GoVersion.: lines[i+3], |
| 159 | Mainscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/ModuleJSON#Main.: true, |
| 160 | }) |
| 161 | } |
| 162 | return mods, nil |
| 163 | } |