diff options
author | Klaus Aehlig <aehlig@google.com> | 2016-10-14 13:37:46 +0000 |
---|---|---|
committer | Yun Peng <pcloudy@google.com> | 2016-10-14 20:24:23 +0000 |
commit | f50e6691da8aa685f31165c66a245b85f68abf06 (patch) | |
tree | 2aa5b7835d0ab63e9381b400c77d110f5d9b527d /src/main/java/com/google/devtools/build/lib/vfs/PathFragment.java | |
parent | fa4f24abf21923bb361ea6b01953cf91173ca7ca (diff) |
Expand $(location :label) to string unambiguously a path
In genrules, cmd strings of the form "$(location :label) ..."
are used with the assumption that the executable named by the
label will be called. This holds true as long as $(location :label)
expands to a string that is recognizable as a path, i.e., as long
as :label does not refer to a file in the top-level directory. In the
latter case, however, that string will be the plain file name and the
shell will search for that name in the search path. This will fail, if
'.' is not in the search path; even worse, if a file with that name
is in the search path before '.', then that one will be called which
is not what the user intended to do. Fix this unintended behavior by
expanding $(location :label) to a string that unambiguously is a path.
--
Change-Id: If8681039a8befae6234fbe0cbe3a0f75eedba7aa
Reviewed-on: https://bazel-review.googlesource.com/#/c/6691
MOS_MIGRATED_REVID=136151500
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/vfs/PathFragment.java')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/vfs/PathFragment.java | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/vfs/PathFragment.java b/src/main/java/com/google/devtools/build/lib/vfs/PathFragment.java index bf1ef05dfe..9fd130e3d8 100644 --- a/src/main/java/com/google/devtools/build/lib/vfs/PathFragment.java +++ b/src/main/java/com/google/devtools/build/lib/vfs/PathFragment.java @@ -23,7 +23,6 @@ import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; import com.google.devtools.build.lib.util.OS; import com.google.devtools.build.lib.util.Preconditions; import com.google.devtools.build.lib.util.StringCanonicalizer; - import java.io.File; import java.io.InvalidObjectException; import java.io.ObjectInputStream; @@ -271,6 +270,26 @@ public final class PathFragment implements Comparable<PathFragment>, Serializabl } /** + * Returns the path string using '/' as the name-separator character, but do so in a way + * unambiguously recognizable as path. In other words, return "." for relative and empty paths, + * and prefix relative paths with one segment by "./". + * + * <p>In this way, a shell will always interpret such a string as path (absolute or relative to + * the working directory) and not as command to be searched for in the search path. + */ + public String getCallablePathString() { + if (isAbsolute) { + return getPathString(); + } else if (segmentCount() == 0) { + return "."; + } else if (segmentCount() == 1) { + return "." + SEPARATOR_CHAR + getPathString(); + } else { + return getPathString(); + } + } + + /** * Returns a sequence consisting of the {@link #getSafePathString()} return of each item in * {@code fragments}. */ |