Debian Codegraph @

cmd/fiximports/main.go

raw symbols SCIP
1 // Copyright 2015 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 fiximports command fixes import declarations to use the canonical
6 // import path for packages that have an "import comment" as defined by
7 // https://golang.org/s/go14customimport.
8 //
9 // # Background
10 //
11 // The Go 1 custom import path mechanism lets the maintainer of a
12 // package give it a stable name by which clients may import and "go
13 // get" it, independent of the underlying version control system (such
14 // as Git) or server (such as github.com) that hosts it. Requests for
15 // the custom name are redirected to the underlying name. This allows
16 // packages to be migrated from one underlying server or system to
17 // another without breaking existing clients.
18 //
19 // Because this redirect mechanism creates aliases for existing
20 // packages, it's possible for a single program to import the same
21 // package by its canonical name and by an alias. The resulting
22 // executable will contain two copies of the package, which is wasteful
23 // at best and incorrect at worst.
24 //
25 // To avoid this, "go build" reports an error if it encounters a special
26 // comment like the one below, and if the import path in the comment
27 // does not match the path of the enclosing package relative to
28 // GOPATH/src:
29 //
30 // $ grep ^package $GOPATH/src/github.com/bob/vanity/foo/foo.go
31 // package foo // import "vanity.com/foo"
32 //
33 // The error from "go build" indicates that the package canonically
34 // known as "vanity.com/foo" is locally installed under the
35 // non-canonical name "github.com/bob/vanity/foo".
36 //
37 // # Usage
38 //
39 // When a package that you depend on introduces a custom import comment,
40 // and your workspace imports it by the non-canonical name, your build
41 // will stop working as soon as you update your copy of that package
42 // using "go get -u".
43 //
44 // The purpose of the fiximports tool is to fix up all imports of the
45 // non-canonical path within a Go workspace, replacing them with imports
46 // of the canonical path. Following a run of fiximports, the workspace
47 // will no longer depend on the non-canonical copy of the package, so it
48 // should be safe to delete. It may be necessary to run "go get -u"
49 // again to ensure that the package is locally installed under its
50 // canonical path, if it was not already.
51 //
52 // The fiximports tool operates locally; it does not make HTTP requests
53 // and does not discover new custom import comments. It only operates
54 // on non-canonical packages present in your workspace.
55 //
56 // The -baddomains flag is a list of domain names that should always be
57 // considered non-canonical. You can use this if you wish to make sure
58 // that you no longer have any dependencies on packages from that
59 // domain, even those that do not yet provide a canonical import path
60 // comment. For example, the default value of -baddomains includes the
61 // moribund code hosting site code.google.com, so fiximports will report
62 // an error for each import of a package from this domain remaining
63 // after canonicalization.
64 //
65 // To see the changes fiximports would make without applying them, use
66 // the -n flag.
67 package mainscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/
68
69 import (
70 "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/"
71 "encoding/jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/"
72 "flagscip-go gomod github.com/golang/go/src go1.25.0 flag/"
73 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
74 "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/"
75 "go/formatscip-go gomod github.com/golang/go/src go1.25.0 `go/format`/"
76 "go/parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/"
77 "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/"
78 "ioscip-go gomod github.com/golang/go/src go1.25.0 io/"
79 "logscip-go gomod github.com/golang/go/src go1.25.0 log/"
80 "osscip-go gomod github.com/golang/go/src go1.25.0 os/"
81 "os/execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/"
82 "pathscip-go gomod github.com/golang/go/src go1.25.0 path/"
83 "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/"
84 "sortscip-go gomod github.com/golang/go/src go1.25.0 sort/"
85 "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/"
86 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
87 )
88
89 // flags
90 var (
91 dryrunscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/dryrun. = flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.Boolscip-go gomod github.com/golang/go/src go1.25.0 flag/Bool().("n", false, "dry run: show changes, but don't apply them")
92 badDomainsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/badDomains. = flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.Stringscip-go gomod github.com/golang/go/src go1.25.0 flag/String().("baddomains", "code.google.com",
93 "a comma-separated list of domains from which packages should not be imported")
94 replaceFlagscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/replaceFlag. = flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.Stringscip-go gomod github.com/golang/go/src go1.25.0 flag/String().("replace", "",
95 "a comma-separated list of noncanonical=canonical pairs of package paths. If both items in a pair end with '...', they are treated as path prefixes.")
96 )
97
98 // seams for testing
99 var (
100 stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/stderr. ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Writerscip-go gomod github.com/golang/go/src go1.25.0 io/Writer# = 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.
101 writeFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/writeFile. = osscip-go gomod github.com/golang/go/src go1.25.0 os/.WriteFilescip-go gomod github.com/golang/go/src go1.25.0 os/WriteFile().
102 )
103
104 const usagescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/usage. = `fiximports: rewrite import paths to use canonical package names.
105
106 Usage: fiximports [-n] package...
107
108 The package... arguments specify a list of packages
109 in the style of the go tool; see "go help packages".
110 Hint: use "all" or "..." to match the entire workspace.
111
112 For details, see https://pkg.go.dev/golang.org/x/tools/cmd/fiximports
113
114 Flags:
115 -n: dry run: show changes, but don't apply them
116 -baddomains a comma-separated list of domains from which packages
117 should not be imported
118 `
119
120 func mainscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/main().() {
121 flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.Parsescip-go gomod github.com/golang/go/src go1.25.0 flag/Parse().()
122
123 if len(flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.Argsscip-go gomod github.com/golang/go/src go1.25.0 flag/Args().()) == 0 {
124 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprint().(stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/stderr., usagescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/usage.)
125 osscip-go gomod github.com/golang/go/src go1.25.0 os/.Exitscip-go gomod github.com/golang/go/src go1.25.0 os/Exit().(1)
126 }
127 if !fiximportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/fiximports().(flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.Argsscip-go gomod github.com/golang/go/src go1.25.0 flag/Args().()...) {
128 osscip-go gomod github.com/golang/go/src go1.25.0 os/.Exitscip-go gomod github.com/golang/go/src go1.25.0 os/Exit().(1)
129 }
130 }
131
132 type canonicalNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/canonicalName# struct{ pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/canonicalName#path., namescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/canonicalName#name. string }
133
134 // fiximports fixes imports in the specified packages.
135 // Invariant: a false result implies an error was already printed.
136 func fiximportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/fiximports().(packages ...string) bool {
137 // importedBy is the transpose of the package import graph.
138 importedBy := make(map[string]map[*listPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#]bool)
139
140 // addEdge adds an edge to the import graph.
141 addEdge := func(from *listPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#, to string) {
142 if to == "C" || to == "unsafe" {
143 return // fake
144 }
145 pkgs := importedBy[to]
146 if pkgs == nil {
147 pkgs = make(map[*listPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#]bool)
148 importedBy[to] = pkgs
149 }
150 pkgs[from] = true
151 }
152
153 // List metadata for all packages in the workspace.
154 pkgs, err := listscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/list().("...")
155 if err != nil {
156 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().(stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/stderr., "importfix: %v\n", err)
157 return false
158 }
159
160 // packageName maps each package's path to its name.
161 packageName := make(map[string]string)
162 for _, p := range pkgs {
163 packageName[p.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#ImportPath.] = p.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#Name.
164 }
165
166 // canonical maps each non-canonical package path to
167 // its canonical path and name.
168 // A present nil value indicates that the canonical package
169 // is unknown: hosted on a bad domain with no redirect.
170 canonical := make(map[string]canonicalNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/canonicalName#)
171 domains := 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().(*badDomainsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/badDomains., ",")
172
173 type replaceItem struct {
174 old, new string
175 matchPrefix bool
176 }
177 var replace []replaceItem
178 for pair := range stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.SplitSeqscip-go gomod github.com/golang/go/src go1.25.0 strings/SplitSeq().(*replaceFlagscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/replaceFlag., ",") {
179 if pair == "" {
180 continue
181 }
182 words := 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().(pair, "=")
183 if len(words) != 2 {
184 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().(stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/stderr., "importfix: -replace: %q is not of the form \"canonical=noncanonical\".\n", pair)
185 return false
186 }
187 replace = append(replace, replaceItem{
188 old: 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().(words[0], "..."),
189 new: 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().(words[1], "..."),
190 matchPrefix: stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(words[0], "...") &&
191 stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(words[1], "..."),
192 })
193 }
194
195 // Find non-canonical packages and populate importedBy graph.
196 for _, p := range pkgs {
197 if p.Errorscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#Error. != nil {
198 msg := p.Errorscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#Error..Errscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/packageError#Err.
199 if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(msg, "code in directory") &&
200 stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(msg, "expects import") {
201 // don't show the very errors we're trying to fix
202 } else {
203 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintlnscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintln().(stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/stderr., p.Errorscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#Error.)
204 }
205 }
206
207 for _, imp := range p.Importsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#Imports. {
208 addEdge(p, imp)
209 }
210 for _, imp := range p.TestImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#TestImports. {
211 addEdge(p, imp)
212 }
213 for _, imp := range p.XTestImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#XTestImports. {
214 addEdge(p, imp)
215 }
216
217 // Does package have an explicit import comment?
218 if p.ImportCommentscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#ImportComment. != "" {
219 if p.ImportCommentscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#ImportComment. != p.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#ImportPath. {
220 canonical[p.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#ImportPath.] = canonicalNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/canonicalName#{
221 pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/canonicalName#path.: p.ImportCommentscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#ImportComment.,
222 namescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/canonicalName#name.: p.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#Name.,
223 }
224 }
225 } else {
226 // Is package matched by a -replace item?
227 var newPath string
228 for _, item := range replace {
229 if item.matchPrefix {
230 if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(p.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#ImportPath., item.old) {
231 newPath = item.new + p.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#ImportPath.[len(item.old):]
232 break
233 }
234 } else if p.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#ImportPath. == item.old {
235 newPath = item.new
236 break
237 }
238 }
239 if newPath != "" {
240 newName := packageName[newPath]
241 if newName == "" {
242 newName = filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Basescip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Base().(newPath) // a guess
243 }
244 canonical[p.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#ImportPath.] = canonicalNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/canonicalName#{
245 pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/canonicalName#path.: newPath,
246 namescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/canonicalName#name.: newName,
247 }
248 continue
249 }
250
251 // Is package matched by a -baddomains item?
252 for _, domain := range domains {
253 slash := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Indexscip-go gomod github.com/golang/go/src go1.25.0 strings/Index().(p.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#ImportPath., "/")
254 if slash < 0 {
255 continue // no slash: standard package
256 }
257 if p.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#ImportPath.[:slash] == domain {
258 // Package comes from bad domain and has no import comment.
259 // Report an error each time this package is imported.
260 canonical[p.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#ImportPath.] = canonicalNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/canonicalName#{}
261
262 // TODO(adonovan): should we make an HTTP request to
263 // see if there's an HTTP redirect, a "go-import" meta tag,
264 // or an import comment in the latest revision?
265 // It would duplicate a lot of logic from "go get".
266 }
267 break
268 }
269 }
270 }
271
272 // Find all clients (direct importers) of canonical packages.
273 // These are the packages that need fixing up.
274 clients := make(map[*listPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#]bool)
275 for path := range canonical {
276 for client := range importedBy[path] {
277 clients[client] = true
278 }
279 }
280
281 // Restrict rewrites to the set of packages specified by the user.
282 if len(packages) == 1 && (packages[0] == "all" || packages[0] == "...") {
283 // no restriction
284 } else {
285 pkgs, err := listscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/list().(packages...)
286 if err != nil {
287 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().(stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/stderr., "importfix: %v\n", err)
288 return false
289 }
290 seen := make(map[string]bool)
291 for _, p := range pkgs {
292 seen[p.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#ImportPath.] = true
293 }
294 for client := range clients {
295 if !seen[client.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#ImportPath.] {
296 delete(clients, client)
297 }
298 }
299 }
300
301 // Rewrite selected client packages.
302 ok := true
303 for client := range clients {
304 if !rewritePackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/rewritePackage().(client, canonical) {
305 ok = false
306
307 // There were errors.
308 // Show direct and indirect imports of client.
309 seen := make(map[string]bool)
310 var direct, indirect []string
311 for p := range importedBy[client.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#ImportPath.] {
312 direct = append(direct, p.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#ImportPath.)
313 seen[p.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#ImportPath.] = true
314 }
315
316 var visit func(path string)
317 visit = func(path string) {
318 for q := range importedBy[path] {
319 qpath := q.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#ImportPath.
320 if !seen[qpath] {
321 seen[qpath] = true
322 indirect = append(indirect, qpath)
323 visit(qpath)
324 }
325 }
326 }
327
328 if direct != nil {
329 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().(stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/stderr., "\timported directly by:\n")
330 sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Stringsscip-go gomod github.com/golang/go/src go1.25.0 sort/Strings().(direct)
331 for _, path := range direct {
332 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().(stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/stderr., "\t\t%s\n", path)
333 visit(path)
334 }
335
336 if indirect != nil {
337 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().(stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/stderr., "\timported indirectly by:\n")
338 sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Stringsscip-go gomod github.com/golang/go/src go1.25.0 sort/Strings().(indirect)
339 for _, path := range indirect {
340 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().(stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/stderr., "\t\t%s\n", path)
341 }
342 }
343 }
344 }
345 }
346
347 return ok
348 }
349
350 // Invariant: false result => error already printed.
351 func rewritePackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/rewritePackage().(client *listPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#, canonical map[string]canonicalNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/canonicalName#) bool {
352 ok := true
353
354 used := make(map[string]bool)
355 var filenames []string
356 filenames = append(filenames, client.GoFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#GoFiles....)
357 filenames = append(filenames, client.TestGoFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#TestGoFiles....)
358 filenames = append(filenames, client.XTestGoFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#XTestGoFiles....)
359 var first bool
360 for _, filename := range filenames {
361 if !first {
362 first = true
363 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().(stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/stderr., "%s\n", client.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#ImportPath.)
364 }
365 err := rewriteFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/rewriteFile().(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().(client.Dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#Dir., filename), canonical, used)
366 if err != nil {
367 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().(stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/stderr., "\tERROR: %v\n", err)
368 ok = false
369 }
370 }
371
372 // Show which imports were renamed in this package.
373 var keys []string
374 for key := range used {
375 keys = append(keys, key)
376 }
377 sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Stringsscip-go gomod github.com/golang/go/src go1.25.0 sort/Strings().(keys)
378 for _, key := range keys {
379 if p := canonical[key]; p.pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/canonicalName#path. != "" {
380 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().(stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/stderr., "\tfixed: %s -> %s\n", key, p.pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/canonicalName#path.)
381 } else {
382 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().(stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/stderr., "\tERROR: %s has no import comment\n", key)
383 ok = false
384 }
385 }
386
387 return ok
388 }
389
390 // rewriteFile reads, modifies, and writes filename, replacing all imports
391 // of packages P in canonical by canonical[P].
392 // It records in used which canonical packages were imported.
393 // used[P]=="" indicates that P was imported but its canonical path is unknown.
394 func rewriteFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/rewriteFile().(filename string, canonical map[string]canonicalNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/canonicalName#, used map[string]bool) error {
395 fset := tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NewFileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NewFileSet().()
396 f, err := parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseFilescip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseFile().(fset, filename, nil, parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseCommentsscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseComments.)
397 if err != nil {
398 return err
399 }
400 var changed bool
401 for _, imp := range f.Importsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Imports. {
402 impPath, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Unquotescip-go gomod github.com/golang/go/src go1.25.0 strconv/Unquote().(imp.Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Path..Valuescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#Value.)
403 if err != nil {
404 logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printfscip-go gomod github.com/golang/go/src go1.25.0 log/Printf().("%s: bad import spec %q: %v",
405 fset.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(imp.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Pos().()), imp.Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Path..Valuescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#Value., err)
406 continue
407 }
408 canon, ok := canonical[impPath]
409 if !ok {
410 continue // import path is canonical
411 }
412
413 used[impPath] = true
414
415 if canon.pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/canonicalName#path. == "" {
416 // The canonical path is unknown (a -baddomain).
417 // Show the offending import.
418 // TODO(adonovan): should we show the actual source text?
419 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().(stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/stderr., "\t%s:%d: import %q\n",
420 shortPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/shortPath().(filename),
421 fset.Positionscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#Position().(imp.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Pos().()).Linescip-go gomod github.com/golang/go/src go1.25.0 `go/token`/Position#Line., impPath)
422 continue
423 }
424
425 changed = true
426
427 imp.Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Path..Valuescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#Value. = strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Quotescip-go gomod github.com/golang/go/src go1.25.0 strconv/Quote().(canon.pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/canonicalName#path.)
428
429 // Add a renaming import if necessary.
430 //
431 // This is a guess at best. We can't see whether a 'go
432 // get' of the canonical import path would have the same
433 // name or not. Assume it's the last segment.
434 newBase := pathscip-go gomod github.com/golang/go/src go1.25.0 path/.Basescip-go gomod github.com/golang/go/src go1.25.0 path/Base().(canon.pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/canonicalName#path.)
435 if imp.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Name. == nil && newBase != canon.namescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/canonicalName#name. {
436 imp.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Name. = &astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#{Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name.: canon.namescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/canonicalName#name.}
437 }
438 }
439
440 if changed && !*dryrunscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/dryrun. {
441 var buf 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#
442 if err := formatscip-go gomod github.com/golang/go/src go1.25.0 `go/format`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/format`/Node().(&buf, fset, f); err != nil {
443 return 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().("%s: couldn't format file: %v", filename, err)
444 }
445 return writeFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/writeFile.(filename, buf.Bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#Bytes().(), 0644)
446 }
447
448 return nil
449 }
450
451 // listPackage corresponds to the output of go list -json,
452 // but only the fields we need.
453 type listPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage# struct {
454 Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#Name. string
455 Dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#Dir. string
456 ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#ImportPath. string
457 GoFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#GoFiles. []string
458 TestGoFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#TestGoFiles. []string
459 XTestGoFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#XTestGoFiles. []string
460 Importsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#Imports. []string
461 TestImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#TestImports. []string
462 XTestImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#XTestImports. []string
463 ImportCommentscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#ImportComment. string
464 Errorscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#Error. *packageErrorscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/packageError# // error loading package
465 }
466
467 // A packageError describes an error loading information about a package.
468 type packageErrorscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/packageError# struct {
469 ImportStackscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/packageError#ImportStack. []string // shortest path from package named on command line to this one
470 Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/packageError#Pos. string // position of error
471 Errscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/packageError#Err. string // the error itself
472 }
473
474 func (e packageErrorscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/packageError#) Errorscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/packageError#Error().() string {
475 if e.Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/packageError#Pos. != "" {
476 return e.Posscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/packageError#Pos. + ": " + e.Errscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/packageError#Err.
477 }
478 return e.Errscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/packageError#Err.
479 }
480
481 // list runs 'go list' with the specified arguments and returns the
482 // metadata for matching packages.
483 func listscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/list().(args ...string) ([]*listPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#, error) {
484 cmd := execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.Commandscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Command().("go", append([]string{"list", "-e", "-json"}, args...)...)
485 cmd.Stdoutscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Stdout. = 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#)
486 cmd.Stderrscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Stderr. = stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/stderr.
487 if err := cmd.Runscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Run().(); err != nil {
488 return nil, err
489 }
490
491 dec := jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/.NewDecoderscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/NewDecoder().(cmd.Stdoutscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Stdout..(ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Readerscip-go gomod github.com/golang/go/src go1.25.0 io/Reader#))
492 var pkgs []*listPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#
493 for {
494 var p listPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/listPackage#
495 if err := dec.Decodescip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Decoder#Decode().(&p); err == ioscip-go gomod github.com/golang/go/src go1.25.0 io/.EOFscip-go gomod github.com/golang/go/src go1.25.0 io/EOF. {
496 break
497 } else if err != nil {
498 return nil, err
499 }
500 pkgs = append(pkgs, &p)
501 }
502 return pkgs, nil
503 }
504
505 // cwd contains the current working directory of the tool.
506 //
507 // It is initialized directly so that its value will be set for any other
508 // package variables or init functions that depend on it, such as the gopath
509 // variable in main_test.go.
510 var cwdscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/cwd. string = func() string {
511 cwd, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Getwdscip-go gomod github.com/golang/go/src go1.25.0 os/Getwd().()
512 if err != nil {
513 logscip-go gomod github.com/golang/go/src go1.25.0 log/.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 log/Fatalf().("os.Getwd: %v", err)
514 }
515 return cwd
516 }()
517
518 // shortPath returns an absolute or relative name for path, whatever is shorter.
519 // Plundered from $GOROOT/src/cmd/go/build.go.
520 func shortPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/shortPath().(path string) string {
521 if rel, err := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Relscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Rel().(cwdscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/fiximports`/cwd., path); err == nil && len(rel) < len(path) {
522 return rel
523 }
524 return path
525 }