Skip to content

Commit 2634a6e

Browse files
bazel-iohvadehra
andauthored
[7.0.1] Fix singlejar resource mapping for external repositories (#20904)
Fixes #20882 Commit 6ae53e5 PiperOrigin-RevId: 598608807 Change-Id: If9230cd1c143eef09beabaf33cb1cb56e4540ae1 --------- Co-authored-by: Googler <[email protected]> Co-authored-by: hvd <[email protected]>
1 parent a42bea7 commit 2634a6e

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
@@ -3485,6 +3485,57 @@ public void testRunIjarWithOutputParameterIsPrivateApi() throws Exception {
34853485
assertContainsEvent("got unexpected keyword argument: output");
34863486
}
34873487

3488+
@Test
3489+
public void testPackSourcesWithExternalResourceArtifact() throws Exception {
3490+
JavaToolchainTestUtil.writeBuildFileForJavaToolchain(scratch);
3491+
scratch.file(
3492+
"foo/custom_rule.bzl",
3493+
"def _impl(ctx):",
3494+
" out = ctx.actions.declare_file('output.jar')",
3495+
" java_toolchain = ctx.attr._java_toolchain[java_common.JavaToolchainInfo]",
3496+
" java_common.pack_sources(",
3497+
" ctx.actions,",
3498+
" java_toolchain = java_toolchain,",
3499+
" output_source_jar = out,",
3500+
" sources = ctx.files.srcs,",
3501+
" )",
3502+
" return [DefaultInfo(files = depset([out]))]",
3503+
"java_custom_library = rule(",
3504+
" implementation = _impl,",
3505+
" attrs = {",
3506+
" 'srcs': attr.label_list(allow_files = True),",
3507+
" '_java_toolchain': attr.label(default = Label('//java/com/google/test:toolchain')),",
3508+
" },",
3509+
" toolchains = ['" + TestConstants.JAVA_TOOLCHAIN_TYPE + "'],",
3510+
" fragments = ['java']",
3511+
")");
3512+
scratch.file("my_other_repo/WORKSPACE");
3513+
scratch.file("my_other_repo/external-file.txt");
3514+
scratch.file("my_other_repo/BUILD", "exports_files(['external-file.txt'])");
3515+
rewriteWorkspace("local_repository(name = 'other_repo', path = './my_other_repo')");
3516+
scratch.file(
3517+
"foo/BUILD",
3518+
"load(':custom_rule.bzl', 'java_custom_library')",
3519+
"java_custom_library(",
3520+
" name = 'custom',",
3521+
" srcs = [",
3522+
" 'internal-file.txt',",
3523+
" '@other_repo//:external-file.txt',",
3524+
" ]",
3525+
")");
3526+
3527+
List<String> arguments =
3528+
((SpawnAction) getGeneratingAction(getConfiguredTarget("//foo:custom"), "foo/output.jar"))
3529+
.getArguments();
3530+
3531+
assertThat(arguments)
3532+
.containsAtLeast(
3533+
"--resources",
3534+
"foo/internal-file.txt:foo/internal-file.txt",
3535+
"external/other_repo/external-file.txt:external-file.txt")
3536+
.inOrder();
3537+
}
3538+
34883539
@Test
34893540
public void mergeAddExports() throws Exception {
34903541
scratch.file(

0 commit comments

Comments
 (0)