diff --git a/scala/private/resources.bzl b/scala/private/resources.bzl index f0e47bcc7..cb457fa3d 100644 --- a/scala/private/resources.bzl +++ b/scala/private/resources.bzl @@ -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): @@ -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 @@ -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 diff --git a/test/BUILD b/test/BUILD index c3b66f4ee..5ff5302c1 100644 --- a/test/BUILD +++ b/test/BUILD @@ -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"], +) diff --git a/test/ScalaTestResourcesFromLocalTargetTest.scala b/test/ScalaTestResourcesFromLocalTargetTest.scala new file mode 100644 index 000000000..717e4dae9 --- /dev/null +++ b/test/ScalaTestResourcesFromLocalTargetTest.scala @@ -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) + } +} \ No newline at end of file diff --git a/test/py_resource.py b/test/py_resource.py new file mode 100644 index 000000000..5cb928578 --- /dev/null +++ b/test/py_resource.py @@ -0,0 +1 @@ +# Empty file just need to be used for reference \ No newline at end of file