Skip to content

Commit

Permalink
Fix resource_strip_prefix of scala_libary for external repositories (b…
Browse files Browse the repository at this point in the history
…azelbuild#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
  • Loading branch information
simuons authored and Andre Rocha committed Jul 6, 2020
1 parent d4362a5 commit 8e5889a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 9 deletions.
5 changes: 5 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
21 changes: 12 additions & 9 deletions scala/private/phases/phase_compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -481,17 +482,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
Expand All @@ -505,12 +506,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 = []
Expand Down
9 changes: 9 additions & 0 deletions test/src/main/scala/scalarules/test/resources/strip/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
workspace(name = "strip_resource_external_workspace")
Original file line number Diff line number Diff line change
@@ -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"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am a text resource!

0 comments on commit 8e5889a

Please sign in to comment.