|
| 1 | +package pkg |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sort" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +// BuildScopedUniverse returns a bazel query expression covering the scoped |
| 10 | +// universe of a seeded run: |
| 11 | +// |
| 12 | +// - each dirty package contributes a `//pkg:all` wildcard, so that targets |
| 13 | +// added to or removed from the package since the seed are picked up |
| 14 | +// (explicit labels from the seed would miss new targets and error on |
| 15 | +// deleted ones); |
| 16 | +// - carried labels — dirty* targets in unchanged packages (i.e. reverse |
| 17 | +// deps of the dirty packages) — are named explicitly via set(). These |
| 18 | +// labels are guaranteed to still exist because their BUILD files did not |
| 19 | +// change and macro (.bzl) changes trigger a full-rehash fallback. |
| 20 | +// |
| 21 | +// The returned expression is parenthesized so it can be substituted into a |
| 22 | +// larger query expression. |
| 23 | +func BuildScopedUniverse(dirtyPackages []string, carriedLabels []string) string { |
| 24 | + terms := make([]string, 0, len(dirtyPackages)+1) |
| 25 | + for _, pkg := range dirtyPackages { |
| 26 | + if pkg == "//" { |
| 27 | + terms = append(terms, "//:all") |
| 28 | + } else { |
| 29 | + terms = append(terms, pkg+":all") |
| 30 | + } |
| 31 | + } |
| 32 | + if len(carriedLabels) > 0 { |
| 33 | + sorted := append([]string(nil), carriedLabels...) |
| 34 | + sort.Strings(sorted) |
| 35 | + terms = append(terms, "set("+strings.Join(sorted, " ")+")") |
| 36 | + } |
| 37 | + if len(terms) == 0 { |
| 38 | + return "set()" |
| 39 | + } |
| 40 | + return "(" + strings.Join(terms, " + ") + ")" |
| 41 | +} |
| 42 | + |
| 43 | +// ScopeTargetsPattern narrows a targets pattern built over //... to the given |
| 44 | +// universe expression by substituting every occurrence of //... with the |
| 45 | +// universe. This preserves the semantics of filters expressed over the full |
| 46 | +// repo — e.g. `//... - attr(tags, "manual", //...)` becomes |
| 47 | +// `(U) - attr(tags, "manual", (U))` — while restricting evaluation to the |
| 48 | +// scoped universe, so manual-tag filtering behaves identically to a full run. |
| 49 | +// |
| 50 | +// Patterns that do not contain //... cannot be scoped this way; callers |
| 51 | +// should fall back to a full computation in that case. |
| 52 | +// |
| 53 | +// Known limitations of scoped mode versus a full //... query: |
| 54 | +// - --filter-incompatible-targets is a cquery-only concern; the seeded path |
| 55 | +// is only supported with --query-backend=query, which never filters |
| 56 | +// incompatible targets in full mode either. |
| 57 | +// - Targets in unchanged packages whose rule-generation depends on state |
| 58 | +// outside the package (e.g. macros) are not re-listed; any .bzl change |
| 59 | +// triggers a full-rehash fallback before reaching this point. |
| 60 | +func ScopeTargetsPattern(originalPattern string, universe string) (string, error) { |
| 61 | + var result strings.Builder |
| 62 | + replacements := 0 |
| 63 | + for start := 0; start < len(originalPattern); { |
| 64 | + idx := strings.Index(originalPattern[start:], "//...") |
| 65 | + if idx < 0 { |
| 66 | + result.WriteString(originalPattern[start:]) |
| 67 | + break |
| 68 | + } |
| 69 | + idx += start |
| 70 | + result.WriteString(originalPattern[start:idx]) |
| 71 | + // Do not rewrite repository-qualified wildcards such as @repo//.... |
| 72 | + if idx > 0 && isLabelCharacter(originalPattern[idx-1]) { |
| 73 | + result.WriteString("//...") |
| 74 | + } else { |
| 75 | + result.WriteString(universe) |
| 76 | + replacements++ |
| 77 | + } |
| 78 | + start = idx + len("//...") |
| 79 | + } |
| 80 | + if replacements == 0 { |
| 81 | + return "", fmt.Errorf("targets pattern %q does not contain //... and cannot be scoped", originalPattern) |
| 82 | + } |
| 83 | + return result.String(), nil |
| 84 | +} |
| 85 | + |
| 86 | +func isLabelCharacter(c byte) bool { |
| 87 | + return c == '@' || c == '_' || c == '-' || c == '.' || c == '+' || |
| 88 | + (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') |
| 89 | +} |
0 commit comments