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
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this repository. | Name | required | |
| bazel_package_name | The short name for the Swift package's Bazel repository. | String | optional | "" |
| bazel_target_mods | A 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. | String | optional | "" |
| build_file | When used, the provided BUILD file will be used instead of generating one. | Label | optional | None |
| cached_json_directory | - | String | optional | "" |
| dep_module_aliases | A 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. | String | optional | "" |
| dependencies_index | A JSON file that contains a mapping of Swift products and Swift modules. | Label | optional | None |
| env | Environment 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 -> String | optional | {} |
| env_inherit | Environment 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 strings | optional | [] |
| module_aliases | Mapping 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 -> String | optional | {} |
| path | The 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. | String | required | |
| repo_mapping | In 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 -> String | optional | |
| target_deps | Additional 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 strings | optional | {} |
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
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this repository. | Name | required | |
| bazel_package_name | The short name for the Swift package's Bazel repository. | String | optional | "" |
| bazel_target_mods | A 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. | String | optional | "" |
| build_file | When used, the provided BUILD file will be used instead of generating one. | Label | optional | None |
| cached_json_directory | - | String | optional | "" |
| dep_module_aliases | A 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. | String | optional | "" |
| dependencies_index | A JSON file that contains a mapping of Swift products and Swift modules. | Label | optional | None |
| env | Environment 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 -> String | optional | {} |
| env_inherit | Environment 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 strings | optional | [] |
| id | The package identifier. | String | required | |
| make_files_read_only | Whether 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. | Boolean | optional | False |
| module_aliases | Mapping 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 -> String | optional | {} |
| netrc | A .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. | Label | optional | None |
| registries | A 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. | Label | optional | None |
| replace_scm_with_registry | When 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. | Boolean | optional | False |
| repo_mapping | In 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 -> String | optional | |
| resolved | A Package.resolved, used to de-duplicate dependency identities when use_registry_identity_for_scm or replace_scm_with_registry is enabled. | Label | optional | None |
| target_deps | Additional 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 strings | optional | {} |
| version | The package version. | String | required |
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
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this repository. | Name | required | |
| bazel_package_name | The short name for the Swift package's Bazel repository. | String | optional | "" |
| bazel_target_mods | A 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. | String | optional | "" |
| branch | branch in the remote repository to checked out. Precisely one of branch, tag, or commit must be specified. | String | optional | "" |
| build_file | When used, the provided BUILD file will be used instead of generating one. | Label | optional | None |
| cached_json_directory | - | String | optional | "" |
| commit | The commit or revision to download from version control. | String | required | |
| dep_module_aliases | A 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. | String | optional | "" |
| dependencies_index | A JSON file that contains a mapping of Swift products and Swift modules. | Label | optional | None |
| env | Environment 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 -> String | optional | {} |
| env_inherit | Environment 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 strings | optional | [] |
| init_submodules | Whether to clone submodules in the repository. | Boolean | optional | False |
| make_files_read_only | Whether 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. | Boolean | optional | False |
| module_aliases | Mapping 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 -> String | optional | {} |
| netrc | A .netrc file for authentication when downloading binary artifacts. | Label | optional | None |
| patch_args | The 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 strings | optional | ["-p0"] |
| patch_cmds | Sequence of Bash commands to be applied on Linux/Macos after patches are applied. | List of strings | optional | [] |
| patch_cmds_win | Sequence 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 strings | optional | [] |
| patch_tool | The patch(1) utility to use. If this is specified, Bazel will use the specified patch tool instead of the Bazel-native patch implementation. | String | optional | "" |
| patches | A 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 labels | optional | [] |
| publicly_expose_all_targets | Allows 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. | Boolean | optional | False |
| recursive_init_submodules | Whether to clone submodules recursively in the repository. | Boolean | optional | True |
| registries | The registries JSON file for the package if using Swift Package Registries. | Label | optional | None |
| remote | The version control location from where the repository should be downloaded. | String | required | |
| replace_scm_with_registry | Whether to replace SCM references with registry references. Only used if registries is provided. | Boolean | optional | False |
| repo_mapping | In 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 -> String | optional | |
| shallow_since | an 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. | String | optional | "" |
| tag | tag in the remote repository to checked out. Precisely one of branch, tag, or commit must be specified. | String | optional | "" |
| target_deps | Additional 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 strings | optional | {} |
| verbose | - | Boolean | optional | False |
| version | The resolved version of the package. | String | optional | "" |