diff --git a/cmd/featuretags/featuretags.go b/cmd/featuretags/featuretags.go index f3aae68cc..f30d67224 100644 --- a/cmd/featuretags/featuretags.go +++ b/cmd/featuretags/featuretags.go @@ -36,27 +36,19 @@ func main() { return } - var keep = map[featuretags.FeatureTag]bool{} + var keep []featuretags.FeatureTag for t := range strings.SplitSeq(*add, ",") { if t != "" { - for ft := range featuretags.Requires(featuretags.FeatureTag(t)) { - keep[ft] = true - } + keep = append(keep, featuretags.FeatureTag(t)) } } var tags []string - if keep[featuretags.CLI] { - tags = append(tags, "ts_include_cli") - } if *min { - for _, f := range slices.Sorted(maps.Keys(features)) { - if f == "" { - continue - } - if !keep[f] && f.IsOmittable() { - tags = append(tags, f.OmitTag()) - } - } + tags = featuretags.MinTags(keep...) + } else if slices.ContainsFunc(keep, func(ft featuretags.FeatureTag) bool { + return featuretags.Requires(ft).Contains(featuretags.CLI) + }) { + tags = append(tags, "ts_include_cli") } removeSet := set.Set[featuretags.FeatureTag]{} for v := range strings.SplitSeq(*remove, ",") { diff --git a/cmd/tailscaled/deps_test.go b/cmd/tailscaled/deps_test.go index 717cd33f1..4c74ca546 100644 --- a/cmd/tailscaled/deps_test.go +++ b/cmd/tailscaled/deps_test.go @@ -4,8 +4,6 @@ package main import ( - "maps" - "slices" "strings" "testing" @@ -304,13 +302,7 @@ func TestOmitUseProxy(t *testing.T) { } func minTags() string { - var tags []string - for _, f := range slices.Sorted(maps.Keys(featuretags.Features)) { - if f.IsOmittable() { - tags = append(tags, f.OmitTag()) - } - } - return strings.Join(tags, ",") + return strings.Join(featuretags.MinTags(), ",") } func TestMinTailscaledNoCLI(t *testing.T) { diff --git a/cmd/tsconnect/wasmbuild/wasmbuild.go b/cmd/tsconnect/wasmbuild/wasmbuild.go index b4c75d109..1b927f15e 100644 --- a/cmd/tsconnect/wasmbuild/wasmbuild.go +++ b/cmd/tsconnect/wasmbuild/wasmbuild.go @@ -14,7 +14,6 @@ import ( "os/exec" "path/filepath" "runtime" - "slices" "strings" "tailscale.com/feature/featuretags" @@ -113,23 +112,7 @@ type BuildInfo struct { // the same string (and therefore the same wasm bytes, given identical // inputs to `go build`). func Tags() string { - keep := map[featuretags.FeatureTag]bool{} - for _, ft := range Keep { - for dep := range featuretags.Requires(ft) { - keep[dep] = true - } - } - var tags []string - for ft := range featuretags.Features { - if ft == "" || !ft.IsOmittable() { - continue - } - if !keep[ft] { - tags = append(tags, ft.OmitTag()) - } - } - slices.Sort(tags) - return strings.Join(tags, ",") + return strings.Join(featuretags.MinTags(Keep...), ",") } // ProdCommand returns an *exec.Cmd that runs `go build` for diff --git a/feature/featuretags/featuretags.go b/feature/featuretags/featuretags.go index befd8d018..fa7ef9f49 100644 --- a/feature/featuretags/featuretags.go +++ b/feature/featuretags/featuretags.go @@ -4,7 +4,11 @@ // Package featuretags is a registry of all the ts_omit-able build tags. package featuretags -import "tailscale.com/util/set" +import ( + "slices" + + "tailscale.com/util/set" +) // CLI is a special feature in the [Features] map that works opposite // from the others: it is opt-in, rather than opt-out, having a different @@ -62,6 +66,28 @@ func RequiredBy(ft FeatureTag) set.Set[FeatureTag] { return s } +// MinTags returns the sorted Go build tags for a minimal build that +// includes only the features in keep and the features they require. +// Every other omittable feature in [Features] gets its ts_omit_ tag, and +// ts_include_cli is added if [CLI] is kept. +func MinTags(keep ...FeatureTag) []string { + kept := set.Set[FeatureTag]{} + for _, ft := range keep { + kept.AddSet(Requires(ft)) + } + var tags []string + if kept.Contains(CLI) { + tags = append(tags, "ts_include_cli") + } + for ft := range Features { + if ft != "" && ft.IsOmittable() && !kept.Contains(ft) { + tags = append(tags, ft.OmitTag()) + } + } + slices.Sort(tags) + return tags +} + // featureDependsOn reports whether feature a (directly or indirectly) depends on b. // It returns true if a == b. func featureDependsOn(a, b FeatureTag) bool { diff --git a/feature/featuretags/featuretags_test.go b/feature/featuretags/featuretags_test.go index c8a9f77ae..f62f0fa1b 100644 --- a/feature/featuretags/featuretags_test.go +++ b/feature/featuretags/featuretags_test.go @@ -87,6 +87,36 @@ func TestRequiredBy(t *testing.T) { } } +func TestMinTags(t *testing.T) { + has := func(tags []string, tag string) bool { return slices.Contains(tags, tag) } + + all := MinTags() + if !slices.IsSorted(all) { + t.Errorf("MinTags() not sorted: %v", all) + } + if has(all, "ts_include_cli") { + t.Errorf("MinTags() includes ts_include_cli") + } + for ft := range Features { + if ft.IsOmittable() && !has(all, ft.OmitTag()) { + t.Errorf("MinTags() missing %q", ft.OmitTag()) + } + } + + // Keeping webclient must also keep what it requires. + got := MinTags("webclient", CLI) + for _, tag := range []string{"ts_omit_webclient", "ts_omit_serve", "ts_omit_netstack"} { + if has(got, tag) { + t.Errorf("MinTags(webclient, cli) includes %q", tag) + } + } + for _, tag := range []string{"ts_include_cli", "ts_omit_drive"} { + if !has(got, tag) { + t.Errorf("MinTags(webclient, cli) missing %q", tag) + } + } +} + // Verify that all "ts_omit_foo" build tags are declared in featuretags.go func TestAllOmitBuildTagsDeclared(t *testing.T) { if _, err := exec.LookPath("git"); err != nil {