Repository navigation
Expand file tree
/
Copy pathreach.go
More file actions
205 lines (186 loc) · 7.25 KB
/
Copy pathreach.go
File metadata and controls
205 lines (186 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package sitec
import (
"fmt"
"strings"
)
// skippedUnreachableSummaryFmt names every skipped package in one line. Only
// packages that already declare assets reach this point, so "skipped" means
// their styles and icons will not ship — the silent failure this diagnostic
// exists to catch. It used to be one log line per package
// (skippedUnreachableFmt): with Extractor's cache cold on every process
// restart, a project with a dozen unreachable sibling packages flooded the
// TUI with an identical-shaped line per package, on every boot. One line
// keeps the full information (the complete list) without the per-package
// noise. It only fires when verbose logging is on and the reachability data
// is complete (see reachabilityPartialFmt) — a partial computation must not
// be reported as a confident list of "these won't ship".
const skippedUnreachableSummaryFmt = "sitec: %d package(s) skipped — they declare assets but are not in the ./... build graph; " +
"their styles and icons will NOT ship: %s"
// reachabilityProbeFailedFmt reports a single build-target probe failure
// (e.g. `go list -deps ./...` for GOOS=js GOARCH=wasm). Logged only when
// verbose, since a probe failure is common on a cold module cache and
// resolves itself on the next scan — see reachabilityPartialFmt for the
// user-facing consequence.
const reachabilityProbeFailedFmt = "sitec: reachability probe failed for GOOS=%q GOARCH=%q: %v"
// reachSet is the set of import paths in the build graph of the start
// directory, across every configuration the artifact is compiled for.
type reachSet map[string]bool
// GraphLister returns the transitive import paths of pattern for the given
// GOOS/GOARCH. Injected so tests don't need a real toolchain.
type GraphLister func(rootDir, pattern, goos, goarch string) ([]string, error)
// buildTargets are the configurations the artifact is compiled for. The
// reachable set is their UNION: a component that only imports the WASM
// client never shows up in the server's graph, and dropping it would lose
// its styles.
var buildTargets = []struct{ GOOS, GOARCH string }{
{"", ""}, // native: the server binary and the SSR extractor
{"js", "wasm"}, // the browser client and edge.wasm
}
type reachability struct {
set reachSet
known bool // false => don't filter: no target succeeded, data unusable
// partial is true when at least one build target's probe failed (e.g. a
// cold module cache on process startup). The union is then INCOMPLETE —
// treating it as ground truth risks excluding a package that is, in
// fact, reachable, silently dropping its styles. Callers must treat
// partial like !known for filtering purposes: better to keep an
// actually-unreachable package around for one extra scan than to drop a
// reachable one.
partial bool
}
// destinationTags are the build-tag sets probed for every platform. The
// destination axis (browser) is orthogonal to the platform axis (GOOS):
// web/browser/main.go is built with -tags browser, while the css.go files of the packages it
// imports are `!wasm` — so webtyp.com/css is only reached by the NATIVE graph
// WITH -tags=browser, and the js/wasm graph without the tag is edge.wasm.
var destinationTags = []string{"", browserBuildTag}
// goListDeps is the default GraphLister: the union of `go list -deps` over
// destinationTags for the given platform, run through tc. It is the only
// lister — a second copy once missed the browser tag and every browser-only
// package was dropped.
func goListDeps(tc Toolchain) GraphLister {
return func(rootDir, pattern, goos, goarch string) ([]string, error) {
var out []string
for _, tags := range destinationTags {
deps, err := listDeps(tc, rootDir, pattern, goos, goarch, tags)
if err != nil {
return nil, err
}
out = append(out, deps...)
}
return out, nil
}
}
func listDeps(tc Toolchain, rootDir, pattern, goos, goarch, tags string) ([]string, error) {
args := []string{"-e", "-deps"}
if tags != "" {
args = append(args, "-tags="+tags)
}
args = append(args, pattern)
var env []string
if goos != "" {
env = append(env, "GOOS="+goos)
}
if goarch != "" {
env = append(env, "GOARCH="+goarch)
}
var data []byte
var err error
if len(env) > 0 {
data, err = tc.ListEnv(rootDir, env, args...)
} else {
data, err = tc.List(rootDir, args...)
}
if err != nil {
return nil, err
}
lines := strings.Split(string(data), "\n")
var out []string
for _, line := range lines {
trimmed := strings.TrimSpace(line)
if trimmed != "" {
out = append(out, trimmed)
}
}
return out, nil
}
// closeOverAssetImports adds to the set every package that an asset file of a
// reachable package imports, until nothing changes. The extractor compiles
// those files natively, so their imports are in its build even when no probe
// sees them: web/browser/main.go is `wasm`-only and css.go is `!wasm`, so a
// component reached only from the browser shows up in the js/wasm graph
// without its css.go, and in no graph with it (components/targetlist →
// components/listgap). Without this the package was skipped, the generated
// extractor did not compile, and the app shipped no style.css.
func (r reachability) closeOverAssetImports(pkgs []module, scan func(dir string) (packageFeatures, error)) error {
if !r.known || r.partial {
return nil
}
scanned := make(map[string]bool)
for changed := true; changed; {
changed = false
for _, m := range pkgs {
// A package's buildassets/ folder holds its asset producers (BUILD_DESTINATIONS D27/D30/D34): it is never in
// the browser graph, so it counts as reached when the package that owns it is.
if owner, ok := buildAssetsOwner(m.path); ok && !r.set[m.path] && r.set[owner] {
r.set[m.path] = true
changed = true
}
if m.dir == "" || scanned[m.path] || !r.set[m.path] {
continue
}
scanned[m.path] = true
feats, err := scan(m.dir)
if err != nil {
return err
}
for imp := range feats.ProducerImports {
if !r.set[imp] {
r.set[imp] = true
changed = true
}
}
}
}
return nil
}
// computeReachability computes the union of reachable packages across every
// build target. log receives a diagnostic when a target's probe fails; pass
// nil to stay silent (non-verbose callers).
func computeReachability(rootDir string, lister GraphLister, log func(...any)) reachability {
if lister == nil {
lister = goListDeps(NewExecToolchain())
}
set := make(reachSet)
anySuccess := false
anyFailure := false
for _, target := range buildTargets {
deps, err := lister(rootDir, "./...", target.GOOS, target.GOARCH)
if err != nil {
anyFailure = true
if log != nil {
log(fmt.Sprintf(reachabilityProbeFailedFmt, target.GOOS, target.GOARCH, err))
}
continue
}
anySuccess = true
for _, dep := range deps {
set[dep] = true
}
}
if !anySuccess {
return reachability{set: nil, known: false}
}
return reachability{set: set, known: true, partial: anyFailure}
}
// buildAssetsOwner returns "example.com/x" for "example.com/x/buildassets" (and for any package below it), false when path
// has no buildassets element.
func buildAssetsOwner(path string) (string, bool) {
parts := strings.Split(path, "/")
for i := len(parts) - 1; i > 0; i-- {
if parts[i] == buildAssetsFolder {
return strings.Join(parts[:i], "/"), true
}
}
return "", false
}