PY-31773 Prepend (not append) virtualenv's bin/ directory to PATH

It should take precedence according to the virtualenvs 'activate' script.
This commit is contained in:
Andrey Vlasovskikh
2018-10-11 15:27:03 +03:00
parent 03d4b7df58
commit 54aaec4828
3 changed files with 18 additions and 7 deletions
@@ -383,7 +383,7 @@ public abstract class PythonCommandLineState extends CommandLineState {
for (Map.Entry<String, String> e : myConfig.getEnvs().entrySet()) {
if (environment.containsKey(e.getKey())) {
if ("PATH".equals(e.getKey())) {
env.put(e.getKey(), PythonEnvUtil.appendToPathEnvVar(env.get("PATH"), e.getValue()));
env.put(e.getKey(), PythonEnvUtil.addToPathEnvVar(env.get("PATH"), e.getValue(), true));
}
else {
env.put(e.getKey(), e.getValue());
@@ -60,18 +60,29 @@ public class PythonEnvUtil {
}
/**
* Appends a value to the end os a path-like environment variable, using system-dependent path separator.
* Adds a value to the end os a path-like environment variable, using system-dependent path separator.
*
* @param source path-like string to append to
* @param value what to append
* @param asPrefix if true, adds a value as a prefix, otherwise as a suffix
* @return modified path-like string
*/
@NotNull
public static String appendToPathEnvVar(@Nullable String source, @NotNull String value) {
public static String addToPathEnvVar(@Nullable String source, @NotNull String value, boolean asPrefix) {
if (StringUtil.isEmpty(source)) return value;
Set<String> paths = Sets.newHashSet(source.split(File.pathSeparator));
return !paths.contains(value) ? source + File.pathSeparator + value : source;
if (!paths.contains(value)) {
if (asPrefix) {
return value + File.pathSeparator + source;
}
else {
return source + File.pathSeparator + value;
}
}
else {
return source;
}
}
public static void addPathsToEnv(@NotNull Map<String, String> env, String key, @NotNull Collection<String> values) {
@@ -83,7 +94,7 @@ public class PythonEnvUtil {
public static void addPathToEnv(@NotNull Map<String, String> env, String key, String value) {
if (!StringUtil.isEmpty(value)) {
if (env.containsKey(key)) {
env.put(key, appendToPathEnvVar(env.get(key), value));
env.put(key, addToPathEnvVar(env.get(key), value, false));
}
else {
env.put(key, value);
@@ -363,11 +363,11 @@ public final class PythonSdkType extends SdkType {
Map<String, String> env = commandLine.getEnvironment();
String pathValue;
if (env.containsKey(PATH)) {
pathValue = PythonEnvUtil.appendToPathEnvVar(env.get(PATH), virtualenvBin);
pathValue = PythonEnvUtil.addToPathEnvVar(env.get(PATH), virtualenvBin, true);
}
else if (passParentEnvironment) {
// append to PATH
pathValue = PythonEnvUtil.appendToPathEnvVar(System.getenv(PATH), virtualenvBin);
pathValue = PythonEnvUtil.addToPathEnvVar(System.getenv(PATH), virtualenvBin, true);
}
else {
pathValue = virtualenvBin;