aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Laszlo Csomor <laszlocsomor@google.com>2018-03-09 01:02:45 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2018-03-09 01:16:20 -0800
commitb961b0ad6cc2578b98d0a307581e23e73392ad02 (patch)
tree37d355bad1b5cce9df79c4120ddaaa0c9fff33f3
parent62678a40664c9a43d3a05334156a3c6143127ed2 (diff)
runfiles,py,java: rlocation accepts absolute path
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 https://github.com/bazelbuild/bazel/issues/4460 Change-Id: I80474f7cc4736a571bf113438a916f71c36a344d Closes #4806. Change-Id: I80474f7cc4736a571bf113438a916f71c36a344d PiperOrigin-RevId: 188453982
-rw-r--r--src/tools/runfiles/java/com/google/devtools/build/runfiles/Runfiles.java4
-rw-r--r--src/tools/runfiles/java/com/google/devtools/build/runfiles/RunfilesTest.java2
-rw-r--r--src/tools/runfiles/runfiles.py6
-rw-r--r--src/tools/runfiles/runfiles_test.py22
4 files changed, 20 insertions, 14 deletions
diff --git a/src/tools/runfiles/java/com/google/devtools/build/runfiles/Runfiles.java b/src/tools/runfiles/java/com/google/devtools/build/runfiles/Runfiles.java
index c93772bff0..bfe571c8ec 100644
--- a/src/tools/runfiles/java/com/google/devtools/build/runfiles/Runfiles.java
+++ b/src/tools/runfiles/java/com/google/devtools/build/runfiles/Runfiles.java
@@ -95,7 +95,9 @@ public abstract class Runfiles {
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);
diff --git a/src/tools/runfiles/java/com/google/devtools/build/runfiles/RunfilesTest.java b/src/tools/runfiles/java/com/google/devtools/build/runfiles/RunfilesTest.java
index c93ab34d9b..0d1904aff0 100644
--- a/src/tools/runfiles/java/com/google/devtools/build/runfiles/RunfilesTest.java
+++ b/src/tools/runfiles/java/com/google/devtools/build/runfiles/RunfilesTest.java
@@ -52,6 +52,7 @@ public final class RunfilesTest {
assertRlocationArg(r, null, null);
assertRlocationArg(r, "", null);
assertRlocationArg(r, "foo/..", "contains uplevel");
+ assertRlocationArg(r, "\\foo", "path is absolute without a drive letter");
}
@Test
@@ -69,7 +70,6 @@ public final class RunfilesTest {
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 {
diff --git a/src/tools/runfiles/runfiles.py b/src/tools/runfiles/runfiles.py
index 3e062034fb..e4938b74b2 100644
--- a/src/tools/runfiles/runfiles.py
+++ b/src/tools/runfiles/runfiles.py
@@ -128,8 +128,10 @@ class _Runfiles(object):
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):
diff --git a/src/tools/runfiles/runfiles_test.py b/src/tools/runfiles/runfiles_test.py
index e4715568eb..cfc3275288 100644
--- a/src/tools/runfiles/runfiles_test.py
+++ b/src/tools/runfiles/runfiles_test.py
@@ -30,16 +30,8 @@ class RunfilesTest(unittest.TestCase):
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:
@@ -141,6 +133,11 @@ class RunfilesTest(unittest.TestCase):
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
@@ -148,6 +145,11 @@ class RunfilesTest(unittest.TestCase):
# 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():