From c64bb72324e31b113c1f6ec222aaacf0543546f8 Mon Sep 17 00:00:00 2001 From: Derek Cormier Date: Sun, 8 Jan 2023 01:17:21 -0800 Subject: [PATCH] fix: write_source_files incompatibility with bzlmod when writing from external repo --- .../.bazelrc | 1 + .../.bazelversion | 1 + .../BUILD.bazel | 10 ++++++++++ .../MODULE.bazel | 17 ++++++++++++++++ .../WORKSPACE | 2 ++ .../foobar.txt | 1 + .../test_extension.bzl | 20 +++++++++++++++++++ lib/private/write_source_file.bzl | 3 +-- 8 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 e2e/bzlmod_write_source_files_external/.bazelrc create mode 120000 e2e/bzlmod_write_source_files_external/.bazelversion create mode 100644 e2e/bzlmod_write_source_files_external/BUILD.bazel create mode 100644 e2e/bzlmod_write_source_files_external/MODULE.bazel create mode 100644 e2e/bzlmod_write_source_files_external/WORKSPACE create mode 100644 e2e/bzlmod_write_source_files_external/foobar.txt create mode 100644 e2e/bzlmod_write_source_files_external/test_extension.bzl diff --git a/e2e/bzlmod_write_source_files_external/.bazelrc b/e2e/bzlmod_write_source_files_external/.bazelrc new file mode 100644 index 0000000..3ce91d2 --- /dev/null +++ b/e2e/bzlmod_write_source_files_external/.bazelrc @@ -0,0 +1 @@ +common --enable_bzlmod diff --git a/e2e/bzlmod_write_source_files_external/.bazelversion b/e2e/bzlmod_write_source_files_external/.bazelversion new file mode 120000 index 0000000..96cf949 --- /dev/null +++ b/e2e/bzlmod_write_source_files_external/.bazelversion @@ -0,0 +1 @@ +../../.bazelversion \ No newline at end of file diff --git a/e2e/bzlmod_write_source_files_external/BUILD.bazel b/e2e/bzlmod_write_source_files_external/BUILD.bazel new file mode 100644 index 0000000..bcb49d7 --- /dev/null +++ b/e2e/bzlmod_write_source_files_external/BUILD.bazel @@ -0,0 +1,10 @@ +load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files") + +# This tests that the "in" file to write_source_files can be a +# label to an external repository target when bzlmod is enabled. +write_source_files( + name = "write_external", + files = { + "foobar.txt": "@test//:foobar.txt", + }, +) diff --git a/e2e/bzlmod_write_source_files_external/MODULE.bazel b/e2e/bzlmod_write_source_files_external/MODULE.bazel new file mode 100644 index 0000000..d8d1095 --- /dev/null +++ b/e2e/bzlmod_write_source_files_external/MODULE.bazel @@ -0,0 +1,17 @@ +module( + name = "e2e_bzlmod_write_source_files_external", + compatibility_level = 1, + version = "0.0.0", +) + +bazel_dep(name = "aspect_bazel_lib", version = "0.0.0") + +local_path_override( + module_name = "aspect_bazel_lib", + path = "../..", +) + +test_ext = use_extension("//:test_extension.bzl", "test_ext") + +use_repo(test_ext, "test") + diff --git a/e2e/bzlmod_write_source_files_external/WORKSPACE b/e2e/bzlmod_write_source_files_external/WORKSPACE new file mode 100644 index 0000000..79d35f9 --- /dev/null +++ b/e2e/bzlmod_write_source_files_external/WORKSPACE @@ -0,0 +1,2 @@ +# Marker file that this folder is the root of a Bazel workspace. +# See MODULE.bazel for dependencies and setup. diff --git a/e2e/bzlmod_write_source_files_external/foobar.txt b/e2e/bzlmod_write_source_files_external/foobar.txt new file mode 100644 index 0000000..323fae0 --- /dev/null +++ b/e2e/bzlmod_write_source_files_external/foobar.txt @@ -0,0 +1 @@ +foobar diff --git a/e2e/bzlmod_write_source_files_external/test_extension.bzl b/e2e/bzlmod_write_source_files_external/test_extension.bzl new file mode 100644 index 0000000..6efadad --- /dev/null +++ b/e2e/bzlmod_write_source_files_external/test_extension.bzl @@ -0,0 +1,20 @@ +"Test module extension to create a test repository" + +def _test_repo_impl(rctx): + rctx.file("BUILD.bazel", """\ +exports_files(["foobar.txt"], visibility = ["//visibility:public"]) +""", executable = False) + + rctx.file("foobar.txt", "foobar\n") + +test_repo = repository_rule( + local = True, + implementation = _test_repo_impl, +) + +def _test_ext_impl(_): + test_repo(name = "test") + +test_ext = module_extension( + implementation = _test_ext_impl, +) diff --git a/lib/private/write_source_file.bzl b/lib/private/write_source_file.bzl index 4feac0d..c5adc65 100644 --- a/lib/private/write_source_file.bzl +++ b/lib/private/write_source_file.bzl @@ -57,8 +57,7 @@ def write_source_file( if not out_file: fail("out_file must be specified if in_file is set") - if in_file and out_file: - in_file = utils.to_label(in_file) + if out_file: out_file = utils.to_label(out_file) if utils.is_external_label(out_file):