Skip to content

Commit

Permalink
Return resource's short_path when fail to find relative (bazelbuild#1010
Browse files Browse the repository at this point in the history
)

* Return resource's short_path when fail to find relative
  • Loading branch information
David Haxton authored and Andre Rocha committed Jul 6, 2020
1 parent 045ae94 commit 2316e4a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
12 changes: 9 additions & 3 deletions scala/private/resources.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def paths(resources, resource_strip_prefix):
return [(_target_path(resource, resource_strip_prefix), resource.path) for resource in resources]

def _target_path(resource, resource_strip_prefix):
path = _target_path_by_strip_prefix(resource, resource_strip_prefix) if resource_strip_prefix else _target_path_by_default_prefixes(resource.path)
path = _target_path_by_strip_prefix(resource, resource_strip_prefix) if resource_strip_prefix else _target_path_by_default_prefixes(resource)
return _strip_prefix(path, "/")

def _target_path_by_strip_prefix(resource, resource_strip_prefix):
Expand All @@ -31,7 +31,9 @@ def _target_path_by_strip_prefix(resource, resource_strip_prefix):
fail("Resource file %s is not under the specified prefix %s to strip" % (path, prefix))
return path[len(prefix):]

def _target_path_by_default_prefixes(path):
def _target_path_by_default_prefixes(resource):
path = resource.path

# Here we are looking to find out the offset of this resource inside
# any resources folder. We want to return the root to the resources folder
# and then the sub path inside it
Expand All @@ -44,7 +46,11 @@ def _target_path_by_default_prefixes(path):
if rel_path:
return rel_path

return path
# Both short_path and path have quirks we wish to avoid, in short_path there are times where
# it is prefixed by `../` instead of `external/`. And in .path it will instead return the entire
# bazel-out/... path, which is also wanting to be avoided. So instead, we return the short-path if
# path starts with bazel-out and the entire path if it does not.
return resource.short_path if path.startswith("bazel-out") else path

def _strip_prefix(path, prefix):
return path[len(prefix):] if path.startswith(prefix) else path
12 changes: 12 additions & 0 deletions test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -755,3 +755,15 @@ scala_junit_test(
tests_from = [":JunitMixedSeparateTarget"],
runtime_deps = [":JunitMixedSeparateTarget"],
)

py_binary(
name = "py_resource_binary",
srcs = ["py_resource.py"],
main = "py_resource.py",
)

scala_test(
name = "ScalaTestResourcesFromLocalTargetTest",
srcs = ["ScalaTestResourcesFromLocalTargetTest.scala"],
resources = [":py_resource_binary"],
)
9 changes: 9 additions & 0 deletions test/ScalaTestResourcesFromLocalTargetTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package scalarules.test

class ScalaTestResourcesFromLocalTargetTest extends org.scalatest.FlatSpec {
"scala_test's resources when referencing local target" should
"assert that local target is not placed in bazel-out, but rather next to the packaged code" in {
assert(getClass.getResource("/bazel-out/darwin-fastbuild/bin/test/py_resource_binary") == null)
assert(getClass.getResource("/test/py_resource_binary") != null)
}
}
1 change: 1 addition & 0 deletions test/py_resource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Empty file just need to be used for reference

0 comments on commit 2316e4a

Please sign in to comment.