Skip to content
This repository has been archived by the owner on Apr 3, 2023. It is now read-only.

Updated workspace path resolution to work with a default label value. #65

Merged
merged 1 commit into from
Sep 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 3 additions & 25 deletions examples/local_package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ This example demonstrates the use of a local Swift package.

## Local Package with Absolute Path

To reference a local Swift package with an absolute path, one needs to specify the `path` and the
`products`.
To reference a local Swift package, one needs to specify the `path` and the `products` in the
`spm_pkg` declaration. The `path` value can be an absolute path or a relative path. The relative
path is resolved realtive to the root of the Bazel workspace.

```python
spm_repositories(
Expand All @@ -20,29 +21,6 @@ spm_repositories(
)
```

## Local Package with Relative Path

To reference a local Swift package with a relative path, the package reference specifies the `path`
and the `products`. In addition, the `workspace_file` attribute must be provided on the
`spm_repositories` declaration. Specifically, it must be `workspace_file = "//:WORKSPACE"`. This
allows the rule to find the `WORKSPACE` file which is used to evaluate the relative path in the
`spm_pkg` declaration.

```python
spm_repositories(
name = "swift_pkgs",
dependencies = [
# Reference to a local package.
spm_pkg(
path = "third_party/foo-kit",
products = ["FooKit"],
),
],
# If using relative paths, one must provide the `workspace_file`.
workspace_file = "//:WORKSPACE",
)
```

## Shared Dependencies Example

This example, also, demonstrates that Swift package depdendencies which have their own external
Expand Down
2 changes: 0 additions & 2 deletions examples/local_package/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,4 @@ spm_repositories(
products = ["FooKit"],
),
],
# If using relative paths, one must provide the `workspace_file`.
workspace_file = "//:WORKSPACE",
)
14 changes: 5 additions & 9 deletions spm/internal/spm_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -617,12 +617,7 @@ def _configure_spm_repository(repository_ctx, pkgs):
def _prepare_local_package(repository_ctx, pkg):
path = pkg.path
if not paths.is_absolute(path):
if repository_ctx.attr.workspace_file == "":
fail("""\
Need to specify the `workspace_file` attribute when using \
relative paths for local packages.\
""")
repo_root = str(repository_ctx.path(repository_ctx.attr.workspace_file).dirname)
repo_root = str(repository_ctx.path(repository_ctx.attr._workspace_file).dirname)
path = paths.join(repo_root, path)

return packages.copy(pkg, path = path)
Expand Down Expand Up @@ -671,10 +666,11 @@ spm_repositories = repository_rule(
(e.g. .macOS(.v10_15))\
""",
),
"workspace_file": attr.label(
"_workspace_file": attr.label(
default = "@//:WORKSPACE",
doc = """\
If using any local packages, this attribute needs to be set to the value \
`//:WORKSPACE`.\
The value of this label helps the rule find the root of the Bazel \
workspace for local path resolution.\
""",
),
"_package_swift_tpl": attr.label(
Expand Down