Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Importing variables in MODULE.bazel files

84 views
Skip to first unread message

Goku Trinidad

unread,
Jan 2, 2025, 10:30:41 AMJan 2
to bazel-discuss
Doing so was trivial with `WORKSPACE` files. You could use `load("//path/to:variables.bzl", "VARIABLE_NAME")`, similar to the approach shown here: https://bazel.build/build/share-variables#sharing_variables_across_multiple_build_files

How can this be done in `MODULE.bazel` files (which do not support `load` statements)? 

I am using Bazel version 7.4.1 if that changes anything.

Thank you.

Alexandre Rostovtsev

unread,
Jan 2, 2025, 1:20:50 PMJan 2
to Goku Trinidad, bazel-discuss, Yun Peng, Xudong Yang

This is not allowed by design; MODULE.bazel is intended to be a static, self-contained config file which is machine-writeable and machine-readable by external tools. Morally, it's the equivalent of a JSON config file, but using Starlark syntax.

For reasoning and background, please see the original design at https://docs.google.com/document/d/1moQfNcEIttsk6vYanNKIy3ZuK53hQUFq1b1r0rmsYVg - in particular, the "Starlark vs JSON/YAML/TOML for the module manifest" section on page 34.

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/bazel-discuss/50e3d220-69d1-41f8-9613-0e2a636ba9c0n%40googlegroups.com.

Yun Peng

unread,
Jan 2, 2025, 1:21:03 PMJan 2
to Alexandre Rostovtsev, Goku Trinidad, bazel-discuss, Xudong Yang
Yes, essentially WORKSPACE is imperative but MODULE.bazel is declarative, we don't allow "load" statements in MODULE.bazel anymore.

What is your exact use case? We can probably find a solution without using "load", probably via a module extension.

Tomasz Jurkiewicz

unread,
Mar 17, 2025, 11:32:39 AMMar 17
to bazel-discuss
I have probably the same issue. I have a collection of constants I need to access in both MODLE.bzl and in the BUILD.bazel files.

So far my workaround was to put one copy in a //constants.bzl file available for bazel rules. Another copy on top of MODULE.bazel, and guarantee the consistency with a smoke test.

I know MODULE allows a limited load statement now, but it explicitly excludes all variables.
Reply all
Reply to author
Forward
0 new messages