Skip to content

Commit d681a95

Browse files
simuonsittaiz
authored andcommitted
Fix resource_strip_prefix of scala_libary for external repositories (#944)
* Fix resource_strip_prefix of scala_libary for external repositories If repository `A` has a `scala_library` with `resources` and `resource_strip_prefix` on which repository `B` depends then repository `B` fails to build complaining that resource doesn't start with correct prefix * Push prefix handling down to _adjust_resources_path_by_strip_prefix Use skylib to concat paths * Rewrite test with the external repo This reflects situation described in PR * Run buildifier * Revert external repository * Revrite test with local_repository * Revert visiblity of scala_library
1 parent 5ce9cf5 commit d681a95

File tree

6 files changed

+36
-9
lines changed

6 files changed

+36
-9
lines changed

WORKSPACE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ filegroup(
126126
path = "third_party/test/new_local_repo",
127127
)
128128

129+
local_repository(
130+
name = "strip_resource_external_workspace",
131+
path = "third_party/test/strip_resource_external_workspace",
132+
)
133+
129134
load("@io_bazel_rules_scala//scala:toolchains.bzl", "scala_register_unused_deps_toolchains")
130135

131136
scala_register_unused_deps_toolchains()

scala/private/phases/phase_compile.bzl

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#
44
# DOCUMENT THIS
55
#
6+
load("@bazel_skylib//lib:paths.bzl", _paths = "paths")
67
load("@bazel_tools//tools/jdk:toolchain_utils.bzl", "find_java_runtime_toolchain", "find_java_toolchain")
78
load(
89
"@io_bazel_rules_scala//scala/private:coverage_replacements_provider.bzl",
@@ -480,17 +481,17 @@ def _try_to_compile_java_jar(
480481
java_compilation_provider = provider,
481482
)
482483

483-
def _adjust_resources_path(path, resource_strip_prefix):
484+
def _adjust_resources_path(resource, resource_strip_prefix):
484485
if resource_strip_prefix:
485-
return _adjust_resources_path_by_strip_prefix(path, resource_strip_prefix)
486+
return _adjust_resources_path_by_strip_prefix(resource, resource_strip_prefix)
486487
else:
487-
return _adjust_resources_path_by_default_prefixes(path)
488+
return _adjust_resources_path_by_default_prefixes(resource.path)
488489

489490
def _add_resources_cmd(ctx):
490491
res_cmd = []
491492
for f in ctx.files.resources:
492493
c_dir, res_path = _adjust_resources_path(
493-
f.short_path,
494+
f,
494495
ctx.attr.resource_strip_prefix,
495496
)
496497
target_path = res_path
@@ -504,12 +505,14 @@ def _add_resources_cmd(ctx):
504505
res_cmd.extend([line])
505506
return "".join(res_cmd)
506507

507-
def _adjust_resources_path_by_strip_prefix(path, resource_strip_prefix):
508-
if not path.startswith(resource_strip_prefix):
509-
fail("Resource file %s is not under the specified prefix to strip" % path)
508+
def _adjust_resources_path_by_strip_prefix(resource, resource_strip_prefix):
509+
path = resource.path
510+
prefix = _paths.join(resource.owner.workspace_root, resource_strip_prefix)
511+
if not path.startswith(prefix):
512+
fail("Resource file %s is not under the specified prefix %s to strip" % (path, prefix))
510513

511-
clean_path = path[len(resource_strip_prefix):]
512-
return resource_strip_prefix, clean_path
514+
clean_path = path[len(prefix):]
515+
return prefix, clean_path
513516

514517
def _collect_java_providers_of(deps):
515518
providers = []

test/src/main/scala/scalarules/test/resources/strip/BUILD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,12 @@ scala_specs2_junit_test(
1414
unused_dependency_checker_mode = "off",
1515
deps = [":noSrcsWithResources"],
1616
)
17+
18+
scala_specs2_junit_test(
19+
name = "resouceStripPrefixFromExternalRepoTest",
20+
size = "small",
21+
srcs = ["ResourceStripPrefixTest.scala"],
22+
suffixes = ["Test"],
23+
unused_dependency_checker_mode = "off",
24+
deps = ["@strip_resource_external_workspace//strip:noSrcsWithResources"],
25+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
workspace(name = "strip_resource_external_workspace")
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("@io_bazel_rules_scala//scala:scala.bzl", "scala_library")
2+
3+
scala_library(
4+
name = "noSrcsWithResources",
5+
resource_strip_prefix = "strip",
6+
resources = ["nosrc_jar_resource.txt"],
7+
visibility = ["//visibility:public"],
8+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
I am a text resource!

0 commit comments

Comments
 (0)