rules_julia

Overview

This repository implements Bazel rules for the Julia programming language.

Setup

To begin using the rules, add the following to your MODULE.bazel file.

bazel_dep(name = "rules_julia", version = "{version}")

julia = use_extension("@rules_julia//julia:extensions.bzl", "julia")
julia.toolchain(
    name = "julia_toolchains",
    version = "1.12.2",
)
use_repo(julia, "julia_toolchains")

register_toolchains(
    "@julia_toolchains//:all",
)

Julia Bazel rules

Rules

Aspects

Module Extensions

julia_binary

load("@rules_julia//julia:defs.bzl", "julia_binary")

julia_binary(name, deps, srcs, data, env, main)

A Julia executable.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsOther Julia libraries this target depends onList of labelsoptional[]
srcsJulia source files (.jl files).List of labelsrequired
dataAdditional files needed at runtimeList of labelsoptional[]
envEnvironment variables to set when running the binary. Supports $(location) expansion.Dictionary: String -> Stringoptional{}
mainThe main entrypoint file. If not specified, defaults to the only file in srcs, or a file matching the target name.LabeloptionalNone

julia_format_test

load("@rules_julia//julia:defs.bzl", "julia_format_test")

julia_format_test(name, config, target)

A rule for running JuliaFormatter on a Julia target.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
configThe config file (.JuliaFormatter.toml) containing JuliaFormatter settings.Labeloptional"@rules_julia//julia/settings:formatter_config"
targetThe target to run JuliaFormatter on.Labelrequired

julia_library

load("@rules_julia//julia:defs.bzl", "julia_library")

julia_library(name, deps, srcs, data)

A sharable Julia library or module.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsOther Julia libraries this target depends onList of labelsoptional[]
srcsJulia source files (.jl files)List of labelsrequired
dataAdditional files needed at runtimeList of labelsoptional[]

julia_standalone_binary

load("@rules_julia//julia:defs.bzl", "julia_standalone_binary")

julia_standalone_binary(name, binary)

A rule for converting a julia_binary to a standalone application.

This rule uses PackageCompiler.jl to create a standalone executable that includes the Julia runtime and all dependencies. The resulting application can be distributed and run on machines without Julia installed.

Dependencies are provided hermetically through Bazel - no network access required! PackageCompiler.jl and all Julia package dependencies are managed through the Bazel build system.

Example:

julia_binary(
    name = "my_app_bin",
    srcs = ["my_app.jl"],
    deps = ["//my/lib"],
)

julia_standalone_binary(
    name = "my_app",
    binary = ":my_app_bin",
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
binaryThe julia_binary target to convert into a standalone applicationLabelrequired

julia_test

load("@rules_julia//julia:defs.bzl", "julia_test")

julia_test(name, deps, srcs, data, env, main)

A Julia test executable.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
depsOther Julia libraries this target depends onList of labelsoptional[]
srcsJulia test source files (.jl files).List of labelsrequired
dataAdditional files needed at runtime for the testList of labelsoptional[]
envEnvironment variables to set when running the test. Supports $(location) expansion.Dictionary: String -> Stringoptional{}
mainThe main test entrypoint file. If not specified, defaults to the only file in srcs, or a file matching the target name.LabeloptionalNone

julia_toolchain

load("@rules_julia//julia:defs.bzl", "julia_toolchain")

julia_toolchain(name, julia, version)

A toolchain for building Julia targets.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
juliaThe path to the Julia binary (julia or julia.exe).Labelrequired
versionThe Julia version string (e.g., '1.12.5').Stringrequired

julia_format_aspect

load("@rules_julia//julia:defs.bzl", "julia_format_aspect")

julia_format_aspect()

An aspect for running JuliaFormatter on targets with Julia sources.

ASPECT ATTRIBUTES

ATTRIBUTES

julia

julia = use_extension("@rules_julia//julia:defs.bzl", "julia")

Bzlmod extensions for Julia

Julia settings

Definitions for all @rules_julia//julia settings

formatter_config

--@rules_julia//julia/settings:formatter_config

The JuliaFormatter config file to use in formatting rules.

PARAMETERS

NameDescriptionOptions
name

-

"formatter_config"

version

--@rules_julia//julia/settings:version

The version of julia to use

PARAMETERS

NameDescriptionOptions
name

-

"version"

Julia Pkg rules

Rules

Module Extensions

julia_pkg_compiler

load("@rules_julia//julia/pkg:defs.bzl", "julia_pkg_compiler")

julia_pkg_compiler(name, manifest_bazel_json, manifest_toml, project_toml)

A rule for generating Manifest.toml and Manifest.bazel.json from Project.toml using Julia's Pkg manager.

This rule uses Julia's native package manager to resolve dependencies and generate a Manifest.toml lockfile and a Manifest.bazel.json file with SHA256 hashes. The Manifest.bazel.json can then be used with the pkg module extension to fetch and build Julia packages hermetically with cryptographic verification.

The manifest_bazel_json attribute is optional. If not specified, the Manifest.bazel.json file will be written next to the Manifest.toml file.

Note that when setting this target up for the first time, an empty Manifest.toml file will need to be created. If you specify manifest_bazel_json, that file should also be created as an empty JSON object {}.

Example:

julia_pkg_compiler(
    name = "pkg_update",
    project_toml = "Project.toml",
    manifest_toml = "Manifest.toml",
    # manifest_bazel_json is optional - defaults to Manifest.bazel.json next to Manifest.toml
)

Then run:

bazel run //:pkg_update

This will update Manifest.toml with resolved package versions and Manifest.bazel.json with SHA256 hashes for all packages and their dependency graph.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
manifest_bazel_jsonThe location of the Manifest.bazel.json lockfile to generate/update with SHA256 hashes. If not specified, defaults to Manifest.bazel.json next to the manifest_toml file.LabeloptionalNone
manifest_tomlThe location of the Manifest.toml lockfile to generate/update.Labelrequired
project_tomlThe Project.toml file describing dependencies.Labelrequired

julia_pkg_test

load("@rules_julia//julia/pkg:defs.bzl", "julia_pkg_test")

julia_pkg_test(name, compiler)

A test rule that verifies Manifest.bazel.json is in sync with Manifest.toml.

This test ensures that the lockfile generated by pkg_compiler contains the correct package versions, UUIDs, and dependencies that match the Manifest.toml file.

The test will fail if:

  • A package is missing from the lockfile
  • Versions don't match between the two files
  • UUIDs don't match
  • The git-tree-sha1 is not in the package URL
  • SHA256 hashes are missing

Example:

julia_pkg_compiler(
    name = "pkg_update",
    project_toml = "Project.toml",
    manifest_toml = "Manifest.toml",
    # Normally optional but required for the `julia_pkg_test` target.
    manifest_bazel_json = "Manifest.bazel.json",
)

julia_pkg_test(
    name = "pkg_sync_test",
    compiler = ":pkg_update",
)

Then run:

bazel test //:pkg_sync_test

This will verify that both manifest files are synchronized. If they're out of sync, the test will fail with detailed error messages indicating what needs to be fixed.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
compilerThe julia_pkg_compiler targetLabelrequired

pkg

pkg = use_extension("@rules_julia//julia/pkg:defs.bzl", "pkg")
pkg.install(name, lockfile, manifest)
pkg.pkg_annotation(dep, patch_args, patch_tool, patches)

A module for defining Julia package dependencies.

TAG CLASSES

install

Attributes

NameDescriptionTypeMandatoryDefault
nameThe name of the module to createNamerequired
lockfileThe Manifest.bazel.json lockfile with SHA256 hashes for all packages.Labelrequired
manifestThe Manifest.toml lockfile associated with Project.toml (deprecated, use lockfile instead).LabeloptionalNone

pkg_annotation

Attributes

NameDescriptionTypeMandatoryDefault
depThe name of the package to annotateStringrequired
patch_argsArguments to pass to the patch tool. See http_archive.patch_argsList of stringsoptional[]
patch_toolThe patch tool to use. See http_archive.patch_toolStringoptional""
patchesList of patch files to apply to the package. See http_archive.patchesList of labelsoptional[]