
Summary
Key points of this document
- Why Rebuild the Build System?
- First, organize artifact types and place them on the same graph.
- Make BUILD definitions use generators as the sole source of truth.
Overview
In a monorepo with an increasing number of services and workspaces, the ability to correctly grasp build reproducibility and the scope of change impact determines development speed and release safety. This article organizes the decisions made to migrate a configuration dependent on pnpm and individual Dockerfiles to Bazel, treating artifacts, tests, and startup verifications within a single dependency graph.
Why Rebuild the Build System?
As the number of service-specific Dockerfiles and individual scripts grows in a monorepo managed by pnpm, the conditions for a successful build become less clear. When local Node.js versions, hoisted dependency packages, and implicitly referenced shared files get mixed, builds might pass locally but fail in CI or within containers. The larger the scale, the longer it takes to isolate the cause.
Another problem is the scope of change impact. If you select rebuild targets solely based on file location, you might miss services that truly need verification when a shared package changes, or conversely, build unrelated services. Unless the build has a complete picture of dependencies, differential verification cannot be accurate.
Therefore, we adopt a hermetic build that treats sources, dependency packages, and toolchains as explicit inputs and does not rely on undeclared environments. Bazel maintains a dependency graph, allowing builds, tests, and container images to be defined on the same graph. The reason for adoption is not the tool's multifunctionality, but its ability to treat reproducibility and impact scope as a structure.
First, organize artifact types and place them on the same graph.
The target services can be organized into three types of artifacts: static frontends, backends or workers, and reverse proxies. Static frontends generate distribution files with Vite and React Router and place them in object storage. Backends and workers are bundled into OCI images, and gateways are treated as independent OCI images.
What's important here is not to confuse container implementation methods with distribution requirements. Delivering screens that don't require SSR as static artifacts, rather than continuously delivering them in containers, can reduce responsibilities and operational paths. On the other hand, gateways that distribute multiple destinations based on hostname or path have reasons to remain as containers.
Instead of having static distribution, OCI images, and tests as separate automations, all are treated as Bazel targets. Even if the execution method differs for each artifact type, being able to trace which inputs lead to which artifacts in a single dependency graph is the premise for differential builds.
- Static Frontend: Generate distribution files and place them in object storage.
- Backend/Worker: Generate bundles and OCI images, and pass them to the execution environment.
- Gateway: Encapsulate the responsibility of distributing destinations into an independent image.
Make BUILD definitions use generators as the sole source of truth.
Manually placing BUILD files in numerous workspaces and having people follow up on dependency changes is not sustainable. We will adopt a method that reads existing workspaces and dependencies from package.json as the primary information source, and generates BUILD definitions, aggregate targets, and test targets for each workspace. The goal is to narrow down what people edit to package.json, which represents the actual state, and the generator.
The generated BUILD definitions are included in the repository. This allows reviewing changes in the dependency graph and referencing definitions even in environments where Bazel is not executed. However, it is crucial not to treat generated artifacts as canonical. CI will check for any differences upon regeneration, and changes that directly modify only BUILD files will be treated as failures.
The generator also inspects dependency directions. By defining allowed directions, such as from application to feature package, and from feature package to shared package, reverse references can be stopped as soon as they appear. Generation is not just about reducing effort, but a mechanism to continuously enforce dependency discipline.
Invisible Dependencies Revealed by Hermeticization
When moving to a hermetic build, paths that previously passed by chance will now appear as failures. This is not so much a Bazel constraint as it is a visualization of implicit dependencies and environmental differences. Rather than loosening the sandbox to avoid failures, it is safer for future maintenance to declare missing inputs one by one.
Typical examples include fixing toolchains, adding internal packages as inputs, and explicit dependencies. Node.js and Bun versions are fixed, including their source and hash, instead of being specified as ranges. Internal sources that Vite and React Router reference relatively, and extends targets shared by multiple tsconfig.json files, are also passed as build inputs without omission.
Especially in configurations relying on pnpm hoisting, undeclared packages can sometimes be imported by chance. This will fail in environments that strictly resolve dependencies package by package, but the fix is simple: explicitly declare the packages used in the package's own package.json. Passing this check improves the accuracy of the dependency graph.
- Node.js and Bun: Fix to exact versions and hashes of acquired artifacts.
- Vite: Explicitly specify the build subcommand to prevent the development server from waiting indefinitely.
- Internal workspace sources and shared configurations: Include reference paths as inputs.
- Bun output destination: Explicitly fix, do not rely on symlink resolution results.
- Undeclared dependencies: Do not allow through hoisting; move back to package.json.
- Execution environment CPU architecture: Determine and treat differences with development machines as cross-builds.
Separate verification of build success and ability to start.
Tests are also targets on the dependency graph. This allows running only tests affected when a shared package is changed. However, even if type checks and tests pass, there's no guarantee that the generated container will start. This is because output placement, startup commands, file owners, and execution users cannot be guaranteed by static build verification alone.
To bridge this gap, we prepare smoke tests that actually start the generated image and verify health checks. Backends are checked with minimal settings that allow them to start even if they cannot reach external services with delayed connections. Gateways are checked to ensure they can distribute to upstream as expected. The goal is not to force the success of external services, but to confirm the container's own startup responsibilities.
Common issues found in this verification include directories becoming one level deeper when placing bundles into images, causing startup commands to fail to find the expected entry. For gateways running as non-root, there might be insufficient write permissions for configurations or temporary files. Since builds can succeed in both cases, it's crucial not to remove startup smoke tests from the pipeline.
- Define unit tests and type checks as Bazel targets.
- Start the image on the target CPU architecture and verify health checks.
- Treat startup commands, artifact placement, execution users, and write permissions as separate verification items.
- On failure, save not only build logs but also container standard output and exit codes.
Treat environmental differences as settings for the same target.
In static frontends, authentication and API connection destinations may be bundled into the artifact at build time. If artifacts built for production are placed directly into a staging environment, users might be directed to production connection destinations. If artifacts need to be created differently for each environment, these differences should be explicitly stated as build settings, not human procedures.
With Bazel, you can use build settings representing environments and select() to switch values for the same target. Environmental transformation rules are aggregated into a single shared module, avoiding duplication with deployment selection scripts. Having duplicate rules can lead to one becoming outdated, causing bugs that only occur in staging environments.
However, environment-specific artifacts complicate cache and identifier handling. The settings used to generate an artifact should be recorded in output metadata to prevent mixing production and staging environment caches. The more unavoidable environmental differences are, the more important it is to be able to trace where those differences lie on the build graph.
Make the switch reversible and leave incomplete tasks.
Being able to create artifacts with Bazel and being able to replace existing deployment paths are different stages. Before switching, you need to prepare the build image used in CI, authentication, remote cache, and startup verification in staging environments. Until preparation is complete, the traditional path should remain default, and the new path should only be executed when explicitly chosen.
The safest order for switching is to verify one gateway or a small static distribution, then one backend, and finally expand to the rest. At each stage, pass builds, target tests, startup smoke tests, and post-delivery health checks, and confirm that you can revert to the traditional path if there are anomalies. Not switching all services at once reduces the scope of failure for the migration itself.
It's also important not to try to make everything green simultaneously. Moving to strict dependency resolution will reveal remaining issues, such as type definition packages that previously relied on hoisting. It's necessary to define which checks are migration gates and which failures are explicitly designated for correction, maintaining clarity about the current status.
Decision Criteria to Take Away from Migration
The value of a hermetic build lies not only in its ability to create artifacts from declared inputs but also in its ability to discover undeclared dependencies. Even if it initially feels stricter than existing builds, by addressing failures in package.json, input definitions, and toolchain fixation, dependencies can be brought closer to reality.
Also, a green build and a green startup are different. Runtime conditions such as artifact placement, startup commands, and permissions are unknown until the container is started. Only by combining tests on the dependency graph with startup smoke tests can pre-delivery verification become a continuous process.
In a large monorepo, the important thing is not merely introducing Bazel. It's about first deciding where to generate BUILD definitions, how to express environmental differences, which verifications to make mandatory, and how far back you can revert upon failure. By designing the migration incrementally and reversibly, you can enhance reproducibility while continuing daily releases without interruption.


