Skip to content

Macros no longer allowed in prelude_bazel? #3835

Closed

Description

@chaoren

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:

  1. 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.

  1. 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 release returns "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

  1. added
    P2
    on Sep 29, 2017
  2. laurentlb commented on Sep 29, 2017

    @laurentlb
    Contributor

    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?

  3. chaoren commented on Sep 29, 2017

    @chaoren
    ContributorAuthor

    Prelude file is inserted at the top of BUILD files. It should have the same restrictions as BUILD files.

    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.

  4. seanmcl commented on Oct 10, 2017

    @seanmcl

    This is particularly hard on CentOS users, since we install through your yum repo and 0.5.2 is no longer published there.

  5. varming commented on Oct 17, 2017

    @varming

    Any news on this issue?

  6. laurentlb commented on Nov 21, 2018

    @laurentlb
    Contributor

    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.

  7. chaoren commented on Nov 21, 2018

    @chaoren
    ContributorAuthor

    So there will be no way to redefine a rule/macro without having to update every single BUILD file with load statements?

  8. ittaiz commented on Nov 21, 2018

    @ittaiz
    Member
  9. laurentlb commented on Nov 21, 2018

    @laurentlb
    Contributor

    I didn't want to scare you; there's no decision.

    To summarize the current state:

    • The prelude file is not documented (I don't think there's any reference on the website).
    • It's not well tested or designed. For example, I don't even know if it works with external repositories.
    • It has performance problems: when you modify the prelude (or any of its dependencies), every BUILD file has to be reloaded. For this reason, we stop using it at Google.

    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).

  10. ittaiz commented on Nov 22, 2018

    @ittaiz
    Member
  11. mboes commented on Nov 22, 2018

    @mboes
    Contributor

    A working prelude_bazel would be really useful. Right now the native rules have a priviledged status, because there's no need to write verbose load() lines for them in every BUILD in a project (there can be many). In a project that uses only Starlark rules, it can get tiring to write the load() 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 of load() 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.

  12. petroseskinder commented on Nov 22, 2018

    @petroseskinder
    Member

    +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 BUILD file). 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.

  13. ittaiz commented on Nov 23, 2018

    @ittaiz
    Member
  14. added
    P3
    and removed
    P2
    on Apr 5, 2019
  15. laurentlb commented on Apr 16, 2019

    @laurentlb
    Contributor

    Thanks for the feedback.

    Say if the prelude file was restricted to only containing load statements.

    This is a good idea. I'd like to make this change: restrict the prelude to contain only load statements. I expect this won't make a big difference for the users, and it will help us in the future.

  16. laurentlb commented on Aug 12, 2020

    @laurentlb
    Contributor

    With 2a73a73, the prelude is handled in a saner way (cc @brandjon):

    • the prelude is evaluated once and then cached (instead of being append in each BUILD file)
    • you can no longer call macros/rules from the prelude (but no one was doing that, right?), just load them
    • error messages are better, with a proper stack trace
  17. brandjon commented on Feb 16, 2021

    @brandjon
    Member

    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.

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

Metadata

Metadata

Assignees

Labels

P3type: feature request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions