Repository Rules

September 10, 2026 ยท View on GitHub

The rules described below are used to build Swift packages and make their products and targets available as Bazel targets.

On this page:

local_swift_package

load("@rules_swift_package_manager//swiftpkg:defs.bzl", "local_swift_package")

local_swift_package(name, bazel_package_name, bazel_target_mods, build_file, cached_json_directory,
                    dep_module_aliases, dependencies_index, env, env_inherit, module_aliases, path,
                    repo_mapping, target_deps)

Used to build a local Swift package.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this repository.Namerequired
bazel_package_nameThe short name for the Swift package's Bazel repository.Stringoptional""
bazel_target_modsA JSON string describing buildozer-style modifications that are applied to the declarations in the generated BUILD.bazel file. Set by the swift_deps.bazel_target_set_*, swift_deps.bazel_target_set_select_*, swift_deps.bazel_target_add and swift_deps.bazel_target_add_select tags; direct users should prefer that typed API.Stringoptional""
build_fileWhen used, the provided BUILD file will be used instead of generating one.LabeloptionalNone
cached_json_directory-Stringoptional""
dep_module_aliasesA JSON string mapping package identities to their module alias mappings (SE-0339), e.g. {"swift-log": {"Logging": "SwiftLog"}}. A Swift target in this package is compiled with -module-alias <key>=<value> for the aliases of every package identity this package directly depends on, so its sources can keep importing the original module name. Set by the swift_deps bzlmod extension from the aliases declared in the root package manifest.Stringoptional""
dependencies_indexA JSON file that contains a mapping of Swift products and Swift modules.LabeloptionalNone
envEnvironment variables that will be passed to the execution environments for this repository rule. (e.g. SPM version check, SPM dependency resolution, SPM package description generation)Dictionary: String -> Stringoptional{}
env_inheritEnvironment variables to inherit from the external environment that will be passed to the execution environments for this repository rule. (e.g. SPM version check, SPM dependency resolution, SPM package description generation)List of stringsoptional[]
module_aliasesMapping of Swift module names defined by this package to replacement module names (SE-0339). A Swift target whose module name matches a key is compiled with the value as its module name, and every generated Swift target in this package is compiled with -module-alias <key>=<value> so package sources can keep importing the original name. Bazel targets outside the package graph must import the replacement name. Use this to resolve module name collisions with same-named modules elsewhere in the build (e.g. when using explicit module maps).Dictionary: String -> Stringoptional{}
pathThe path to the local Swift package directory. This can be an absolute path or a path relative to the workspace root. The swift_deps module extension stores a workspace-relative path here whenever the package is inside the workspace root so that the lock file remains portable across machines.Stringrequired
repo_mappingIn WORKSPACE context only: a dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.

For example, an entry "@foo": "@bar" declares that, for any time this repository depends on @foo (such as a dependency on @foo//some:target, it should actually resolve that dependency within globally-declared @bar (@bar//some:target).

This attribute is not supported in MODULE.bazel context (when invoking a repository rule inside a module extension's implementation function).
Dictionary: String -> Stringoptional
target_depsAdditional dependencies to add to generated targets. Keys are Swift package target names, which are mapped to generated implementation target names. If a key already contains .rspm, it is matched as a generated target name unchanged. Values may be Bazel label strings or Swift package target names. Bare value strings and local label strings such as OtherTarget or :OtherTarget are mapped to generated target labels such as :OtherTarget.rspm when they match Swift package targets in the same generated BUILD package. Values that contain .rspm, external labels, cross-package labels, and local labels that do not match package targets are emitted unchanged.Dictionary: String -> List of stringsoptional{}

registry_swift_package

load("@rules_swift_package_manager//swiftpkg:defs.bzl", "registry_swift_package")

registry_swift_package(name, bazel_package_name, bazel_target_mods, build_file,
                       cached_json_directory, dep_module_aliases, dependencies_index, env,
                       env_inherit, id, make_files_read_only, module_aliases, netrc, registries,
                       replace_scm_with_registry, repo_mapping, resolved, target_deps, version)

Used to download and build an external Swift package from a registry.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this repository.Namerequired
bazel_package_nameThe short name for the Swift package's Bazel repository.Stringoptional""
bazel_target_modsA JSON string describing buildozer-style modifications that are applied to the declarations in the generated BUILD.bazel file. Set by the swift_deps.bazel_target_set_*, swift_deps.bazel_target_set_select_*, swift_deps.bazel_target_add and swift_deps.bazel_target_add_select tags; direct users should prefer that typed API.Stringoptional""
build_fileWhen used, the provided BUILD file will be used instead of generating one.LabeloptionalNone
cached_json_directory-Stringoptional""
dep_module_aliasesA JSON string mapping package identities to their module alias mappings (SE-0339), e.g. {"swift-log": {"Logging": "SwiftLog"}}. A Swift target in this package is compiled with -module-alias <key>=<value> for the aliases of every package identity this package directly depends on, so its sources can keep importing the original module name. Set by the swift_deps bzlmod extension from the aliases declared in the root package manifest.Stringoptional""
dependencies_indexA JSON file that contains a mapping of Swift products and Swift modules.LabeloptionalNone
envEnvironment variables that will be passed to the execution environments for this repository rule. (e.g. SPM version check, SPM dependency resolution, SPM package description generation)Dictionary: String -> Stringoptional{}
env_inheritEnvironment variables to inherit from the external environment that will be passed to the execution environments for this repository rule. (e.g. SPM version check, SPM dependency resolution, SPM package description generation)List of stringsoptional[]
idThe package identifier.Stringrequired
make_files_read_onlyWhether to make files in the fetched Swift package read-only. This helps prevent accidental edits when external repositories are exposed by tools such as rules_xcodeproj.BooleanoptionalFalse
module_aliasesMapping of Swift module names defined by this package to replacement module names (SE-0339). A Swift target whose module name matches a key is compiled with the value as its module name, and every generated Swift target in this package is compiled with -module-alias <key>=<value> so package sources can keep importing the original name. Bazel targets outside the package graph must import the replacement name. Use this to resolve module name collisions with same-named modules elsewhere in the build (e.g. when using explicit module maps).Dictionary: String -> Stringoptional{}
netrcA .netrc file that contains authentication credentials used for fetching Swift packages and or binary artifacts.

When provided, this file will be passed to Swift Package Manager commands using the --netrc-file flag during package resolution and updates.
LabeloptionalNone
registriesA registries.json file that defines the configured Swift package registries.

The registries.json file is used when resolving Swift packages from a Swift package registry. It is created by Swift Package Manager when using the swift package-registry commands.

When using the swift_package_tool rules, this file is symlinked to the config_path directory defined in the configure_swift_package tag. If not using the swift_package_tool rules, the file must be in one of Swift Package Manager's search paths or in the manually specified --config-path directory.
LabeloptionalNone
replace_scm_with_registryWhen enabled replaces SCM identities in dependencies package description with identities from the registries.

Using this option requires that the registries provide repositoryURLs as metadata for the package.

When True the equivalent --replace-scm-with-registry option must be used with the Swift Package Manager CLI (or swift_package rule) so that the resolved file includes the version and identity information from the registry.

For more information see the Swift Package Manager documentation.
BooleanoptionalFalse
repo_mappingIn WORKSPACE context only: a dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.

For example, an entry "@foo": "@bar" declares that, for any time this repository depends on @foo (such as a dependency on @foo//some:target, it should actually resolve that dependency within globally-declared @bar (@bar//some:target).

This attribute is not supported in MODULE.bazel context (when invoking a repository rule inside a module extension's implementation function).
Dictionary: String -> Stringoptional
resolvedA Package.resolved, used to de-duplicate dependency identities when use_registry_identity_for_scm or replace_scm_with_registry is enabled.LabeloptionalNone
target_depsAdditional dependencies to add to generated targets. Keys are Swift package target names, which are mapped to generated implementation target names. If a key already contains .rspm, it is matched as a generated target name unchanged. Values may be Bazel label strings or Swift package target names. Bare value strings and local label strings such as OtherTarget or :OtherTarget are mapped to generated target labels such as :OtherTarget.rspm when they match Swift package targets in the same generated BUILD package. Values that contain .rspm, external labels, cross-package labels, and local labels that do not match package targets are emitted unchanged.Dictionary: String -> List of stringsoptional{}
versionThe package version.Stringrequired

swift_package

load("@rules_swift_package_manager//swiftpkg:defs.bzl", "swift_package")

swift_package(name, bazel_package_name, bazel_target_mods, branch, build_file,
              cached_json_directory, commit, dep_module_aliases, dependencies_index, env, env_inherit,
              init_submodules, make_files_read_only, module_aliases, netrc, patch_args, patch_cmds,
              patch_cmds_win, patch_tool, patches, publicly_expose_all_targets,
              recursive_init_submodules, registries, remote, replace_scm_with_registry, repo_mapping,
              shallow_since, tag, target_deps, verbose, version)

Used to download and build an external Swift package.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this repository.Namerequired
bazel_package_nameThe short name for the Swift package's Bazel repository.Stringoptional""
bazel_target_modsA JSON string describing buildozer-style modifications that are applied to the declarations in the generated BUILD.bazel file. Set by the swift_deps.bazel_target_set_*, swift_deps.bazel_target_set_select_*, swift_deps.bazel_target_add and swift_deps.bazel_target_add_select tags; direct users should prefer that typed API.Stringoptional""
branchbranch in the remote repository to checked out. Precisely one of branch, tag, or commit must be specified.Stringoptional""
build_fileWhen used, the provided BUILD file will be used instead of generating one.LabeloptionalNone
cached_json_directory-Stringoptional""
commitThe commit or revision to download from version control.Stringrequired
dep_module_aliasesA JSON string mapping package identities to their module alias mappings (SE-0339), e.g. {"swift-log": {"Logging": "SwiftLog"}}. A Swift target in this package is compiled with -module-alias <key>=<value> for the aliases of every package identity this package directly depends on, so its sources can keep importing the original module name. Set by the swift_deps bzlmod extension from the aliases declared in the root package manifest.Stringoptional""
dependencies_indexA JSON file that contains a mapping of Swift products and Swift modules.LabeloptionalNone
envEnvironment variables that will be passed to the execution environments for this repository rule. (e.g. SPM version check, SPM dependency resolution, SPM package description generation)Dictionary: String -> Stringoptional{}
env_inheritEnvironment variables to inherit from the external environment that will be passed to the execution environments for this repository rule. (e.g. SPM version check, SPM dependency resolution, SPM package description generation)List of stringsoptional[]
init_submodulesWhether to clone submodules in the repository.BooleanoptionalFalse
make_files_read_onlyWhether to make files in the fetched Swift package read-only. This helps prevent accidental edits when external repositories are exposed by tools such as rules_xcodeproj.BooleanoptionalFalse
module_aliasesMapping of Swift module names defined by this package to replacement module names (SE-0339). A Swift target whose module name matches a key is compiled with the value as its module name, and every generated Swift target in this package is compiled with -module-alias <key>=<value> so package sources can keep importing the original name. Bazel targets outside the package graph must import the replacement name. Use this to resolve module name collisions with same-named modules elsewhere in the build (e.g. when using explicit module maps).Dictionary: String -> Stringoptional{}
netrcA .netrc file for authentication when downloading binary artifacts.LabeloptionalNone
patch_argsThe arguments given to the patch tool. Defaults to -p0, however -p1 will usually be needed for patches generated by git. If multiple -p arguments are specified, the last one will take effect.If arguments other than -p are specified, Bazel will fall back to use patch command line tool instead of the Bazel-native patch implementation. When falling back to patch command line tool and patch_tool attribute is not specified, patch will be used.List of stringsoptional["-p0"]
patch_cmdsSequence of Bash commands to be applied on Linux/Macos after patches are applied.List of stringsoptional[]
patch_cmds_winSequence of Powershell commands to be applied on Windows after patches are applied. If this attribute is not set, patch_cmds will be executed on Windows, which requires Bash binary to exist.List of stringsoptional[]
patch_toolThe patch(1) utility to use. If this is specified, Bazel will use the specified patch tool instead of the Bazel-native patch implementation.Stringoptional""
patchesA list of files that are to be applied as patches after extracting the archive. By default, it uses the Bazel-native patch implementation which doesn't support fuzz match and binary patch, but Bazel will fall back to use patch command line tool if patch_tool attribute is specified or there are arguments other than -p in patch_args attribute.List of labelsoptional[]
publicly_expose_all_targetsAllows to expose internal build targets required for package compilation. The structure and labels of exposed targets may change in future releases without requiring a major version bump.BooleanoptionalFalse
recursive_init_submodulesWhether to clone submodules recursively in the repository.BooleanoptionalTrue
registriesThe registries JSON file for the package if using Swift Package Registries.LabeloptionalNone
remoteThe version control location from where the repository should be downloaded.Stringrequired
replace_scm_with_registryWhether to replace SCM references with registry references. Only used if registries is provided.BooleanoptionalFalse
repo_mappingIn WORKSPACE context only: a dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.

For example, an entry "@foo": "@bar" declares that, for any time this repository depends on @foo (such as a dependency on @foo//some:target, it should actually resolve that dependency within globally-declared @bar (@bar//some:target).

This attribute is not supported in MODULE.bazel context (when invoking a repository rule inside a module extension's implementation function).
Dictionary: String -> Stringoptional
shallow_sincean optional date, not after the specified commit; the argument is not allowed if a tag is specified (which allows cloning with depth 1). Setting such a date close to the specified commit allows for a more shallow clone of the repository, saving bandwidth and wall-clock time.Stringoptional""
tagtag in the remote repository to checked out. Precisely one of branch, tag, or commit must be specified.Stringoptional""
target_depsAdditional dependencies to add to generated targets. Keys are Swift package target names, which are mapped to generated implementation target names. If a key already contains .rspm, it is matched as a generated target name unchanged. Values may be Bazel label strings or Swift package target names. Bare value strings and local label strings such as OtherTarget or :OtherTarget are mapped to generated target labels such as :OtherTarget.rspm when they match Swift package targets in the same generated BUILD package. Values that contain .rspm, external labels, cross-package labels, and local labels that do not match package targets are emitted unchanged.Dictionary: String -> List of stringsoptional{}
verbose-BooleanoptionalFalse
versionThe resolved version of the package.Stringoptional""