Repository navigation
Description
Please provide the following information. The more we know about your system and use case, the more easily and likely we can help.
Description of the problem / feature request / question:
- Define a macro in prelude_bazel:
$ bazel info
ERROR: tools/build_rules/prelude_bazel:1:1: syntax error at 'def': This is not supported in BUILD files. Move the block to a .bzl file and load it
It complains that def is not support in BUILD files, but prelude_bazel is not a BUILD file.
- Move the macro definition to //tools/build_rules:rules.bzl and load it in prelude_bazel:
$ bazel info
ERROR: error loading package '@bazel_tools//tools/jdk': Extension file not found. Unable to load file '@bazel_tools//tools/jdk:rules.bzl': file doesn't exist or isn't a file
It seems that prelude_bazel loads the bzl file relative whatever (potentially external) package is processing the prelude instead of //tools/build_rules.
It is also illegal to bind() bzl files to //external.
The only solution is to place the bzl file containing the macros in a nested workspace, add it to the WORKSPACE file as a local_repository(), then load it in prelude_bazel through the nested external
workspace.
This is extremely convoluted to just have macros in the prelude.
If possible, provide a minimal example to reproduce the problem:
Environment info
-
Operating System: Debian
-
Bazel version (output of
bazel info release): release 0.6.0 -
If
bazel info releasereturns "development version" or "(@Non-Git)", please tell us what source tree you compiled Bazel from; git commit hash is appreciated (git rev-parse HEAD):
Have you found anything relevant by searching the web?
(e.g. StackOverflow answers,
GitHub issues,
email threads on the bazel-discuss Google group)
Anything else, information or logs or outputs that would be helpful?
(If they are large, please upload as attachment or provide link).
Activity
laurentlb commentedon Sep 29, 2017
Prelude file is inserted at the top of BUILD files. It should have the same restrictions as BUILD files.
Maybe you should do with the prelude file?
chaoren commentedon Sep 29, 2017
In a regular BUILD file, I would put my macros in a bzl file next to the BUILD file, then load them. Pretty simple. Can't do that in prelude now. There's no way to load a bzl file except from an external workspace. Because the prelude will run for things like
@bazel_tools//tools/jdk.Perhaps prelude shouldn't be inserted in BUILD files from other workspaces? Then loading a local bzl file with an absolute label would work.
seanmcl commentedon Oct 10, 2017
This is particularly hard on CentOS users, since we install through your yum repo and 0.5.2 is no longer published there.
varming commentedon Oct 17, 2017
Any news on this issue?
laurentlb commentedon Nov 21, 2018
No, the behavior change is intended.
More generally, I encourage you to avoid using the prelude file. It was a temporary solution, it's not clear whether we want to support it in the long-term.
chaoren commentedon Nov 21, 2018
So there will be no way to redefine a rule/macro without having to update every single BUILD file with load statements?
ittaiz commentedon Nov 21, 2018
laurentlb commentedon Nov 21, 2018
I didn't want to scare you; there's no decision.
To summarize the current state:
But since some people use it, we can keep it in Bazel for now (it's not difficult to keep it working).
In the future, we may provide better tooling for maintaining load statements (insert / remove them automatically).
ittaiz commentedon Nov 22, 2018
mboes commentedon Nov 22, 2018
A working
prelude_bazelwould be really useful. Right now the native rules have a priviledged status, because there's no need to write verboseload()lines for them in everyBUILDin a project (there can be many). In a project that uses only Starlark rules, it can get tiring to write theload()lines especially for new users, who are not Bazel experts and just want to write simple build descriptions as quickly as possible. Bazel naming conventions mean repository names are long by necessity, compounding the verbosity ofload()lines.@laurentlb it sounds to me like the limitations you mention are all incidental, and could conceivably be fixed. Say if the prelude file was restricted to only containing load statements.
petroseskinder commentedon Nov 22, 2018
+1
Similar to @mboes, our primary use case is for automatically loading custom Stylark rules, (rather than have users explicitly load the rules in each
BUILDfile). This issue is felt most acutely with native rules that we cannot configure as easily, i.e.java_library,java_binary,java_test. To circumvent these limitations, we have created macros with the desired javacopts/jvm_flags set by default, and users load the associated rules.If there is another mechanism we could use to preload non-native rules that would be nice.
ittaiz commentedon Nov 23, 2018
laurentlb commentedon Apr 16, 2019
Thanks for the feedback.
This is a good idea. I'd like to make this change: restrict the prelude to contain only
loadstatements. I expect this won't make a big difference for the users, and it will help us in the future.laurentlb commentedon Aug 12, 2020
With 2a73a73, the prelude is handled in a saner way (cc @brandjon):
brandjon commentedon Feb 16, 2021
This bug appears to no longer repro now that we're using the new prelude semantics.
It is still recommended to use the prelude sparingly if at all. Injecting custom symbols into BUILD files on a per-repo basis can make builds less readable.