Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: always include files from the same workspace as the build target in copy_to_directory() #360

Merged
merged 3 commits into from
Oct 10, 2023
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
1 change: 1 addition & 0 deletions .aspect/workflows/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ workspaces:
- e2e/smoke
- e2e/coreutils
- e2e/copy_to_directory
- e2e/external_copy_to_directory
bazel:
flags:
- --remote_download_minimal
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ jobs:
- "."
- "e2e/coreutils"
- "e2e/copy_to_directory"
- "e2e/external_copy_to_directory"
- "e2e/smoke"
exclude:
# Don't test Bazel 5.3 with bzlmod. (Unrecognized option: --enable_bzlmod)
Expand Down
2 changes: 1 addition & 1 deletion docs/copy_to_directory.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions e2e/external_copy_to_directory/.bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
app/
Empty file.
1 change: 1 addition & 0 deletions e2e/external_copy_to_directory/.bazelversion
13 changes: 13 additions & 0 deletions e2e/external_copy_to_directory/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load(":directory.bzl", "directory")

directory(
name = "dir",
srcs = ["foo.txt"],
visibility = ["//visibility:public"],
)

build_test(
name = "test",
targets = [":dir"],
)
28 changes: 28 additions & 0 deletions e2e/external_copy_to_directory/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
workspace(name = "external_copy_to_directory")

local_repository(
name = "aspect_bazel_lib",
path = "../..",
)

load("@aspect_bazel_lib//:internal_deps.bzl", "bazel_lib_internal_deps")

bazel_lib_internal_deps()

load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies")

aspect_bazel_lib_dependencies()

load("@aspect_bazel_lib//:deps.bzl", "go_dependencies")

go_dependencies()

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

go_rules_dependencies()

go_register_toolchains(version = "1.18.3")

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")

gazelle_dependencies()
Empty file.
1 change: 1 addition & 0 deletions e2e/external_copy_to_directory/app/.bazelversion
6 changes: 6 additions & 0 deletions e2e/external_copy_to_directory/app/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sh_test(
name = "test",
srcs = ["test.sh"],
data = ["@external_copy_to_directory//:dir"],
deps = ["@bazel_tools//tools/bash/runfiles"],
)
31 changes: 31 additions & 0 deletions e2e/external_copy_to_directory/app/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
local_repository(
name = "external_copy_to_directory",
path = "..",
)

local_repository(
name = "aspect_bazel_lib",
path = "../../..",
)

load("@aspect_bazel_lib//:internal_deps.bzl", "bazel_lib_internal_deps")

bazel_lib_internal_deps()

load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies")

aspect_bazel_lib_dependencies()

load("@aspect_bazel_lib//:deps.bzl", "go_dependencies")

go_dependencies()

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

go_rules_dependencies()

go_register_toolchains(version = "1.18.3")

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")

gazelle_dependencies()
19 changes: 19 additions & 0 deletions e2e/external_copy_to_directory/app/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# --- begin runfiles.bash initialization v2 ---
# Copy-pasted from the Bazel Bash runfiles library v2.
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
source "$0.runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v2 ---

# Read external directory and make sure it exists with a file.
readonly DIR=$(rlocation external_copy_to_directory/dir/)
readonly FILES=$(ls "${DIR}" | wc -l)
if [[ ${FILES} != 1 ]]; then
echo "Expected exactly 1 file under external directory, but found ${FILES}:" >&2
ls "${DIR}" >&2
exit 1
fi
36 changes: 36 additions & 0 deletions e2e/external_copy_to_directory/directory.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Test rule executing `copy_to_directory_bin_action`."""

load("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory_bin_action")

def _directory_impl(ctx):
dst = ctx.actions.declare_directory(ctx.attr.name)

copy_to_directory_bin_action(
ctx,
name = ctx.attr.name,
copy_to_directory_bin = ctx.executable._tool,
dst = dst,
files = ctx.files.srcs,
verbose = True,
)

return DefaultInfo(files = depset([dst]))

directory = rule(
implementation = _directory_impl,
attrs = {
"srcs": attr.label_list(
mandatory = True,
allow_files = True,
),
"_tool": attr.label(
executable = True,
cfg = "exec",
default = "@aspect_bazel_lib//tools/copy_to_directory",
),
},
doc = """
Copies the given source files to a directory with
`copy_to_directory_bin_action()`.
""",
)
1 change: 1 addition & 0 deletions e2e/external_copy_to_directory/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo
4 changes: 2 additions & 2 deletions lib/private/copy_to_directory.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Globs are supported (see rule docstring above).

Files from external repositories are only copied into the output directory if
the external repository they come from matches one of the external repository patterns
specified.
specified or if they are in the same external repository as this target.

When copied from an external repository, the file path in the output directory
defaults to the file's path within the external repository. The external repository
Expand Down Expand Up @@ -840,7 +840,7 @@ def copy_to_directory_bin_action(
inputs = file_inputs + [config_file],
outputs = [dst],
executable = copy_to_directory_bin,
arguments = [config_file.path],
arguments = [config_file.path, ctx.label.workspace_name],
mnemonic = "CopyToDirectory",
progress_message = "Copying files to directory %s" % _progress_path(dst),
execution_requirements = override_execution_requirements or execution_requirements_for_copy(ctx),
Expand Down