diff --git a/WORKSPACE b/WORKSPACE index 2dd6084e8..4094d66e3 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -126,6 +126,11 @@ filegroup( path = "third_party/test/new_local_repo", ) +local_repository( + name = "strip_resource_external_workspace", + path = "third_party/test/strip_resource_external_workspace", +) + load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_register_unused_deps_toolchains") scala_register_unused_deps_toolchains() diff --git a/scala/private/phases/phase_compile.bzl b/scala/private/phases/phase_compile.bzl index 0500e2f37..ca846e81a 100644 --- a/scala/private/phases/phase_compile.bzl +++ b/scala/private/phases/phase_compile.bzl @@ -3,6 +3,7 @@ # # DOCUMENT THIS # +load("@bazel_skylib//lib:paths.bzl", _paths = "paths") load("@bazel_tools//tools/jdk:toolchain_utils.bzl", "find_java_runtime_toolchain", "find_java_toolchain") load( "@io_bazel_rules_scala//scala/private:coverage_replacements_provider.bzl", @@ -480,17 +481,17 @@ def _try_to_compile_java_jar( java_compilation_provider = provider, ) -def _adjust_resources_path(path, resource_strip_prefix): +def _adjust_resources_path(resource, resource_strip_prefix): if resource_strip_prefix: - return _adjust_resources_path_by_strip_prefix(path, resource_strip_prefix) + return _adjust_resources_path_by_strip_prefix(resource, resource_strip_prefix) else: - return _adjust_resources_path_by_default_prefixes(path) + return _adjust_resources_path_by_default_prefixes(resource.path) def _add_resources_cmd(ctx): res_cmd = [] for f in ctx.files.resources: c_dir, res_path = _adjust_resources_path( - f.short_path, + f, ctx.attr.resource_strip_prefix, ) target_path = res_path @@ -504,12 +505,14 @@ def _add_resources_cmd(ctx): res_cmd.extend([line]) return "".join(res_cmd) -def _adjust_resources_path_by_strip_prefix(path, resource_strip_prefix): - if not path.startswith(resource_strip_prefix): - fail("Resource file %s is not under the specified prefix to strip" % path) +def _adjust_resources_path_by_strip_prefix(resource, resource_strip_prefix): + path = resource.path + prefix = _paths.join(resource.owner.workspace_root, resource_strip_prefix) + if not path.startswith(prefix): + fail("Resource file %s is not under the specified prefix %s to strip" % (path, prefix)) - clean_path = path[len(resource_strip_prefix):] - return resource_strip_prefix, clean_path + clean_path = path[len(prefix):] + return prefix, clean_path def _collect_java_providers_of(deps): providers = [] diff --git a/test/src/main/scala/scalarules/test/resources/strip/BUILD b/test/src/main/scala/scalarules/test/resources/strip/BUILD index b4036aaf9..b13cb4542 100644 --- a/test/src/main/scala/scalarules/test/resources/strip/BUILD +++ b/test/src/main/scala/scalarules/test/resources/strip/BUILD @@ -14,3 +14,12 @@ scala_specs2_junit_test( unused_dependency_checker_mode = "off", deps = [":noSrcsWithResources"], ) + +scala_specs2_junit_test( + name = "resouceStripPrefixFromExternalRepoTest", + size = "small", + srcs = ["ResourceStripPrefixTest.scala"], + suffixes = ["Test"], + unused_dependency_checker_mode = "off", + deps = ["@strip_resource_external_workspace//strip:noSrcsWithResources"], +) diff --git a/third_party/test/strip_resource_external_workspace/WORKSPACE b/third_party/test/strip_resource_external_workspace/WORKSPACE new file mode 100644 index 000000000..e5d127dc1 --- /dev/null +++ b/third_party/test/strip_resource_external_workspace/WORKSPACE @@ -0,0 +1 @@ +workspace(name = "strip_resource_external_workspace") diff --git a/third_party/test/strip_resource_external_workspace/strip/BUILD.bazel b/third_party/test/strip_resource_external_workspace/strip/BUILD.bazel new file mode 100644 index 000000000..2e2167d71 --- /dev/null +++ b/third_party/test/strip_resource_external_workspace/strip/BUILD.bazel @@ -0,0 +1,8 @@ +load("@io_bazel_rules_scala//scala:scala.bzl", "scala_library") + +scala_library( + name = "noSrcsWithResources", + resource_strip_prefix = "strip", + resources = ["nosrc_jar_resource.txt"], + visibility = ["//visibility:public"], +) diff --git a/third_party/test/strip_resource_external_workspace/strip/nosrc_jar_resource.txt b/third_party/test/strip_resource_external_workspace/strip/nosrc_jar_resource.txt new file mode 100644 index 000000000..b5bf914d1 --- /dev/null +++ b/third_party/test/strip_resource_external_workspace/strip/nosrc_jar_resource.txt @@ -0,0 +1 @@ +I am a text resource!