Skip to content

Commit

Permalink
runfiles,py,java: rlocation accepts absolute path
Browse files Browse the repository at this point in the history
The rlocation functions in the Python and Java
runfiles libraries (under
@bazel_tools//tools/runfiles) now consistently
return the argument itself if it is an absolute
path.

If the argument is a driveless absolute Windows
path (e.g. "\\windows\\system32") then rlocation
reports an error.

See #4460

Change-Id: I80474f7cc4736a571bf113438a916f71c36a344d

Closes #4806.

Change-Id: I80474f7cc4736a571bf113438a916f71c36a344d
PiperOrigin-RevId: 188453982
  • Loading branch information
laszlocsomor authored and Copybara-Service committed Mar 9, 2018
1 parent 62678a4 commit b961b0a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ public final String rlocation(String path) {
Util.checkArgument(path != null);
Util.checkArgument(!path.isEmpty());
Util.checkArgument(!path.contains(".."), "path contains uplevel references: \"%s\"", path);
if (new File(path).isAbsolute() || path.charAt(0) == File.separatorChar) {
Util.checkArgument(
!path.startsWith("\\"), "path is absolute without a drive letter: \"%s\"", path);
if (new File(path).isAbsolute()) {
return path;
}
return rlocationChecked(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void testRlocationArgumentValidation() throws Exception {
assertRlocationArg(r, null, null);
assertRlocationArg(r, "", null);
assertRlocationArg(r, "foo/..", "contains uplevel");
assertRlocationArg(r, "\\foo", "path is absolute without a drive letter");
}

@Test
Expand All @@ -69,7 +70,6 @@ public void testCreatesManifestBasedRunfiles() throws Exception {
assertThat(r.rlocation("foo")).isNull();

if (isWindows()) {
assertThat(r.rlocation("\\foo")).isEqualTo("\\foo");
assertThat(r.rlocation("c:/foo")).isEqualTo("c:/foo");
assertThat(r.rlocation("c:\\foo")).isEqualTo("c:\\foo");
} else {
Expand Down
6 changes: 4 additions & 2 deletions src/tools/runfiles/runfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@ def Rlocation(self, path):
raise TypeError()
if ".." in path:
raise ValueError("path contains uplevel references: \"%s\"" % path)
if os.path.isabs(path) or path[0] == os.sep:
raise ValueError("path is absolute: \"%s\"" % path)
if path[0] == "\\":
raise ValueError("path is absolute without a drive letter: \"%s\"" % path)
if os.path.isabs(path):
return path
return self._strategy.RlocationChecked(path)

def EnvVars(self):
Expand Down
22 changes: 12 additions & 10 deletions src/tools/runfiles/runfiles_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,8 @@ def testRlocationArgumentValidation(self):
self.assertRaises(TypeError, lambda: r.Rlocation(1))
self.assertRaisesRegexp(ValueError, "contains uplevel",
lambda: r.Rlocation("foo/.."))
if RunfilesTest.IsWindows():
self.assertRaisesRegexp(ValueError, "is absolute",
lambda: r.Rlocation("\\foo"))
self.assertRaisesRegexp(ValueError, "is absolute",
lambda: r.Rlocation("c:/foo"))
self.assertRaisesRegexp(ValueError, "is absolute",
lambda: r.Rlocation("c:\\foo"))
else:
self.assertRaisesRegexp(ValueError, "is absolute",
lambda: r.Rlocation("/foo"))
self.assertRaisesRegexp(ValueError, "is absolute without a drive letter",
lambda: r.Rlocation("\\foo"))

def testCreatesManifestBasedRunfiles(self):
with _MockFile(contents=["a/b c/d"]) as mf:
Expand Down Expand Up @@ -141,13 +133,23 @@ def testManifestBasedRlocation(self):
self.assertEqual(
r.Rlocation("Foo/Bar/runfile3"), "D:\\the path\\run file 3.txt")
self.assertIsNone(r.Rlocation("unknown"))
if RunfilesTest.IsWindows():
self.assertEqual(r.Rlocation("c:/foo"), "c:/foo")
self.assertEqual(r.Rlocation("c:\\foo"), "c:\\foo")
else:
self.assertEqual(r.Rlocation("/foo"), "/foo")

def testDirectoryBasedRlocation(self):
# The _DirectoryBased strategy simply joins the runfiles directory and the
# runfile's path on a "/". This strategy does not perform any normalization,
# nor does it check that the path exists.
r = runfiles.CreateDirectoryBased("foo/bar baz//qux/")
self.assertEqual(r.Rlocation("arg"), "foo/bar baz//qux/arg")
if RunfilesTest.IsWindows():
self.assertEqual(r.Rlocation("c:/foo"), "c:/foo")
self.assertEqual(r.Rlocation("c:\\foo"), "c:\\foo")
else:
self.assertEqual(r.Rlocation("/foo"), "/foo")

@staticmethod
def IsWindows():
Expand Down

0 comments on commit b961b0a

Please sign in to comment.