How to programmatically figure out the current bazel system?

40 views
Skip to first unread message

Dorian Haglund

unread,
Jun 12, 2025, 4:59:08 AMJun 12
to bazel-discuss

Hello,

I'm writing a script which adds a dependency to an arbitrary bazel project. The script should edit the MODULE.bazel or WORKSPACE file, depending on the bazel system in use. It tries to figure out which system is used by probing the filesystem, looking for WORKSPACE, MODULE.bazel or WORKSPACE.bzlmod.

However, this heuristic is brittle, and projects (such as cppitertools) may have an empty WORKSPACE file.

Is there a way to reliably get the system in use?

Best regards,

Dorian

Alexandre Rostovtsev

unread,
Jun 12, 2025, 1:32:15 PMJun 12
to Dorian Haglund, bazel-discuss
In the most general case, it is impossible. That's because after your tool is finished doing its thing, the user might decide to launch `bazel --enable_workspace --noenable_bzlmod build //whatever`, or they could instead launch `bazel --enable_bzlmod --noenable_workspace build //whatever` - and you have no idea which alternative the user will decide to pick, and whether or not the user is expecting for both alternatives to work, or cares only about one (and which one).

However, as a heuristic that will work in most cases, you should prefer to edit MODULE.bazel, and fall back to editing WORKSPACE only if there is no pre-existing MODULE.bazel file. That is because the bazel ecosystem is moving towards modules, and in bazel 8.0 and higher, support for WORKSPACE files is disabled by default.
 

--
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/2776bd44-8e7e-4e53-9b00-12dea05d9534n%40googlegroups.com.

Alexandre Rostovtsev

unread,
Jun 12, 2025, 1:34:36 PMJun 12
to Dorian Haglund, bazel-discuss
(Sorry, typo, should be `bazel build --enable_workspace --noenable_bzlmod //whatever` and `bazel build --enable_bzlmod --noenable_workspace //whatever`)

Dorian Haglund

unread,
Jun 13, 2025, 5:06:36 AMJun 13
to Alexandre Rostovtsev, bazel-discuss
Thank you for your quick response, I'll adapt my script accordingly. Note that I posted the same question on stack overflow a few days ago. In case you wish to answer.
--
Dorian Haglund
Reply all
Reply to author
Forward
0 new messages