Skip to content

Commit

Permalink
[7.0.1] Fix singlejar resource mapping for external repositories (#20904
Browse files Browse the repository at this point in the history
)

Fixes #20882

Commit
6ae53e5

PiperOrigin-RevId: 598608807
Change-Id: If9230cd1c143eef09beabaf33cb1cb56e4540ae1

---------

Co-authored-by: Googler <[email protected]>
Co-authored-by: hvd <[email protected]>
  • Loading branch information
3 people authored Jan 16, 2024
1 parent a42bea7 commit 2634a6e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/main/starlark/builtins_bzl/common/java/java_helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3485,6 +3485,57 @@ public void testRunIjarWithOutputParameterIsPrivateApi() throws Exception {
assertContainsEvent("got unexpected keyword argument: output");
}

@Test
public void testPackSourcesWithExternalResourceArtifact() throws Exception {
JavaToolchainTestUtil.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<String> 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(
Expand Down

0 comments on commit 2634a6e

Please sign in to comment.