Skip to content

Commit 6ae53e5

Browse files
hvadehracopybara-github
authored andcommitted
Fix singlejar resource mapping for external repositories
Fixes #20882 PiperOrigin-RevId: 598608807 Change-Id: If9230cd1c143eef09beabaf33cb1cb56e4540ae1
1 parent acdb8e7 commit 6ae53e5

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/main/starlark/builtins_bzl/common/java/java_helper.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,13 @@ def _executable_providers(ctx):
293293
return []
294294

295295
def _resource_mapper(file):
296+
root_relative_path = paths.relativize(
297+
path = file.path,
298+
start = paths.join(file.root.path, file.owner.workspace_root),
299+
)
296300
return "%s:%s" % (
297301
file.path,
298-
semantics.get_default_resource_path(file.short_path, segment_extractor = _java_segments),
302+
semantics.get_default_resource_path(root_relative_path, segment_extractor = _java_segments),
299303
)
300304

301305
def _create_single_jar(

src/test/java/com/google/devtools/build/lib/rules/java/JavaStarlarkApiTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3526,6 +3526,57 @@ public void testRunIjarWithOutputParameterIsPrivateApi() throws Exception {
35263526
assertContainsEvent("got unexpected keyword argument: output");
35273527
}
35283528

3529+
@Test
3530+
public void testPackSourcesWithExternalResourceArtifact() throws Exception {
3531+
JavaTestUtil.writeBuildFileForJavaToolchain(scratch);
3532+
scratch.file(
3533+
"foo/custom_rule.bzl",
3534+
"def _impl(ctx):",
3535+
" out = ctx.actions.declare_file('output.jar')",
3536+
" java_toolchain = ctx.attr._java_toolchain[java_common.JavaToolchainInfo]",
3537+
" java_common.pack_sources(",
3538+
" ctx.actions,",
3539+
" java_toolchain = java_toolchain,",
3540+
" output_source_jar = out,",
3541+
" sources = ctx.files.srcs,",
3542+
" )",
3543+
" return [DefaultInfo(files = depset([out]))]",
3544+
"java_custom_library = rule(",
3545+
" implementation = _impl,",
3546+
" attrs = {",
3547+
" 'srcs': attr.label_list(allow_files = True),",
3548+
" '_java_toolchain': attr.label(default = Label('//java/com/google/test:toolchain')),",
3549+
" },",
3550+
" toolchains = ['" + TestConstants.JAVA_TOOLCHAIN_TYPE + "'],",
3551+
" fragments = ['java']",
3552+
")");
3553+
scratch.file("my_other_repo/WORKSPACE");
3554+
scratch.file("my_other_repo/external-file.txt");
3555+
scratch.file("my_other_repo/BUILD", "exports_files(['external-file.txt'])");
3556+
rewriteWorkspace("local_repository(name = 'other_repo', path = './my_other_repo')");
3557+
scratch.file(
3558+
"foo/BUILD",
3559+
"load(':custom_rule.bzl', 'java_custom_library')",
3560+
"java_custom_library(",
3561+
" name = 'custom',",
3562+
" srcs = [",
3563+
" 'internal-file.txt',",
3564+
" '@other_repo//:external-file.txt',",
3565+
" ]",
3566+
")");
3567+
3568+
List<String> arguments =
3569+
((SpawnAction) getGeneratingAction(getConfiguredTarget("//foo:custom"), "foo/output.jar"))
3570+
.getArguments();
3571+
3572+
assertThat(arguments)
3573+
.containsAtLeast(
3574+
"--resources",
3575+
"foo/internal-file.txt:foo/internal-file.txt",
3576+
"external/other_repo/external-file.txt:external-file.txt")
3577+
.inOrder();
3578+
}
3579+
35293580
@Test
35303581
public void mergeAddExports() throws Exception {
35313582
scratch.file(

0 commit comments

Comments
 (0)