3.2.7 Project Specifications & Flag Sets

extra
Bazel 9 (PROJECT.scl verified on 9.1.0)

Bazel knows what a target is, what a package is, and what a repository is. It does not know what a project is. That gap matters because projects carry policy: which flags produce a correct build, which configurations are sanctioned, and what "just build it" means for a given set of source files. Without a first-class project concept, that policy lives in .bazelrc layers, team wikis, and tribal knowledge, and the build system cannot enforce it.1

Project Specifications and Flag Sets ("flagsets") are an experimental feature that fills this gap. A PROJECT.scl file placed in the source tree defines named flag sets for the targets below it, and Bazel applies the right flags automatically based on which targets the developer builds.1,2,3

Why .bazelrc is not enough

The .bazelrc hierarchy covered in 3.2.1 .bazelrc Hierarchy handles the common case well: the workspace rc sets team defaults, named configs separate CI from local builds, and try-import gives each developer a personal override. Three structural limitations remain:1

No per-project scoping in monorepos. .bazelrc applies to the entire workspace. If two projects in the same repository need different flags, developers must remember which flags apply where and unset the others when switching context.1,3

Adding flags, not defining policy. .bazelrc stacks flags from multiple sources but never declares which combination is canonical. A user rc, a CI wrapper, or an IDE plugin can all inject flags that override project intent. At best this causes cache misses from configuration divergence. At worst it silently produces incorrect builds.1

Opaque to external tools. .bazelrc uses a custom format that is not Starlark, has no formal specification, and cannot be consumed by CI systems, IDEs, or analysis tools without reimplementing the Bazel rc parser.1

What a PROJECT.scl file looks like

The file must be named exactly PROJECT.scl. Bazel ignores a lowercase project.scl without any message, so a misspelled file looks like a feature that does nothing. The contents are SCL (Starlark Configuration Language), a strict subset of Starlark meant for declarative configuration, and the file must assign a top-level variable named project.1,3

The schema is defined as protocol buffers inside Bazel. Starlark constructors for it live in a generated file, project_proto.scl, which you copy from Bazel's source tree (src/main/protobuf/project/project_proto.scl) into your repository and load like any other .scl file. The project-scl-flagsets snippet uses this file for its main project:

load("//:project_proto.scl", "buildable_unit_pb2", "project_pb2")

# No enforcement_policy, so Bazel uses the default: WARN.
project = project_pb2.Project.create(
    buildable_units = [
        buildable_unit_pb2.BuildableUnit.create(
            name = "release",
            target_patterns = ["//warn/..."],
            flags = ["--compilation_mode=opt"],
            is_default = True,
            description = "Optimized build, applied when --scl_config is not set.",
        ),
        buildable_unit_pb2.BuildableUnit.create(
            name = "dev",
            target_patterns = ["//warn/..."],
            flags = ["--compilation_mode=dbg"],
            description = "Debug build, selected with --scl_config=dev.",
        ),
    ],
)

(warn/PROJECT.scl)

Each BuildableUnit is one named flag set: a name, the flags to apply, the target_patterns it covers, an optional is_default, and a description that Bazel shows to developers. Construct it with buildable_unit_pb2.BuildableUnit.create and add it to the project's buildable_units list.3

How Bazel picks the flags

For every top-level target, Bazel walks up the package path and uses the innermost PROJECT.scl it finds. Different subtrees can have their own files, so one monorepo can host several projects with independent flag sets. A nested file overrides its parent for the targets below it.1,3

Within that file, a build without --scl_config applies the unit whose is_default = True and whose target_patterns match the target. --scl_config=<name> selects a unit explicitly, the way --config=<name> selects a named rc config:3

bazel build //warn:mode                  # applies "release"
bazel build //warn:mode --scl_config=dev # applies "dev"

Bazel says what it chose. These two lines come from the snippet's first build:

INFO: Reading project settings from //warn:PROJECT.scl.
INFO: Applying flags from the config 'release' defined in //warn:PROJECT.scl: [--compilation_mode=opt]

An unknown --scl_config value fails, and the error lists every unit with its description, so the file doubles as the "how to build this project" page that would otherwise live in team docs. If the file defines units but none is a matching default, every build of that target must pass --scl_config explicitly. The snippet pins both errors.

target_patterns accepts specific targets, recursive patterns, and - exclusions such as -//some/path/.... That lets one file give different defaults to different subtrees, for example Mac and Linux code, or tests and production code.1,3

Enforcement policies

A project can declare how strictly Bazel treats flags that the developer adds on top of the selected unit. The policy is a single enforcement_policy field on the project, not on each unit. It takes a case-insensitive string and defaults to warn:3

PolicyBehavior
warn (default)Prints a warning listing the extra output-affecting flags. The build continues.
compatibleFails when an extra flag sets something the selected unit also sets to a different value. Unrelated flags only warn, and repeating the unit's own value is accepted.
strictFails when any extra output-affecting flag is present.
project = project_pb2.Project.create(
    # Fail when the command line or an rc file sets a flag this config also sets.
    enforcement_policy = "compatible",
    buildable_units = [
        # ...
    ],
)

(compatible/PROJECT.scl)

"Extra flags" means more than the command line. Bazel's messages say "command line or user bazelrc", but on Bazel 9.1.0 the snippet shows that a line in the checked-in workspace .bazelrc counts too. A --config=ci is reported as --config=ci, not as the flags it expands to. A compatible project therefore fails when the team's own .bazelrc sets a conflicting value:

ERROR: Cannot parse options: This build uses a project file (//compatible:PROJECT.scl) that does not allow conflicting flags in the command line or user bazelrc. Found ['--compilation_mode=dbg']. Please remove these flags or disable project file resolution via --noenforce_project_configs.

The warn policy has a less obvious consequence. In the snippet, bazel build //warn:mode --compilation_mode=dbg prints the warning and then builds in opt anyway. A conflicting value never takes effect: warn overrides it with the unit's value, while compatible and strict stop the build.

Two version-specific caveats matter in practice. Both are pinned by the snippet's assertion script, so they will show up as test failures once Bazel changes:

  • On Bazel 9.1.0 and 9.3.1, Bazel counts several --flag_alias=...@@rules_python+... entries as user flags. They come from rules_python, which the built-in bazel_tools module pulls into every workspace. As a result, warn and compatible projects print the "also sets output-affecting flags" warning on every build, even with an empty command line. A strict project fails even when the developer passes no flags at all.
  • --noenforce_project_configs turns project resolution off entirely: no PROJECT.scl is read and no unit is applied. This is the escape hatch Bazel's error messages recommend.

Treat compatible as the practical starting point. It catches real contradictions while still allowing additive flags such as a local --define.1,3 Reserve strict for a later Bazel release that no longer counts the module-registered aliases as user flags.

Flagsets do not limit where a setting travels

Take the board example from 4.7.4 Transition Boundaries & Gotchas: boards alpha and beta share a generic logging library, and each board builds in dbg or opt. The board value is set near the top of the graph and, by default, travels into every dependency. The logging library therefore gets one configured copy per board even when it compiles identically for both. Each copy costs analysis time and its own output directory.4

Because PROJECT.scl already holds flag policy, it is natural to expect it to fix this. Flagsets do not. buildable_units and enforcement_policy decide which values an invocation starts with and which extra flags it rejects. Once the build starts, the board value travels exactly as far as it would without a project file.

Limiting how far one setting travels is the job of setting scope, a separate mechanism that also uses PROJECT.scl. The setting opts in with scope = "project", and a PROJECT.scl next to it lists the directories where the value stays meaningful in its own field, project_directories:4,5

# //boards/BUILD.bazel
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")

string_flag(
    name = "board",
    build_setting_default = "none",
    scope = "project",
)
# //boards/PROJECT.scl
load("//:project_proto.scl", "project_pb2")

project = project_pb2.Project.create(
    project_directories = ["//firmware"],
)

Scope does not set the board. A transition that enters the project still does that, for example one per board image. Scope only decides where the value is dropped. Inside //firmware, targets see the board that the transition set. When analysis leaves it, for example into a logging library under //libs, the setting resets to none, so both boards can share one copy of the library for each compilation mode.4

Draw that boundary only where board identity really stops mattering. A library outside it that still selects board-specific sources now sees none and builds the wrong variant. Keep such a library inside the project, or pass the board-specific part to it through another explicit interface.4

Project scoping is experimental, and the Bazel 9.3.1 integration tests that exercise it enable --experimental_enable_scl_dialect.5 The flagsets snippet used earlier covers only mode selection and enforcement, not setting scope. 4.7.4 Transition Boundaries & Gotchas compares scopes with reset transitions, including how command-line values and exec dependencies behave.

Current limitations

The feature is still experimental. Three design constraints shape how far it can go today:1

Output-affecting flags only. Units can set flags that change build outputs, like --compilation_mode, but not flags that control how Bazel runs. Putting --jobs=4 in a unit fails with project flags don't support non-output affecting option: --jobs. The Bazel team has said it plans to loosen this.1

Single configuration per invocation. All top-level targets in one invocation must share the same flags. Building a target that has a project file together with a target that has none works: the project's flags apply to both, which helps incremental migration. Building targets owned by two different PROJECT.scl files fails:1

ERROR: Mismatching default configs for a multi-project build. Targets have different project settings:
  - //warn:mode -> //warn:PROJECT.scl
  - //warn/nested:mode -> //warn/nested:PROJECT.scl

A side effect is that bazel build //... in a repository with several projects fails until the invocation is narrowed to one project.

No transitive dependency configs. Flags come from the project of the top-level target and propagate down to its dependencies. A library cannot yet declare its own required configuration, so a shared library built for two projects gets whatever flags each top-level project sets.1

key takeaway

PROJECT.scl gives Bazel a canonical answer to "how do you build this project": the innermost file above a target names its flag sets, picks a default, and sets a policy for extra flags. Write it with the project_pb2 / buildable_unit_pb2 constructors, name the file exactly PROJECT.scl, and remember that the policy belongs to the whole project. The feature is experimental. On the tested Bazel 9.1.0 and 9.3.1 versions, module flag aliases make strict fail and warn noisy, so pilot with compatible rather than assuming this is a replacement for .bazelrc. Flagsets choose the starting flag values but do not limit how far a setting travels through dependencies. That is the job of setting scope: scope = "project" on the setting plus project_directories in its PROJECT.scl.

Check your understanding · 4 questions

1.What problem does PROJECT.scl solve that .bazelrc cannot?

Select one answer

2.A team wants //app builds to use --compilation_mode=opt unless a developer asks for the debug flag set. Which PROJECT.scl fragment does Bazel actually parse?

Select one answer

3.Match each enforcement_policy value to how Bazel treats an extra --compilation_mode=dbg when the selected unit sets --compilation_mode=opt:

Drag each answer onto the matching prompt, or click an answer and then click a prompt

Answers
warn (default)
compatible
strict

4.True or false about PROJECT.scl behavior on Bazel 9.1:

Choose True or False for each sentence

Building targets owned by two different PROJECT.scl files in one invocation fails, while building a project target together with a target that has no project file applies the project's flags to both.
Flags set in the checked-in workspace .bazelrc are exempt from enforcement policies, because only command-line flags count.
A unit may set --jobs=4 so that every build of the project uses four parallel jobs.
A file named project.scl in lowercase is ignored without any message.
0 of 4 answered

Footnotes

  1. Flagsets - Susan Steinman & Greg Estren, Google — motivation, project-file selection by walking up the tree, enforcement policies, target patterns, current limitations, and future plans (slides use a simplified syntax) ↩1 ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 ↩10 ↩11 ↩12 ↩13 ↩14

  2. State of the Union - Tobias Werth & John Field, Google — initial announcement of Project Specifications and Flag Sets as a shift toward project-centric configuration ↩

  3. Managing Bazel Flags in Monorepos with Flagsets (PROJECT.scl) — the shipped project_pb2 / buildable_unit_pb2 API, --scl_config, project-level enforcement policies, target_patterns syntax, and a real STRICT failure ↩1 ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9

  4. Taming Configurations Explosion and One Build, Many Configurations — project-scoped settings versus project defaults, the cost of unnecessary propagation, and the need to preserve settings that still matter. ↩1 ↩2 ↩3 ↩4

  5. Bazel — core implementation, documentation, and regression corpus, project.proto and flags_scoping_integration_tests.sh at 9.3.1 — separate policy and membership fields, and project-scope reset tests run with --experimental_enable_scl_dialect. ↩1 ↩2