diff --git a/lib/paths.bzl b/lib/paths.bzl index ed74a60..9758151 100644 --- a/lib/paths.bzl +++ b/lib/paths.bzl @@ -162,7 +162,8 @@ def _relativize(path, start): will fail if `path` is not beneath `start` (rather than use parent segments to walk up to the common file system root). - Relativizing paths that start with parent directory references is not allowed. + Relativizing paths that start with parent directory references only works if + the path both start with the same initial parent references. Args: path: The path to relativize. @@ -176,9 +177,6 @@ def _relativize(path, start): start_segments = [] start_length = len(start_segments) - if (path.startswith("..") or start.startswith("..")): - fail("Cannot relativize paths above the current (unknown) directory") - if (path.startswith("/") != start.startswith("/") or len(segments) < start_length): fail("Path '%s' is not beneath '%s'" % (path, start)) diff --git a/tests/paths_tests.bzl b/tests/paths_tests.bzl index 5765916..c11473d 100644 --- a/tests/paths_tests.bzl +++ b/tests/paths_tests.bzl @@ -195,6 +195,10 @@ def _relativize_test(ctx): # Try a case where a parent directory is normalized away. asserts.equals(env, "baz", paths.relativize("foo/bar/../baz", "foo")) + # Relative paths work, as long as they share a common start. + asserts.equals(env, "file", paths.relativize("../foo/bar/baz/file", "../foo/bar/baz")) + asserts.equals(env, "baz/file", paths.relativize("../foo/bar/baz/file", "../foo/bar")) + # TODO(allevato): Test failure cases, once that is possible. unittest.end(env)