Skip to content

Fixed bug with cquery not returning error code on failure#28790

Open
rdelfin wants to merge 2 commits intobazelbuild:masterfrom
rdelfin:fix-cquery-nofailure
Open

Fixed bug with cquery not returning error code on failure#28790
rdelfin wants to merge 2 commits intobazelbuild:masterfrom
rdelfin:fix-cquery-nofailure

Conversation

@rdelfin
Copy link
Contributor

@rdelfin rdelfin commented Feb 25, 2026

Description

There is a bug in the cquery command where the command will exit with a 0 exit code even if the command fails at any step along the way (e.g. while fetching a dependency). In this PR, I override the process function to throw a clear starlark eval error.

Motivation

Fixes #21466. This will fix a bug in the cquery command. Currently the only way to detect a failure of a cquery command that uses starlark:expr is by parsing the stdout of the command and search for errors, which can be quite flaky.

Build API Changes

No

Checklist

  • I have added tests for the new use cases (if any).
  • I have updated the documentation (if applicable).

Release Notes

RELNOTES: Fixes a bug in cquery where starlark:expr failures were being ignored for the status code

@github-actions github-actions bot added team-Configurability platforms, toolchains, cquery, select(), config transitions awaiting-review PR is awaiting review from an assigned reviewer labels Feb 25, 2026
@rdelfin
Copy link
Contributor Author

rdelfin commented Feb 25, 2026

I've tested this with the example commands given in the issue both before and after this change:

$ ~/bazel-dev cquery --output=starlark --starlark:expr='target.no_such_attribute' :all
Starting local Bazel server (no_version) and connecting to it...
INFO: Analyzed 6 targets (102 packages loaded, 5550 targets configured).
INFO: Found 6 targets...
ERROR: Starlark evaluation error for //examples/cpp:hello-fail_test: Traceback (most recent call last):
	File "--starlark:expr", line 1, column 35, in format
Error: Accessing providers via the field syntax on structs is deprecated and removed.
ERROR: Starlark evaluation error for //examples/cpp:hello-lib: Traceback (most recent call last):
	File "--starlark:expr", line 1, column 35, in format
Error: Accessing providers via the field syntax on structs is deprecated and removed.
ERROR: Starlark evaluation error for //examples/cpp:hello-success_test: Traceback (most recent call last):
	File "--starlark:expr", line 1, column 35, in format
Error: Accessing providers via the field syntax on structs is deprecated and removed.
ERROR: Starlark evaluation error for //examples/cpp:hello-world: Traceback (most recent call last):
	File "--starlark:expr", line 1, column 35, in format
Error: Accessing providers via the field syntax on structs is deprecated and removed.
ERROR: Starlark evaluation error for //examples/cpp:runfile: Traceback (most recent call last):
	File "--starlark:expr", line 1, column 35, in format
Error: Accessing providers via the field syntax on structs is deprecated and removed.
ERROR: Starlark evaluation error for //examples/cpp:srcs: Traceback (most recent call last):
	File "--starlark:expr", line 1, column 35, in format
Error: Accessing providers via the field syntax on structs is deprecated and removed.
ERROR: Error doing post analysis query: Starlark evaluation failed while formatting cquery results
INFO: Elapsed time: 7.453s, Critical Path: 0.00s
INFO: 0 processes.
ERROR: Build did NOT complete successfully

$ echo $?
1

$ ~/bazel-dev cquery --output=starlark --starlark:expr='fail("Explicit failure")' :all
INFO: Analyzed 6 targets (101 packages loaded, 5550 targets configured).
INFO: Found 6 targets...
ERROR: Starlark evaluation error for //examples/cpp:hello-fail_test: Traceback (most recent call last):
	File "--starlark:expr", line 1, column 33, in format
Error in fail: Explicit failure
ERROR: Starlark evaluation error for //examples/cpp:hello-lib: Traceback (most recent call last):
	File "--starlark:expr", line 1, column 33, in format
Error in fail: Explicit failure
ERROR: Starlark evaluation error for //examples/cpp:hello-success_test: Traceback (most recent call last):
	File "--starlark:expr", line 1, column 33, in format
Error in fail: Explicit failure
ERROR: Starlark evaluation error for //examples/cpp:hello-world: Traceback (most recent call last):
	File "--starlark:expr", line 1, column 33, in format
Error in fail: Explicit failure
ERROR: Starlark evaluation error for //examples/cpp:runfile: Traceback (most recent call last):
	File "--starlark:expr", line 1, column 33, in format
Error in fail: Explicit failure
ERROR: Starlark evaluation error for //examples/cpp:srcs: Traceback (most recent call last):
	File "--starlark:expr", line 1, column 33, in format
Error in fail: Explicit failure
ERROR: Error doing post analysis query: Starlark evaluation failed while formatting cquery results
INFO: Elapsed time: 1.382s, Critical Path: 0.00s
INFO: 0 processes.
ERROR: Build did NOT complete successfully

$ echo $?
1

@rdelfin
Copy link
Contributor Author

rdelfin commented Feb 25, 2026

The previous CI failures seem to be legit, but to fix them I've had to add some logic to make sure we actually process all results. This seems better than just changing the test as making the actual output of failing cquery commands seems like breaking a legitimate expectation. However, let me know if this isn't an appropriate solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting-review PR is awaiting review from an assigned reviewer team-Configurability platforms, toolchains, cquery, select(), config transitions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bazel cquery ignores starlark:expr failures, produces zero-exit status.

1 participant