diff --git a/BUILD.bazel b/BUILD.bazel index bc2fc5a8a..e58944abc 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,9 +1,10 @@ load("@bazel_gazelle//:def.bzl", "gazelle") -load("//buildifier:def.bzl", "buildifier") +load("//buildifier:def.bzl", "buildifier", "buildifier_test") exports_files([ "LICENSE", "launcher.js", + "WORKSPACE", ]) config_setting( @@ -20,6 +21,7 @@ filegroup( test_suite( name = "tests", tests = [ + ":make_location_lint", "//api_proto:api.gen.pb.go_checkshtest", "//build:build_test", "//build_proto:build.gen.pb.go_checkshtest", @@ -45,3 +47,11 @@ gazelle( buildifier( name = "buildifier", ) + +buildifier_test( + name = "make_location_lint", + lint_mode = "warn", + lint_warnings = ["make-location"], + no_sandbox = True, + workspace = "//:WORKSPACE", +) diff --git a/WARNINGS.md b/WARNINGS.md index 2514b8813..70b4caf81 100644 --- a/WARNINGS.md +++ b/WARNINGS.md @@ -38,6 +38,7 @@ Warning categories supported by buildifier's linter: * [`list-append`](#list-append) * [`load`](#load) * [`load-on-top`](#load-on-top) + * [`make-location`](#make-location) * [`module-docstring`](#module-docstring) * [`name-conventions`](#name-conventions) * [`native-android`](#native-android) @@ -748,6 +749,36 @@ they can follow only comments and docstrings. -------------------------------------------------------------------------------- +## The `$(location)` make variable is deprecated + + * Category name: `make-location` + * Automatic fix: no + * [Suppress the warning](#suppress): `# buildifier: disable=make-location` + +The `$(location)` and `$(locations)` make variables are legacy synonyms for +`$(execpath)` and `$(rootpath)` whose behavior depends on the attribute being +expanded. Use `$(execpath ...)` when you need the execution path, or +`$(rootpath ...)` when you need the runfiles path. + +Examples that trigger this warning: + +```python +genrule( + name = "example", + srcs = [":input"], + outs = ["output"], + cmd = "cp $(location :input) $@", +) +``` + +Instead, use an explicit make variable: + +```python +cmd = "cp $(execpath :input) $@", +``` + +-------------------------------------------------------------------------------- + ## The file has no module docstring * Category name: `module-docstring` diff --git a/build/build_defs.bzl b/build/build_defs.bzl index 2a2c48930..900f8b828 100644 --- a/build/build_defs.bzl +++ b/build/build_defs.bzl @@ -144,7 +144,7 @@ eof srcs = [src + "_check.sh"], deps = ["@bazel_tools//tools/bash/runfiles"], data = [src, gen], - args = ["$(location " + src + ")", "$(location " + gen + ")"], + args = ["$(rootpath " + src + ")", "$(rootpath " + gen + ")"], ) # magic copy rule used to update the checked-in version @@ -152,7 +152,7 @@ eof name = src + "_copysh", srcs = [gen], outs = [src + "copy.sh"], - cmd = "echo 'cp $${BUILD_WORKSPACE_DIRECTORY}/$(location " + gen + + cmd = "echo 'cp $${BUILD_WORKSPACE_DIRECTORY}/$(execpath " + gen + ") $${BUILD_WORKSPACE_DIRECTORY}/" + native.package_name() + "/" + src + "' > $@", ) sh_binary( diff --git a/buildifier/BUILD.bazel b/buildifier/BUILD.bazel index 5bf6338e9..634a0407a 100644 --- a/buildifier/BUILD.bazel +++ b/buildifier/BUILD.bazel @@ -93,7 +93,7 @@ sh_test( size = "small", srcs = ["integration_test.sh"], args = [ - "$(location :buildifier)", + "$(rootpath :buildifier)", ], data = [ ":buildifier", diff --git a/buildifier/config/config_test.go b/buildifier/config/config_test.go index c9291769e..441cfad01 100644 --- a/buildifier/config/config_test.go +++ b/buildifier/config/config_test.go @@ -77,6 +77,7 @@ func ExampleExample() { // "keyword-positional-params", // "list-append", // "load", + // "make-location", // "module-docstring", // "name-conventions", // "native-android", @@ -298,6 +299,7 @@ func TestValidate(t *testing.T) { "keyword-positional-params", "list-append", "load", + "make-location", "module-docstring", "name-conventions", "native-android", @@ -400,6 +402,7 @@ func TestValidate(t *testing.T) { "keyword-positional-params", "list-append", "load", + "make-location", "module-docstring", "name-conventions", "native-android", @@ -502,6 +505,7 @@ func TestValidate(t *testing.T) { "keyword-positional-params", "list-append", "load", + "make-location", "module-docstring", "name-conventions", "native-android", @@ -604,6 +608,7 @@ func TestValidate(t *testing.T) { "keyword-positional-params", "list-append", "load", + "make-location", "module-docstring", "name-conventions", "native-android", diff --git a/buildifier/integration_test.sh b/buildifier/integration_test.sh index b9a6389c5..9662fdbe1 100755 --- a/buildifier/integration_test.sh +++ b/buildifier/integration_test.sh @@ -292,6 +292,7 @@ cat > golden/.buildifier.example.json <