Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.0.1] Fix singlejar resource mapping for external repositories #20904

Merged
merged 3 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
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<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