Skip to content

Commit

Permalink
Expand $(location :label) to string unambiguously a path
Browse files Browse the repository at this point in the history
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
  • Loading branch information
aehlig authored and meteorcloudy committed Oct 14, 2016
1 parent fa4f24a commit f50e669
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.google.devtools.build.lib.packages.OutputFile;
import com.google.devtools.build.lib.rules.AliasProvider;
import com.google.devtools.build.lib.vfs.PathFragment;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -335,7 +334,7 @@ private static Set<String> getPaths(Collection<Artifact> artifacts, boolean take
PathFragment execPath =
takeExecPath ? artifact.getExecPath() : artifact.getRootRelativePath();
if (execPath != null) { // omit middlemen etc
paths.add(execPath.getPathString());
paths.add(execPath.getCallablePathString());
}
}
return paths;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
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;
Expand Down Expand Up @@ -270,6 +269,26 @@ public String getSafePathString() {
return (!isAbsolute && (segmentCount() == 0)) ? "." : getPathString();
}

/**
* 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}.
Expand Down

0 comments on commit f50e669

Please sign in to comment.