From 8ed41786dd33029c05ed68d3d3b899ddc483d4ee Mon Sep 17 00:00:00 2001 From: Farid Zakaria Date: Tue, 26 Aug 2025 12:38:21 -0700 Subject: [PATCH] enable_workspace when bazel verison > 8 If you don't want workspace enabled and are on version 8, unused_deps did not work due to the lack of workspace support. You could either fix this by adding it to your project's bazelrc however we conditionally enable it here depending on the version of bazel being executed. This does have the side-effect that it seems to cause a rebuild due to the arguments of the CLI changing. --- unused_deps/unused_deps.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/unused_deps/unused_deps.go b/unused_deps/unused_deps.go index ef5d8f40b..ca36276f8 100644 --- a/unused_deps/unused_deps.go +++ b/unused_deps/unused_deps.go @@ -76,6 +76,37 @@ javac_params = aspect( ` ) +/** + * Parses `bazel version` output and returns the version string. + * Example output: + * Build label: 8.2.1 + * Build target: bazel-out/darwin-fastbuild/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar + * Build time: Wed Jan 27 + */ +func bazelMajorVersion() (int, error) { + out, err := cmdWithStderr(*buildTool, "version").Output() + if err != nil { + return 0, err + } + lines := strings.Split(string(out), "\n") + for _, line := range lines { + if strings.HasPrefix(line, "Build label: ") { + version := strings.TrimPrefix(line, "Build label: ") + parts := strings.Split(version, ".") + if len(parts) < 1 { + return 0, fmt.Errorf("could not parse version from 'bazel version' output: %s", version) + } + var major int + _, err := fmt.Sscanf(parts[0], "%d", &major) + if err != nil { + return 0, fmt.Errorf("could not parse major version from 'bazel version' output: %s", version) + } + return major, nil + } + } + return 0, errors.New("could not find version in 'bazel version' output") +} + func stringList(name, help string) func() []string { f := flag.String(name, "", help) return func() []string { @@ -381,6 +412,9 @@ func main() { buildCmd = append(buildCmd, "--output_groups=+unused_deps_outputs") buildCmd = append(buildCmd, "--override_repository=unused_deps="+aspectDir) buildCmd = append(buildCmd, "--aspects=@@unused_deps//:unused_deps.bzl%javac_params") + if version, err := bazelMajorVersion(); err == nil && version >= 8 { + buildCmd = append(buildCmd, "--enable_workspace=true") + } buildCmd = append(buildCmd, buildOptions()...) blazeArgs := append(buildCmd, targetPatterns...)