bazel_workspace
Native Bazel workspace seed.
Description#
bazel_workspace reads the Bazel graph through
bazel query and materializes every
rule the query returns as a bazel_target graph node. When a graph target
is built, Struo reads Bazel's action graph for that target through
bazel aquery. A configured query selects
the default outputs, runfiles and validation closure. Struo executes that closure
in <workspace>/.struo/bazel-actions/, with declared source copies and
content-verified external repository files. Bazel supplies analysis and
external repository downloads; Struo supplies execution.
bazel build is not invoked in the ownership path.
Unsupported internal actions fall back to bazel <capability> for the
target. This includes opaque file writes and stamped build information when
required by the selected outputs. The provider records unsupported mnemonics.
Struo decides this from an action graph without file contents or parameter
files, before requesting the full graph. Delegated commands use the
workspace's own Bazel configuration, so native and delegated builds share
Bazel's caches. They remain uncached by Struo, with Bazel owning their outputs.
They are not cacheable native-action ownership.
Owned spawn actions declare their actual inputs, outputs and environment.
They are cacheable only with explicit toolchain_inputs; this declaration must
cover every implicit host dependency, including compiler subprocesses, system
headers and link libraries. Contents participate in action identity. Merely
declaring the compiler executable is insufficient. Without this declaration,
spawns bypass the cache. External-file staging, file writes, materialized
aliases and manifest-mapped runfiles remain cacheable. Aliases and runfiles are
copied by value rather than linked into mutable native outputs.
Owned tests use the exposed test launcher and manifest mapping, rule arguments,
environment, size, timeout and shard count. Test execution intentionally bypasses
the cache; compilation and runfiles preparation remain cacheable. Declare
additional native test command environment through test_env. Struo does not
infer .bazelrc test command options or implement flaky-test retries. Unsupported
test action shapes delegate. These contracts do not imply hermetic remote
execution or cross-host cache portability: host tool paths, native action
arguments and runfiles manifests can contain absolute paths.
Struo discovers a Bazel workspace automatically from MODULE.bazel,
WORKSPACE.bazel, or WORKSPACE. A repository without struo.toml can
therefore query, build, and test its rules with struo as soon as its native
workspace file is checked in. Discovery skips generated directories such as
bazel-bin, bazel-out, and bazel-testlogs.
The default query is kind("rule", //...), which returns every buildable
rule the workspace exposes. The query attribute overrides that expression
when only a subset of the graph should reach Struo. The exclude_packages
attribute drops named package prefixes from the default query without
rewriting it.
test_query independently selects default test roots from that graph. It does
not remove build targets or change explicit struo test <target> requests.
Struo does not infer Bazel test command filters from .bazelrc; declare the
matching query when the native suite excludes tagged tests. For example,
tests(//...) except attr("tags", "benchmark", //...) excludes benchmark-tagged
tests from targetless testing while keeping them available to build.
Each rule becomes one of three target kinds so the graph advertises only
the capabilities the underlying rule actually supports: _test rules become
bazel_test (build + test), _binary
rules become bazel_binary (build + run),
and everything else becomes bazel_target
(build only). Every generated target records the original Bazel label on
bazel_label and its rule class on bazel_rule_kind. The graph node name
folds / and : into _ and adds a bz_ prefix so the label survives
Struo's target-name grammar; two Bazel labels that would collide in that
mapping are reported as a resolver error instead of silently deduplicated.
struo query targets prints the label-to-name mapping.
Attributes#
| Attribute | Type | Required | Default | Description |
|---|---|---|---|---|
bazel | string | no | bazel | Bazel executable name or workspace-relative path. Resolves through bazelisk when installed. |
env | map<string,string> | no | {} | Declared environment for discovery, analysis, and execution. Empty values override inherited host variables. Native action environments reported by aquery take precedence. |
toolchain_inputs | list<string> | no | [] | Absolute host files and trees covering implicit toolchain inputs. Contents fingerprint owned spawn actions. Required for spawn caching, not a remote execution declaration. |
test_env | map<string,string> | no | {} | Additional declared test runtime environment. Overrides rule environment. |
coverage_per_test | bool | no | false | Measure generated native test rules as atomic invocations using Bazel coverage |
coverage_sources | list<string> | no | [] | Product source globs expected in each test rule's native instrumentation universe |
query | string | no | kind("rule", //...) | Bazel query expression used to enumerate rules. |
test_query | string | no | All enumerated test rules | Bazel query selecting default test roots from the enumerated graph, independently of build selection. |
exclude_packages | list<string> | no | [] | Package prefixes to strip from the default query. Each entry excludes both //<prefix>/... and //<prefix>:*. |
resolver_inputs | list<string> | no | srcs | Package-relative source globs supplied during graph loading. |
Providers and capabilities#
The workspace target emits bazel_workspace and exposes the build
capability. The resolver emits one bazel_target per rule; those children
carry the actual build, test, and run capabilities.
Direct use#
Discover and use the workspace without writing a manifest:
struo query targets
struo build
struo testTargetless build selects the single discovered workspace seed. Targetless
test selects the first-party Bazel test rules reported by its resolver.
Discovery does not create struo.toml.
To declare a portable environment, add an explicit workspace seed. For example, clear an unrelated host SDK installation during repository discovery and analysis:
[[target]]
name = "bazel"
kind = "bazel_workspace"
srcs = ["MODULE.bazel"]
[target.attrs.env]
ANDROID_HOME = ""
ANDROID_SDK_ROOT = ""The environment is forwarded to every generated target, including tests and delegated Bazel commands. Owned actions do not inherit the caller's environment. Environment declaration alone does not enable caching for actions that depend on undeclared host state.
Coexistence with Struo outputs#
Struo writes its own cache and outputs under .struo/. When Bazel walks the
workspace for a query, it also visits that directory unless the repository
lists it in a
.bazelignore
file. Add a single line to .bazelignore alongside MODULE.bazel:
.struoWithout that entry, a checked-in BUILD.bazel created by another native
integration can be picked up by bazel query and fail the resolver with a
missing rule loader.
Sources#
- Bazel query language describes the expressions used to enumerate rules during graph loading.
- Bazel command-line reference
documents the
bazel build,bazel test, andbazel runcommands the target kind delegates to.