Alan Donovan | 3cded4a | 2014-09-09 18:39:26 -0400 | [diff] [blame] | 1 | // Copyright 2014 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 importgraph computes the forward and reverse import |
| 6 | // dependency graphs for all packages in a Go workspace. |
David Symonds | 24257c8 | 2014-12-09 15:00:58 +1100 | [diff] [blame] | 7 | package importgraph // import "golang.org/x/tools/refactor/importgraph" |
Alan Donovan | 3cded4a | 2014-09-09 18:39:26 -0400 | [diff] [blame] | 8 | |
| 9 | import ( |
| 10 | "go/build" |
| 11 | "sync" |
| 12 | |
Andrew Gerrand | 5ebbcd1 | 2014-11-10 08:50:40 +1100 | [diff] [blame] | 13 | "golang.org/x/tools/go/buildutil" |
Alan Donovan | 3cded4a | 2014-09-09 18:39:26 -0400 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | // A Graph is an import dependency graph, either forward or reverse. |
| 17 | // |
| 18 | // The graph maps each node (a package import path) to the set of its |
| 19 | // successors in the graph. For a forward graph, this is the set of |
| 20 | // imported packages (prerequisites); for a reverse graph, it is the set |
| 21 | // of importing packages (clients). |
| 22 | // |
Alan Donovan | ac02106 | 2016-02-22 15:26:12 -0500 | [diff] [blame] | 23 | // Graph construction inspects all imports in each package's directory, |
| 24 | // including those in _test.go files, so the resulting graph may be cyclic. |
Alan Donovan | 3cded4a | 2014-09-09 18:39:26 -0400 | [diff] [blame] | 25 | type Graph map[string]map[string]bool |
| 26 | |
| 27 | func (g Graph) addEdge(from, to string) { |
| 28 | edges := g[from] |
| 29 | if edges == nil { |
| 30 | edges = make(map[string]bool) |
| 31 | g[from] = edges |
| 32 | } |
| 33 | edges[to] = true |
| 34 | } |
| 35 | |
| 36 | // Search returns all the nodes of the graph reachable from |
| 37 | // any of the specified roots, by following edges forwards. |
| 38 | // Relationally, this is the reflexive transitive closure. |
| 39 | func (g Graph) Search(roots ...string) map[string]bool { |
| 40 | seen := make(map[string]bool) |
| 41 | var visit func(x string) |
| 42 | visit = func(x string) { |
| 43 | if !seen[x] { |
| 44 | seen[x] = true |
| 45 | for y := range g[x] { |
| 46 | visit(y) |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | for _, root := range roots { |
| 51 | visit(root) |
| 52 | } |
| 53 | return seen |
| 54 | } |
| 55 | |
Alan Donovan | e079f6c | 2015-01-21 14:13:33 -0500 | [diff] [blame] | 56 | // Build scans the specified Go workspace and builds the forward and |
Alan Donovan | 3cded4a | 2014-09-09 18:39:26 -0400 | [diff] [blame] | 57 | // reverse import dependency graphs for all its packages. |
Alan Donovan | fa833fd | 2015-12-18 14:11:09 -0500 | [diff] [blame] | 58 | // It also returns a mapping from canonical import paths to errors for packages |
Alan Donovan | a986643 | 2015-06-04 18:38:33 -0400 | [diff] [blame] | 59 | // whose loading was not entirely successful. |
| 60 | // A package may appear in the graph and in the errors mapping. |
Alan Donovan | fa833fd | 2015-12-18 14:11:09 -0500 | [diff] [blame] | 61 | // All package paths are canonical and may contain "/vendor/". |
Alan Donovan | 3cded4a | 2014-09-09 18:39:26 -0400 | [diff] [blame] | 62 | func Build(ctxt *build.Context) (forward, reverse Graph, errors map[string]error) { |
Alan Donovan | 41f0d01 | 2014-12-03 09:56:23 -0500 | [diff] [blame] | 63 | type importEdge struct { |
| 64 | from, to string |
| 65 | } |
Alan Donovan | 3cded4a | 2014-09-09 18:39:26 -0400 | [diff] [blame] | 66 | type pathError struct { |
| 67 | path string |
| 68 | err error |
| 69 | } |
Alan Donovan | 41f0d01 | 2014-12-03 09:56:23 -0500 | [diff] [blame] | 70 | |
| 71 | ch := make(chan interface{}) |
Alan Donovan | 3cded4a | 2014-09-09 18:39:26 -0400 | [diff] [blame] | 72 | |
Alan Donovan | 3a85b8d | 2015-12-30 14:17:09 -0500 | [diff] [blame] | 73 | go func() { |
| 74 | sema := make(chan int, 20) // I/O concurrency limiting semaphore |
| 75 | var wg sync.WaitGroup |
| 76 | buildutil.ForEachPackage(ctxt, func(path string, err error) { |
Alan Donovan | 41f0d01 | 2014-12-03 09:56:23 -0500 | [diff] [blame] | 77 | if err != nil { |
| 78 | ch <- pathError{path, err} |
| 79 | return |
| 80 | } |
Alan Donovan | a986643 | 2015-06-04 18:38:33 -0400 | [diff] [blame] | 81 | |
Alan Donovan | 3a85b8d | 2015-12-30 14:17:09 -0500 | [diff] [blame] | 82 | wg.Add(1) |
| 83 | go func() { |
| 84 | defer wg.Done() |
Alan Donovan | fa833fd | 2015-12-18 14:11:09 -0500 | [diff] [blame] | 85 | |
Alan Donovan | 3a85b8d | 2015-12-30 14:17:09 -0500 | [diff] [blame] | 86 | sema <- 1 |
Alan Donovan | 1071209 | 2016-01-08 13:18:57 -0500 | [diff] [blame] | 87 | bp, err := ctxt.Import(path, "", 0) |
Alan Donovan | 3a85b8d | 2015-12-30 14:17:09 -0500 | [diff] [blame] | 88 | <-sema |
Alan Donovan | fa833fd | 2015-12-18 14:11:09 -0500 | [diff] [blame] | 89 | |
Alan Donovan | 3a85b8d | 2015-12-30 14:17:09 -0500 | [diff] [blame] | 90 | if err != nil { |
| 91 | if _, ok := err.(*build.NoGoError); ok { |
| 92 | // empty directory is not an error |
| 93 | } else { |
| 94 | ch <- pathError{path, err} |
Alan Donovan | fa833fd | 2015-12-18 14:11:09 -0500 | [diff] [blame] | 95 | } |
Alan Donovan | 3a85b8d | 2015-12-30 14:17:09 -0500 | [diff] [blame] | 96 | // Even in error cases, Import usually returns a package. |
Alan Donovan | fa833fd | 2015-12-18 14:11:09 -0500 | [diff] [blame] | 97 | } |
Alan Donovan | fa833fd | 2015-12-18 14:11:09 -0500 | [diff] [blame] | 98 | |
Alan Donovan | 3a85b8d | 2015-12-30 14:17:09 -0500 | [diff] [blame] | 99 | // absolutize resolves an import path relative |
| 100 | // to the current package bp. |
| 101 | // The absolute form may contain "vendor". |
| 102 | // |
| 103 | // The vendoring feature slows down Build by 3×. |
| 104 | // Here are timings from a 1400 package workspace: |
| 105 | // 1100ms: current code (with vendor check) |
| 106 | // 880ms: with a nonblocking cache around ctxt.IsDir |
| 107 | // 840ms: nonblocking cache with duplicate suppression |
| 108 | // 340ms: original code (no vendor check) |
| 109 | // TODO(adonovan): optimize, somehow. |
Alan Donovan | 1071209 | 2016-01-08 13:18:57 -0500 | [diff] [blame] | 110 | memo := make(map[string]string) |
| 111 | absolutize := func(path string) string { |
| 112 | canon, ok := memo[path] |
| 113 | if !ok { |
| 114 | sema <- 1 |
| 115 | bp2, _ := ctxt.Import(path, bp.Dir, build.FindOnly) |
| 116 | <-sema |
Alan Donovan | 3a85b8d | 2015-12-30 14:17:09 -0500 | [diff] [blame] | 117 | |
Alan Donovan | 1071209 | 2016-01-08 13:18:57 -0500 | [diff] [blame] | 118 | if bp2 != nil { |
| 119 | canon = bp2.ImportPath |
| 120 | } else { |
| 121 | canon = path |
Alan Donovan | 3a85b8d | 2015-12-30 14:17:09 -0500 | [diff] [blame] | 122 | } |
Alan Donovan | 1071209 | 2016-01-08 13:18:57 -0500 | [diff] [blame] | 123 | memo[path] = canon |
Alan Donovan | 3a85b8d | 2015-12-30 14:17:09 -0500 | [diff] [blame] | 124 | } |
Alan Donovan | 1071209 | 2016-01-08 13:18:57 -0500 | [diff] [blame] | 125 | return canon |
Alan Donovan | a986643 | 2015-06-04 18:38:33 -0400 | [diff] [blame] | 126 | } |
Alan Donovan | 3a85b8d | 2015-12-30 14:17:09 -0500 | [diff] [blame] | 127 | |
| 128 | if bp != nil { |
| 129 | for _, imp := range bp.Imports { |
| 130 | ch <- importEdge{path, absolutize(imp)} |
| 131 | } |
| 132 | for _, imp := range bp.TestImports { |
| 133 | ch <- importEdge{path, absolutize(imp)} |
| 134 | } |
| 135 | for _, imp := range bp.XTestImports { |
| 136 | ch <- importEdge{path, absolutize(imp)} |
| 137 | } |
Alan Donovan | a986643 | 2015-06-04 18:38:33 -0400 | [diff] [blame] | 138 | } |
Alan Donovan | 3a85b8d | 2015-12-30 14:17:09 -0500 | [diff] [blame] | 139 | |
| 140 | }() |
| 141 | }) |
Alan Donovan | 41f0d01 | 2014-12-03 09:56:23 -0500 | [diff] [blame] | 142 | wg.Wait() |
| 143 | close(ch) |
| 144 | }() |
Alan Donovan | 3cded4a | 2014-09-09 18:39:26 -0400 | [diff] [blame] | 145 | |
Alan Donovan | 41f0d01 | 2014-12-03 09:56:23 -0500 | [diff] [blame] | 146 | forward = make(Graph) |
| 147 | reverse = make(Graph) |
| 148 | |
| 149 | for e := range ch { |
| 150 | switch e := e.(type) { |
| 151 | case pathError: |
| 152 | if errors == nil { |
| 153 | errors = make(map[string]error) |
| 154 | } |
| 155 | errors[e.path] = e.err |
| 156 | |
| 157 | case importEdge: |
| 158 | if e.to == "C" { |
| 159 | continue // "C" is fake |
| 160 | } |
| 161 | forward.addEdge(e.from, e.to) |
| 162 | reverse.addEdge(e.to, e.from) |
| 163 | } |
| 164 | } |
Alan Donovan | 3cded4a | 2014-09-09 18:39:26 -0400 | [diff] [blame] | 165 | |
| 166 | return forward, reverse, errors |
| 167 | } |