Repository rule errors are printed three times
- Dominant language
- Java
- Stars
- 25.8k
- Forks
- 4.6k
- Avg merge
- 2d 20h
- Merged PRs (30d)
- 72
Description
### Description of the bug:
When a repository rule implementation wants to report a failure, the only option right now is to use `fail()`, but that will print the message three times in the output. Here is an example with a syntax error, but fail() invocations behave the same:
$ bazel build @terse//:'*'
INFO: Repository terse instantiated at:
/home/layus/projects/bazel-bug/repeated_error/WORKSPACE:3:10: in
Repository rule repo_fail defined at:
/home/layus/projects/bazel-bug/repeated_error/main.bzl:23:28: in
ERROR: An error occurred during the fetch of repository 'terse':
Traceback (most recent call last):
File "/home/layus/projects/bazel-bug/repeated_error/main.bzl", line 3, column 29, in _repo_fail
verbose = repository_ctx.attrs.verbose
Error: 'repository_ctx' value has no field or method 'attrs' (did you mean 'attr'?)
ERROR: /home/layus/projects/bazel-bug/repeated_error/WORKSPACE:3:10: fetching repo_fail rule //external:terse: Traceback (most recent call last):
File "/home/layus/projects/bazel-bug/repeated_error/main.bzl", line 3, column 29, in _repo_fail
verbose = repository_ctx.attrs.verbose
Error: 'repository_ctx' value has no field or method 'attrs' (did you mean 'attr'?)
ERROR: 'repository_ctx' value has no field or method 'attrs' (did you mean 'attr'?)
INFO: Elapsed time: 0.073s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded)
The problem is that this error message is the only place to provide information about the failure. We often see messages spanning 10s or 100s of lines there from rules_nixpkgs, as the error contains the output of the underlying command. Printing it three times (four including quiet = False) scrambles the log, and is generally unhelpful.
Not sure about the proper fix, but maybe an 'error()' builtin, related to https://github.com/bazelbuild/bazel/issues/4772 ?
Or wrapping https://github.com/bazelbuild/bazel/blob/7.0.0-pre.20230123.5/src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkRepositoryFunction.java#L267-L273 with an `AlreadyReportedRepositoryAccessException` ?, leaving only the last one to get rid of ?
My impression here is that the two backtraces are about the same, and one should go. As for the last error, it should only contain a single line summary, not the full message again.
See https://gist.github.com/layus/005db27d799612bc7cbc0ccbf4cf554d#file-output-md for more insights
### What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.
See https://gist.github.com/layus/005db27d799612bc7cbc0ccbf4cf554d#file-output-md for a detailed MWE.
### Which operating system are you running Bazel on?
Ubuntu
### What is the output of `bazel info release`?
7.0.0-pre.20230123.5
### If `bazel info release` returns `development version` or `(@non-git)`, tell us how you built Bazel.
built with nix
### What's the output of `git remote get-url origin; git rev-parse master; git rev-parse HEAD` ?
_No response_
### Have you found anything relevant by searching the web?
_No response_
### Any other information, logs, or outputs that you want to share?
https://gist.github.com/layus/005db27d799612bc7cbc0ccbf4cf554d#file-output-md
Also, a library-side improvement for readability https://github.com/tweag/rules_nixpkgs/pull/389
Contributor guide
Research direction
Start in src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkRepositoryFunction.java around the cited repository error handling. Reproduce the minimal example from the linked gist and inspect how the fetch error and final summary are emitted. Done means repository rule failures no longer repeat the full message and retain a concise final summary.
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
- Mostly clear
- Newbie friendliness
- 35/100