Skip to content

Add make-location lint for deprecated $(location) make variables - #1484

Open
alexeagle wants to merge 2 commits into
bazelbuild:mainfrom
alexeagle:lint/make-location-variable
Open

Add make-location lint for deprecated $(location) make variables#1484
alexeagle wants to merge 2 commits into
bazelbuild:mainfrom
alexeagle:lint/make-location-variable

Conversation

@alexeagle

Copy link
Copy Markdown
Contributor

Summary

  • Add a new buildifier lint category, make-location, that flags deprecated $(location) and $(locations) make variables in BUILD files.
  • Dogfood the rule in this repo by replacing existing uses with $(execpath ...) and enforcing the lint via //:make_location_lint.

Background / deprecation context

Bazel's $(location) and $(locations) make variables are legacy pre-Starlark synonyms for $(execpath) and $(rootpath). Which path they expand to depends on the attribute being expanded, which makes behavior hard to predict and easy to get wrong.

From the Make Variables reference:

location: A synonym for either execpath or rootpath, depending on the attribute being expanded. This is legacy pre-Starlark behavior and not recommended unless you really know what it does for a particular rule. See #2475 for details.

The underlying inconsistency is discussed in bazelbuild/bazel#2475 (e.g. $(location) expanding to an exec path in some attributes but a runfiles path in others). Bazel's docs now steer users toward explicit variables:

This lint nudges BUILD authors toward those explicit forms instead of the ambiguous legacy alias.

Changes

  • New make-location warning in warn/warn_bazel.go (BUILD files only; does not flag load("location", ...)).
  • Tests, WARNINGS.md / warnings.textproto docs, and warning-list updates.
  • Repo fixes: buildifier/BUILD.bazel, buildozer/BUILD.bazel, build/build_defs.bzl.
  • New buildifier_test target //:make_location_lint added to //:tests.

Test plan

  • bazel test //warn:warn_test --test_filter=TestMakeLocationVariable
  • bazel test //warn/docs:docs_test
  • bazel test //buildifier/config:config_test
  • bazel test //:make_location_lint

Flag $(location) and $(locations) in BUILD files in favor of explicit
$(execpath ...) or $(rootpath ...), dogfood the rule in this repo, and
enforce it via a buildifier_test over the workspace.
@alexeagle
alexeagle requested a review from a team as a code owner August 3, 2026 15:47
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@alexeagle
alexeagle requested review from vladmos and removed request for a team August 3, 2026 15:47
sh_test scripts pass args through rlocation, which expects runfiles
paths like the legacy $(location) expansion, not execroot paths.
@alexeagle

Copy link
Copy Markdown
Contributor Author

FYI @Wyverald as you mentioned this team is considering donating to bazel-contrib, I've got more PRs like this to improve the tool...

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant