From a2e228dde0fa8f126afb07eb05166782355583da Mon Sep 17 00:00:00 2001 From: Jason Bedard Date: Wed, 30 Mar 2022 17:04:35 -0700 Subject: [PATCH] fix(write_source_files): fix writing to workspace root (#53) --- .gitignore | 1 + BUILD.bazel | 14 ++++++++++++++ e2e/write_source_files_root.sh | 5 +++++ lib/private/write_source_file.bzl | 2 +- 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100755 e2e/write_source_files_root.sh diff --git a/.gitignore b/.gitignore index ac51a05..b1d35b8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ bazel-* +test-out/ diff --git a/BUILD.bazel b/BUILD.bazel index e7269d1..fade2cf 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -20,3 +20,17 @@ bzl_library( "@bazel_tools//tools/build_defs/repo:utils.bzl", ], ) + +# write_source_files() to a git ignored subdirectory of the root +load("//lib:write_source_files.bzl", "write_source_files") +genrule( + name = "write_source_file_root", + outs = ["write_source_file-root_directory"], + cmd = "mkdir -p $@ && echo 'test' > $@/test.txt", + visibility = ["//visibility:private"], +) +write_source_files( + name = "write_source_file_root-test", + diff_test = False, + files = {"test-out/dist/write_source_file_root-test": ":write_source_file_root"}, +) diff --git a/e2e/write_source_files_root.sh b/e2e/write_source_files_root.sh new file mode 100755 index 0000000..c6ff7ce --- /dev/null +++ b/e2e/write_source_files_root.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +set -e + +bazel run //:write_source_file_root-test diff --git a/lib/private/write_source_file.bzl b/lib/private/write_source_file.bzl index f52327f..1edcb5e 100644 --- a/lib/private/write_source_file.bzl +++ b/lib/private/write_source_file.bzl @@ -266,7 +266,7 @@ def _write_source_file_impl(ctx): else: fail("in file %s must be a single file or a target that provides DefaultOutputPathInfo or DirectoryPathInfo" % ctx.attr.in_file.label) - out_path = "/".join([ctx.label.package, ctx.attr.out_file]) + out_path = "/".join([ctx.label.package, ctx.attr.out_file]) if ctx.label.package else ctx.attr.out_file paths.append((in_path, out_path)) if ctx.attr.is_windows: