bazel syntax: move Environment.callerLabel to BazelStarlarkContext.analysisRuleLabel

BazelStarlarkContext is the application state carried by each Starlark thread
created by Bazel/Blaze, and is thus the logical place to hold this information.

The callerLabel field exists only to support the legacy behavior of
Label(relative_to_caller_repository=True), which causes a label string to be
parsed relative to the repository of the rule being analyzed; the default
behavior is to use repository of the file containing the call to Label.

Aside: the deprecated semantics are actually cleaner, since they rely only on
state---analysisRuleLabel---in the Starlark thread. By contrast, the supported
semantics are more complex and subtle because they rely on dynamic scope:
in effect, they introspect on the call stack and use the Label associated with
the topmost file. Dynamic scope has been recognized as a mistake for over 50 years.

The analysisRuleLabel field is set only for analysis-phase (rule and aspect
implementation) Starlark threads, and is the label of the rule instance.
(Previously, setCallerLabel was done only for rule implementation threads,
not aspect threads, but I suspect this was an oversight, so I made them
consistent.)

The motivation for this change is to eliminate Environment.getCallerLabel,
thus breaking a dependency from Environment to Label.

Also: remove a use of getCallerLabel in the android rules. I made it use
ruleContext.getLabel, which is consistent with other places in the android
rules. I suspect that someone grabbed this function by mistake while blindly
groping for anything that would give them a Label.

Also: remove obfuscating "convenience" constructor of BazelStarlarkContext.
It's much clearer to have one constructor with lots of /*foo=*/null arguments
at each call than to have two or more constructors that are hard to tell apart
and impossible to remember.

Also: document some of the problems with the Label function.

Once unknown commit is also submitted, we can delete Environment.callerLabel.

PiperOrigin-RevId: 270722058
13 files changed
tree: b982bbe185f9aff31511c71c96712c09ee3d866d
  1. .bazelci/
  2. examples/
  3. scripts/
  4. site/
  5. src/
  6. third_party/
  7. tools/
  8. .bazelrc
  9. .gitattributes
  10. .gitignore
  11. AUTHORS
  12. BUILD
  13. CHANGELOG.md
  14. CODEOWNERS
  15. combine_distfiles.py
  16. combine_distfiles_to_tar.sh
  17. compile.sh
  18. CONTRIBUTING.md
  19. CONTRIBUTORS
  20. distdir.bzl
  21. ISSUE_TEMPLATE.md
  22. LICENSE
  23. README.md
  24. WORKSPACE
README.md

Bazel

{Fast, Correct} - Choose two

Build and test software of any size, quickly and reliably.

  • Speed up your builds and tests: Bazel only rebuilds what is necessary. With advanced local and distributed caching, optimized dependency analysis and parallel execution, you get fast and incremental builds.

  • One tool, multiple languages: Build and test Java, C++, Android, iOS, Go, and a wide variety of other language platforms. Bazel runs on Windows, macOS, and Linux.

  • Scalable: Bazel helps you scale your organization, codebase, and continuous integration solution. It handles codebases of any size, in multiple repositories or a huge monorepo.

  • Extensible to your needs: Easily add support for new languages and platforms with Bazel's familiar extension language. Share and re-use language rules written by the growing Bazel community.

Getting Started

Documentation

Contributing to Bazel

See CONTRIBUTING.md

Build status

Bazel is released in ‘Beta’. See the product roadmap to learn about the path toward a stable 1.0 release.