Duplicate results/output with cquery and `--output starlark` after build
- Dominant language
- Java
- Stars
- 25.8k
- Forks
- 4.6k
- Avg merge
- 2d 20h
- Merged PRs (30d)
- 72
Description
### Description of the bug:
The following cquery has two targets:
```
bazel cquery --output build "@bazel_tools//tools/jdk:current_java_toolchain + @bazel_tools//tools/jdk:current_java_runtime"
# /../external/bazel_tools/tools/jdk/BUILD:186:10
alias(
name = "current_java_toolchain",
actual = "@rules_java//toolchains:current_java_toolchain",
)
# Rule current_java_toolchain instantiated at (most recent call last):
# /../external/bazel_tools/tools/jdk/BUILD:186:10 in
# /../external/bazel_tools/tools/jdk/BUILD:186:10
alias(
name = "current_java_runtime",
actual = "@rules_java//toolchains:current_java_runtime",
)
# Rule current_java_runtime instantiated at (most recent call last):
# /../external/bazel_tools/tools/jdk/BUILD:186:10 in
```
However, when changing the output format to `--output starlark` and applying a formatter the formatter gets called multiple times for the same target.
```
bazel cquery --output starlark --starlark:file .../toolchains-query-format.scl "@bazel_tools//tools/jdk:current_java_toolchain + @bazel_tools//tools/jdk:current_java_runtime" 2>/dev/null
# toolchain_infos for
java_toolchain_info_source_version=17
java_toolchain_info_target_version=17
java_toolchain_info_java_home=external/rules_java++toolchains+remotejdk21_macos_aarch64
# toolchain_infos for
java_toolchain_info_source_version=17
java_toolchain_info_target_version=17
java_toolchain_info_java_home=external/rules_java++toolchains+remotejdk21_macos_aarch64
# toolchain_infos for
java_toolchain_info_source_version=11
java_toolchain_info_target_version=11
java_toolchain_info_java_home=external/rules_java++toolchains+remotejdk21_macos_aarch64
# runtime_infos for
java_runtime_info_java_home=external/rules_java++toolchains+local_jdk
# runtime_infos for
java_runtime_info_java_home=external/rules_java++toolchains+remotejdk11_macos_aarch64
```
The formatter is:
```
def format(target):
toolchain_infos = {k: v for k, v in providers(target).items() if k.endswith('JavaToolchainInfo')}
runtime_infos = {k: v for k, v in providers(target).items() if k.endswith('JavaRuntimeInfo')}
if len(toolchain_infos) == 1:
java_toolchain_info = toolchain_infos.values()[0]
return '# toolchain_infos for ' + str(target) + '\n' + 'java_toolchain_info_source_version=' + java_toolchain_info.source_version + '\njava_toolchain_info_target_version=' + java_toolchain_info.target_version + '\njava_toolchain_info_java_home=' + java_toolchain_info.java_runtime.java_home
if len(runtime_infos) == 1:
java_runtime_info = runtime_infos.values()[0]
return '# runtime_infos for ' + str(target) + '\n' + 'java_runtime_info_java_home=' + java_runtime_info.java_home
fail("Unable to obtain JavaToolchainInfo or JavaRuntimeInfo.")
```
Note, it seems to be correct after a `bazel shutdown` but as soon as I build something the output of the above `cquery` changes to the one with multiple targets above.
### Which category does this issue belong to?
Core
### What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.
```
git clone https://github.com/eclipseguru/bazel-jdt-java-toolchain.git
cd bazel-jdt-java-toolchain
test-bazel/toolchains-query.sh
```
Note, the scripts implements shutdown, then does cquery, then a build and then cquery again. The first cquery is correct, the second has the incorrect duplication in its output. Thus, the build is changing something within the Bazel server process, which impacts the output of cquery.
### Which operating system are you running Bazel on?
MacOS
### What is the output of `bazel info release`?
release 8.3.1
### If `bazel info release` returns `development version` or `(@non-git)`, tell us how you built Bazel.
_No response_
### What's the output of `git remote get-url origin; git rev-parse HEAD` ?
```text
```
### If this is a regression, please try to identify the Bazel commit where the bug was introduced with bazelisk --bisect.
_No response_
### Have you found anything relevant by searching the web?
_No response_
### Any other information, logs, or outputs that you want to share?
_No response_
Contributor guide
Research direction
Start with the reproducer at test-bazel/toolchains-query.sh and run its shutdown, cquery, build, and cquery sequence. Compare the first and second --output starlark results, focusing on why the build changes formatter invocation or target output within the Bazel server. Done means the post-build cquery produces one formatted result per target, matching the pre-build behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- build-system
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 38/100