This document explains the JSON-based integration between Bazel and MoonBit that maintains hermeticity while enabling rich communication between the two build systems.
The integration uses JSON as an intermediate format to:
- Preserve Hermeticity: All build information is explicitly declared in JSON files
- Enable Rich Communication: Complex build configurations can be passed between systems
- Maintain Dependency Tracking: Full dependency graphs are preserved in JSON format
- Support Multiple Platforms: Platform-specific configurations are handled via JSON
Bazel → [JSON Config] → MoonBit Compiler → [JSON Metadata] → Bazel
-
JSON Configuration Files (Bazel → MoonBit):
*.moon.build.json: Main build configuration*.moon.hermetic.json: Hermetic build settings*.moon.deps.json: Dependency manifest
-
JSON Metadata Files (MoonBit → Bazel):
*.moon.metadata.json: Build output metadata*.moon.test.report.json: Test results and coverage
{
"bazel": {
"label": "//package:target",
"workspace_name": "workspace",
"package_name": "package",
"target_name": "target",
"is_main": false,
"is_test": false,
"platform": "darwin_arm64",
"configuration": "release"
},
"sources": ["source1.mbt", "source2.mbt"],
"dependencies": [
{
"label": "//other:dep",
"package": "other",
"name": "dep",
"metadata": {}
}
],
"options": {
"target": "wasm",
"optimization": "release",
"debug_info": false,
"output_format": "json"
}
}{
"hermetic_build": {
"bazel_version": "7.0.0",
"moonbit_version": "0.6.33",
"platform": {
"os": "darwin",
"arch": "arm64",
"cpu": "apple_arm64"
},
"toolchain": {
"type": "hermetic",
"source": "bazel_vendor",
"checksum": "verified"
},
"target": "wasm",
"output": "output.wasm",
"timestamp": "2026-01-11T00:00:00Z",
"build_id": "target_hash"
}
}{
"dependencies": [
{
"label": "//other:dep",
"package": "other",
"name": "dep"
}
],
"transitive_dependencies": [
{
"label": "//base:lib",
"package": "base",
"name": "lib"
}
],
"dependency_graph": {
"//other:dep": {
"direct": true,
"transitive": ["//base:lib"]
}
}
}{
"moonbit": {
"version": "0.6.33",
"target": "wasm",
"optimization_level": "release"
},
"dependencies": ["//other:dep"],
"outputs": ["output.wasm"],
"warnings": [],
"errors": [],
"timing": {
"compile_time_ms": 100,
"optimization_time_ms": 50,
"total_time_ms": 150
}
}The JSON integration maintains hermeticity through:
- Explicit Toolchain Declaration: All tools are specified in JSON configs
- Checksum Verification: Toolchain checksums are included in hermetic configs
- Complete Dependency Tracking: Full dependency graphs are preserved
- Platform Specification: Target platforms are explicitly declared
- Environment Isolation: Build environment is fully specified in JSON
-
Bazel Analysis Phase:
- Bazel analyzes the build graph
- Generates JSON configuration files for each MoonBit target
- Declares all inputs and outputs explicitly
-
MoonBit Compilation Phase:
- MoonBit reads JSON configuration files
- Performs compilation with specified settings
- Generates JSON metadata about the build
-
Bazel Execution Phase:
- Bazel reads MoonBit's JSON metadata
- Updates dependency graph with build information
- Provides outputs to dependent targets
# In your BUILD.bazel file
moonbit_library(
name = "my_lib",
srcs = ["lib.mbt"],
deps = [":other_lib"],
)
moonbit_binary(
name = "my_bin",
srcs = ["main.mbt"],
deps = [":my_lib"],
)This will generate:
my_lib.moon.build.json- Build configurationmy_lib.moon.hermetic.json- Hermetic settingsmy_lib.moon.deps.json- Dependency manifestmy_lib.moon.metadata.json- Build metadata
- Hermetic Builds: All build information is explicitly declared
- Cross-Platform Support: Platform configurations are handled via JSON
- Rich Metadata: Detailed build information is preserved
- Debugging Support: Full build logs and timing information
- Toolchain Flexibility: Multiple toolchain versions can be supported
The integration is implemented in:
moonbit/private/json_utils.bzl- JSON generation and parsingmoonbit/private/compilation.bzl- Compilation actions with JSONmoonbit/private/toolchain.bzl- Toolchain management with JSON metadata
The enhanced checksum registry provides comprehensive JSON-based checksum management:
{
"tool_name": "moonbit",
"github_repo": "moonbitlang/moonbit",
"latest_version": "0.6.33",
"last_checked": "2026-01-01T00:00:00Z",
"build_type": "binary",
"versions": {
"0.6.33": {
"release_date": "2026-01-01",
"platforms": {
"darwin_arm64": {
"sha256": "89d87662194a4a2d7cc345b1fecf8bbe42e0a00ee7e68c8f2e407f3f3da4b51f",
"url_suffix": "moonbit-darwin-aarch64.tar.gz",
"binaries": ["moon"],
"archive_type": "tar.gz"
},
"linux_amd64": {
"sha256": "401d0c5408819a0ed6197d2db54c10e17fe16c6e5654df1bf8b2de71a9bbc1e5",
"url_suffix": "moonbit-linux-x86_64.tar.gz",
"binaries": ["moon"],
"archive_type": "tar.gz"
}
}
}
},
"supported_platforms": [
"darwin_amd64", "darwin_arm64",
"linux_amd64", "linux_arm64",
"windows_amd64"
]
}- Comprehensive Metadata: Includes tool name, GitHub repository, versions, platforms, and more
- Version History: Complete version history with release dates
- Platform Support: Explicit platform definitions with binaries and archive types
- Backward Compatibility: Automatic conversion from legacy format
- Validation: Built-in checksum validation and auto-fix capabilities
The checksum registry integrates with Bazel's hermetic toolchain system:
# In vendor_toolchains.bzl
load("//moonbit/checksums:registry_v2.bzl",
"get_moonbit_checksum_v2",
"get_moonbit_info_v2",
"get_latest_moonbit_version_v2")
# Use enhanced functions with fallback to legacy
def _vendor_moonbit_toolchain_impl(repository_ctx):
latest_version = get_latest_moonbit_version_v2(repository_ctx)
tool_info = get_moonbit_info_v2(repository_ctx, latest_version, platform)
checksum = get_moonbit_checksum_v2(repository_ctx, latest_version, platform)
# ... rest of implementation- Copy Files: Copy
registry_v2.bzlandmoonbit_v2.jsonto your project - Update BUILD.bazel: Export both legacy and new files
- Update Imports: Change imports to use enhanced registry
- Test: Verify both formats work correctly
- Gradual Adoption: Enable new features when ready
See the example in examples/json_integration/ for a complete working example.