WORKSPACE to Bzlmod, it’s
highly recommended to use the migration script. This helper
tool automates many of the steps involved in migrating your external dependency
management system.
Note: If you want to try out the AI driven Bzlmod migration, check Bzlmod Migration Agent Setup.
Core Functionality
The script’s primary functions are:- Collecting dependency information: Analyzing your project’s
WORKSPACEfile to identify external repositories used by specified build targets, using Bazel’s experimental_repository_resolved_file flag to generate a resolved dependencies file containing this information. - Identifying direct dependencies: Using
bazel queryto determine which repositories are direct dependencies for the specified targets. - Migrating to Bzlmod: Translating relevant
WORKSPACEdependencies into their Bzlmod equivalents. This is a two-step process:- Introduce all identified direct dependencies to the
MODULE.bazelfile. - Build specified targets with Bzlmod enabled, then iteratively identify and fix recognizable errors. This step is needed since some dependencies might be missing in the first step.
- Introduce all identified direct dependencies to the
- Generating a migration report: Creating a
migration_info.mdfile that documents the migration process. This report includes a list of direct dependencies, the generated Bzlmod declarations, and any manual steps that may be required to complete the migration.
- Dependencies available in the Bazel Central Registry
- User-defined custom repository rules
- Package manager dependencies
- Maven
- Go
- Python
- The migration tool is a best-effort utility. Always double-check its recommendations for correctness.
- Use the migration tool with Bazel 7 (not supported with Bazel 8).
How to Use the Migration Tool
Before you begin:- Upgrade to the latest Bazel 7 release, which provides robust support for both WORKSPACE and Bzlmod.
-
Verify the following command runs successfully for your project’s main build
targets:
Command for running the script
Once the prerequisites are met, run the following commands to use the migration tool:Files generated by this script
MODULE.bazel- The central manifest file for Bzlmod, which declares the project’s metadata and its direct dependencies on other Bazel modules.migration_info.md- A file providing step-by-step instructions on how the migration tool was executed, designed to assist in the manual completion of the migration process, if necessary.resolved_deps.py- Contains a comprehensive list of the project’s external dependencies, generated by analyzing the project’sWORKSPACEfile, serving as a reference during the transition.query_direct_deps- Contains migration-relevant information regarding the utilized targets, obtained by invoking Bazel with--output=buildon the project’sWORKSPACEfile. This file is primarily consumed by the migration script.extension_for_XXX- A file containing a module extension definition. The migration tool generates these files for dependencies that are not standard Bazel modules but can be managed using Bzlmod’s module extensions.
Flags
Flags available in this migration scripts are:--t/--target: Targets to migrate. This flag is repeatable, and the targets are accumulated.--i/--initial: DeletesMODULE.bazel,resolved_deps.py,migration_info.mdfiles and starts from scratch - Detect direct dependencies, introduce them in MODULE.bazel and rerun generation of resolved dependencies.
Post-migration cleanup
- Delete
migration_info.md,resolved_deps.pyandquery_direct_deps. - Clean up comments from
MODULE.bazelfile which were used for the migration, such as# -- bazel_dep definitions -- #.
Migration Example
To see the migration script in action, consider the following scenario when Python, Maven and Go dependencies are declared inWORKSPACE file.
Moreover, to demonstrate usage of module extension, custom macro is invoked from
WORKSPACE and it is defined in my_custom_macro.bzl.
The end goal is to have MODULE.bazel file and delete the WORKSPACE file,
without impacting the user experience.
The first step is to follow How to Use the Migration
Tool, which mostly is checking the bazel version
(it must be Bazel 7) and adding an alias to the migration script.
Then, running migrate2bzlmod -t=//... outputs:
- Generates
./resolved_deps.pyfile, which contains info about all external repositories declared and loaded using yourWORKSPACEfile. RESOLVEDkeyword describes all dependencies which are resolved by the tool and added to theMODULE.bazelfile.IMPORTANTkeyword describes significant information worth investing time.- All dependencies have been resolved in this example, at least with
--nobuildflag. - It is important to run the full build (command specified) and manually fix potential errors (e.g. toolchain not registered correctly).
migration_info.mdfile contains details about the migration. Check details at this section.
Transformations
This section illustrates the migration of code from theWORKSPACE file to
MODULE.bazel.
WORKSPACE - Bazel Module
MODULE.bazel - Bazel Module
WORKSPACE - Go Extension
MODULE.bazel - Go Extension
WORKSPACE - Python Extension
MODULE.bazel - Python Extension
WORKSPACE - Maven Extension
MODULE.bazel - Maven Extension
WORKSPACE - Repo rule
MODULE.bazel - Repo rule
WORKSPACE - Module extension
MODULE.bazel - Module extension
extension_for_my_custom_macro.bzl
Tips with debugging
This section provides useful commands and information to help debug issues that may arise during the Bzlmod migration.Useful tips
-
Override version - Not rarely it happens that upgrading the version of a
dependency causes troubles. Bzlmod could change the version of the
dependency due to the MVS algorithm.
In order to use the same or similar version as it was in the WORKSPACE,
override it with
single_version_override.
Note that this is useful for debugging differences between WORKSPACE and
Bzlmod, but you shouldn’t rely on this feature in the long term.
single_version_override(module_name = "{dep_name}", version = "{version}") -
Use bazel mod command.
-
Check the version of a specified repo with
show_repocommand. For example:bazel mod show_repo @rules_python -
Check information about a module extension with the
show_extensioncommand. For example:bazel mod show_extension @rules_python//python/extensions:pip.bzl%pip
-
Check the version of a specified repo with
-
Use vendor mode to create a local copy of a repo when
you want to monitor or control the source of the repo. For example:
bazel vendor --enable_bzlmod --vendor_dir=vendor_src --repo=@protobuf
Migration Report Generation
This file is updated with each run of the migration script or it’s generated from scratch if it’s the first run or if the--i
flag is used. The report contains:
- Command for local testing.
- List of direct dependencies (at least the ones which are directly used in the project).
-
For each dependency, a drop-down menu for checking where the repository was
declared in the
WORKSPACEfile, which is particularly useful for the debugging. You can see it as:
Click here to see where and how the repo was declared in the WORKSPACE file
bazel_dep(name = "rules_python", version = "1.6.1")