From 48b35181569cc93552810211c08bc204f7963578 Mon Sep 17 00:00:00 2001 From: Googler Date: Mon, 15 Jan 2024 07:43:35 -0800 Subject: [PATCH] Fix singlejar resource mapping for external repositories Fixes https://github.com/bazelbuild/bazel/issues/20882 PiperOrigin-RevId: 598608807 Change-Id: If9230cd1c143eef09beabaf33cb1cb56e4540ae1 --- .../builtins_bzl/common/java/java_helper.bzl | 6 ++- .../lib/rules/java/JavaStarlarkApiTest.java | 51 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/main/starlark/builtins_bzl/common/java/java_helper.bzl b/src/main/starlark/builtins_bzl/common/java/java_helper.bzl index 0123a1f4abcbd4..b0fa94626f4f98 100644 --- a/src/main/starlark/builtins_bzl/common/java/java_helper.bzl +++ b/src/main/starlark/builtins_bzl/common/java/java_helper.bzl @@ -293,9 +293,13 @@ def _executable_providers(ctx): return [] def _resource_mapper(file): + root_relative_path = paths.relativize( + path = file.path, + start = paths.join(file.root.path, file.owner.workspace_root), + ) return "%s:%s" % ( file.path, - semantics.get_default_resource_path(file.short_path, segment_extractor = _java_segments), + semantics.get_default_resource_path(root_relative_path, segment_extractor = _java_segments), ) def _create_single_jar( diff --git a/src/test/java/com/google/devtools/build/lib/rules/java/JavaStarlarkApiTest.java b/src/test/java/com/google/devtools/build/lib/rules/java/JavaStarlarkApiTest.java index 4f16c0e0d0e58c..ad8641088a3fbf 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/java/JavaStarlarkApiTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/java/JavaStarlarkApiTest.java @@ -3485,6 +3485,57 @@ public void testRunIjarWithOutputParameterIsPrivateApi() throws Exception { assertContainsEvent("got unexpected keyword argument: output"); } + @Test + public void testPackSourcesWithExternalResourceArtifact() throws Exception { + JavaTestUtil.writeBuildFileForJavaToolchain(scratch); + scratch.file( + "foo/custom_rule.bzl", + "def _impl(ctx):", + " out = ctx.actions.declare_file('output.jar')", + " java_toolchain = ctx.attr._java_toolchain[java_common.JavaToolchainInfo]", + " java_common.pack_sources(", + " ctx.actions,", + " java_toolchain = java_toolchain,", + " output_source_jar = out,", + " sources = ctx.files.srcs,", + " )", + " return [DefaultInfo(files = depset([out]))]", + "java_custom_library = rule(", + " implementation = _impl,", + " attrs = {", + " 'srcs': attr.label_list(allow_files = True),", + " '_java_toolchain': attr.label(default = Label('//java/com/google/test:toolchain')),", + " },", + " toolchains = ['" + TestConstants.JAVA_TOOLCHAIN_TYPE + "'],", + " fragments = ['java']", + ")"); + scratch.file("my_other_repo/WORKSPACE"); + scratch.file("my_other_repo/external-file.txt"); + scratch.file("my_other_repo/BUILD", "exports_files(['external-file.txt'])"); + rewriteWorkspace("local_repository(name = 'other_repo', path = './my_other_repo')"); + scratch.file( + "foo/BUILD", + "load(':custom_rule.bzl', 'java_custom_library')", + "java_custom_library(", + " name = 'custom',", + " srcs = [", + " 'internal-file.txt',", + " '@other_repo//:external-file.txt',", + " ]", + ")"); + + List arguments = + ((SpawnAction) getGeneratingAction(getConfiguredTarget("//foo:custom"), "foo/output.jar")) + .getArguments(); + + assertThat(arguments) + .containsAtLeast( + "--resources", + "foo/internal-file.txt:foo/internal-file.txt", + "external/other_repo/external-file.txt:external-file.txt") + .inOrder(); + } + @Test public void mergeAddExports() throws Exception { scratch.file(