# .bazelproject file reference

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com.cn/en-us/help/idea/llms.txt).

A [project view file](https://www.jetbrains.com.cn/en-us/help/idea/bazel-project-view.html.md) is split into named sections and uses a Python-like format with two spaces indentation.

<a id="file_and_section_syntax"></a>

## File and section syntax

* The content after `#` until the end of the line is treated as a comment.

* Section headers start at the beginning of a line and use the `name: body` or `name body` format. Both a space or a colon are acceptable after a section name.

* Section body extends until the next section header.

<a id="section_types"></a>

## Section types

All named sections belong to one of the following types:

* Single value sections: one Boolean, Int, Path, or String value, for example: ```TEXT derive_targets_from_directories: true import_depth 2 ```

* List sections: multiple values separated with a space or a newline, for example: ```TEXT build_flags: --config=ci --cxxopt=-O2 ```

* Excludable list sections: multiple values separated with a space or a newline; entries starting with `-` are excluded. For example: ```TEXT targets: //app:all -//app:tests ```

<a id="multiple_occurrences"></a>

## Repeating sections

Sections can appear multiple times. If that happens, the following resolution rules apply:

* Single-value sections: the last occurrence wins.

* List sections: the sections are merged in the order of appearance.

* Excludable list sections: like with regular list sections, both included and excluded lists are merged in the order of appearance.

<a id="imports"></a>

## Imports

A project view can import other project views using `import` and `try_import`.

* `import path/to/file.bazelproject` Required import. If the file cannot be found, the project view is not considered valid.

* `try_import path/to/optional.bazelproject` Optional import. If the file cannot be found, the project view is still valid without importing the other project view.

IntelliJ IDEA parses all `import` entries first, then all `try_import` entries in the order they appear in the file. After the imports, the current file's own sections are processed.

You can use multiple imports in a project view. In this case, the imported list sections (for example, `targets`) are merged and single-value sections (for example, `bazel_binary`) override the previously encountered ones.

<a id="sections_reference"></a>

## Sections reference

This section contains the complete list of available sections grouped by type. For the list of section types and their syntax, refer to the [Section types](#section_types) chapter.

<a id="excludable_list_sections"></a>

### Excludable list sections

targets (List<Path>)
: A set of Bazel targets to include in the import. Supports the recursive wildcard for packages (`/...`). The more targets you have, the slower your IDE experience might be.
:
:
:
: Example:
:
:
:
:
: ```
: targets:
: //install/src/main/kotlin/org/jetbrains/bazel/install
: //executioncontext/projectview/...
: -//executioncontext/projectview/src/main/kotlin/org/jetbrains/bazel/projectview/parser/...
: ```

directories (List<Path>)
: The directories to be mapped into Bazel targets. Can be used with [derive_targets_from_directories](#derive_targets_from_directories) to automatically discover the relevant targets by scanning the specified directories.
:
:
:
: Example:
:
:
:
:
: ```
: directories:
: install/src/main/kotlin/org/jetbrains/bazel/install
: executioncontext/projectview/
: -executioncontext/projectview/src/main/kotlin/org/jetbrains/bazel/projectview/parser
: ```

<a id="list_sections"></a>

### List sections

build_flags (List<String>)
: A set of Bazel flags that is passed with all Bazel command invocations. By default, no flags are passed.
:
:
:
: Example:
:
:
:
:
: ```
: build_flags:
: --define=ij_product=intellij-latest
: ```

sync_flags (List<String>)
: A set of Bazel flags that is passed with Bazel command invocations during sync. By default, no flags are passed.
:
:
:
: Example:
:
:
:
:
: ```
: sync_flags:
: --define=ij_product=intellij-latest
: ```

debug_flags (List<String>)
: A set of Bazel flags that is passed to `bazel run` in debug mode.
:
:
:
: Example:
:
:
:
:
: ```
: debug_flags:
: --cxxopt=-g
: ```

enabled_rules (List<String>)
: Explicitly declares which external Bazel rulesets should be enabled for your project and overrides auto-detection, which can be incomplete for some setups.
:
:
:
: The plugin also enables the rulesets that a listed ruleset needs, such as `rules_kotlin` adds `rules_java`.
:
:
:
: Example:
:
:
:
:
: ```
: enabled_rules:
: rules_java
: rules_kotlin
: ```

import_run_configurations (List<Path>)
: A list of XML files to import as run configurations during Bazel sync.
:
:
:
: Example:
:
:
:
:
: ```
: import_run_configurations:
: tools/intellij/run_application.xml
: tools/intellij/run_tests.xml
: ```
:
:
:
: > **Note:**
: > Run configurations from Google's Bazel plugin are converted to the new format automatically upon import.
:
:
:
: > **Tip:**
: > For exporting run configurations to XML, refer to [Share run/debug configurations](https://www.jetbrains.com.cn/en-us/help/idea/run-debug-configuration.html.md#share-configurations)

index (List<String>)
: A list of glob patterns for files to index inside [directories](#directories), in addition to the sources, resources, and libraries defined by the project.
:
:
:
: * A pattern without a slash matches a file name in any directory, for example, `package.json` or `*.xml`.
:
: * A pattern with a slash matches a workspace-relative path prefix, for example, `docs/*`.
:
: * `*` indexes all files inside `directories`.
:
:
:
: Example:
:
:
:
:
: ```
: index:
: *.xml
: package.json
: docs/*
: ```
:
:
:
: > **Note:**
: > The plugin always indexes `BUILD`, `WORKSPACE`, and `MODULE.bazel` files, as well as files matching `*.bazel`, `*.bzl`, `*.bzlmod`, and `*.bazelproject`. You cannot exclude these files from indexing.

index_additional_files_in_directories (List<String>)
: This section is deprecated. Use [index](#index) instead. It accepts the same patterns.

<a id="single_value_sections"></a>

### Single-value sections

bazel_binary (Path)
: Path to Bazel, which will be used on the server for operations such as building a project or executing a Bazel query. If not specified, the server will use the binary from the `$PATH` variable.
:
:
:
: Example:
:
:
:
:
: ```
: bazel_binary: /usr/local/bin/bazel
: ```

derive_targets_from_directories (Boolean)
: Specifies if targets should be derived from the directories in the `directories` section. By default, targets are not derived (`false`), but the directories are not excluded from the project view.
:
:
:
: Example:
:
:
:
:
: ```
: derive_targets_from_directories: true
: ```

import_depth (Int)
: Specifies how many levels of Bazel targets' dependencies should be imported as modules. Only the targets that are present in the workspace are imported. You can use a negative value to import all transitive dependencies. The default is `-1`, meaning that all transitive context is included.
:
:
:
: Example:
:
:
:
:
: ```
: import_depth: 1
: ```

allow_manual_targets_sync (Boolean)
: Allow syncing targets tagged as manual. By default (`false`), manual-only targets are skipped.
:
:
:
: Example:
:
:
:
:
: ```
: allow_manual_targets_sync: true
: ```

shard_sync (Boolean)
: Allows splitting Bazel sync/build into smaller batches, also referred to as shards, instead of executing a single sync/build. This helps avoid Bazel running out of memory and reduce issues like overly long command lines when syncing large sets of targets. The default is `false` because of a [potential memory leak issue](https://github.com/bazelbuild/bazel/issues/19412) with Bazel.
:
:
:
: Example:
:
:
:
:
: ```
: shard_sync: true
: ```

target_shard_size (Int)
: Specifies the number of targets to be built in a shard. The default is `1000`
:
:
:
: Example:
:
:
:
:
: ```
: target_shard_size: 500
: ```

sharding_approach (String)
: Specifies the strategy used to shard the list of original targets. Used in combination with [shard_sync](#shard_sync). The default is `QUERY_AND_SHARD`.
:
:
:
: The following values are possible:
:
:
:
: * `EXPAND_AND_SHARD`: expand wildcard targets to package targets, query single targets, then shard to batches
:
: * `QUERY_AND_SHARD`: query single targets from the given list of targets without expanding, then shard to batches
:
: * `SHARD_ONLY`: split unexpanded wildcard targets into batches
:
:
:
: Example:
:
:
:
:
: ```
: sharding_approach: EXPAND_AND_SHARD
: ```
:
:
:
: Both `EXPAND_AND_SHARD` and `expand_and_shard` are accepted.

gazelle_target (String)
: Path to the Gazelle target to run automatically at the start of each Bazel sync.
:
:
:
: If specified, IntelliJ IDEA runs the target to update or generates BUILD files for the directories included in the project view before collecting targets and indexing sources.
:
:
:
: Example:
:
:
:
:
: ```
: gazelle_target: //tools/gazelle:gazelle
: ```

index_all_files_in_directories (Boolean)
: This section is deprecated. To index all files inside [directories](#directories), use [index](#index) with the `*` pattern instead.
:
:
:
:
: ```
: index: *
: ```

run_config_run_with_bazel (Boolean)
: Controls whether run configurations execute using Bazel (`bazel run`/`bazel test`) or through [--script_path](https://bazel.build/reference/command-line-reference#run-flag--script_path).
:
:
:
: When enabled, Bazel handles the full execution, which allows you to benefit from Bazel's test result caching and remote build execution (RBE). The trade-off is that Bazel acquires a workspace lock during execution, so you cannot run multiple run configurations in parallel.
:
:
:
: When disabled, IntelliJ IDEA compiles the target through Bazel but launches the resulting binary directly, bypassing the Bazel lock.
:
:
:
: > **Note:**
: > This setting is ignored when debugging: debug sessions never use Bazel execution.
:
:
:
: The default is `true`.
:
:
:
: Example:
:
:
:
:
: ```
: run_config_run_with_bazel: false
: ```

