Skip to content

Commit

Permalink
Improve heuristic which determines if a folder should be added to the…
Browse files Browse the repository at this point in the history
… interpreter pythonpath or not to consider virtual envs in the project itself.
  • Loading branch information
fabioz committed Nov 10, 2024
1 parent b762c4e commit 066ffff
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public boolean selectByDefault(String data) {
return !isChildOfRootPath(data, rootPaths);
}

public boolean forceDeselect(String data) {
return isRootPath(data, rootPaths);
}

public boolean exists(String data) {
return new File(data).exists();
}
Expand Down Expand Up @@ -74,6 +78,21 @@ public static boolean isChildOfRootPath(String data, Set<IPath> rootPaths) {
return false;
}

public static boolean isRootPath(String data, Set<IPath> rootPaths) {
java.nio.file.Path nativePath = new File(data).toPath();

for (IPath p : rootPaths) {
try {
if (Files.isSameFile(nativePath, p.toFile().toPath())) {
return true;
}
} catch (IOException e) {
Log.log(e);
}
}
return false;
}

/**
* Creates a Set of the root paths of all projects (and the workspace root itself).
* @return A HashSet of root paths.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.SafeRunner;
Expand Down Expand Up @@ -375,6 +376,8 @@ public static InterpreterInfo fromString(String received, boolean askUserInOutPa
DefaultPathsForInterpreterInfo defaultPaths = new DefaultPathsForInterpreterInfo(
resolvingInterpreter);

Set<IPath> pythonHomePaths = new HashSet<>();

for (int j = 0; j < xmlNodes.getLength(); j++) {
Node xmlChild = xmlNodes.item(j);
String name = xmlChild.getNodeName();
Expand All @@ -388,6 +391,14 @@ public static InterpreterInfo fromString(String received, boolean askUserInOutPa
} else if ("executable".equals(name)) {
infoExecutable = data;

// Handling of python inside of project.
File pythonExecutableFile = new File(infoExecutable);
File pythonHome = pythonExecutableFile.getParentFile();
if (new File(pythonHome, "pyenv.cfg").exists()) {
pythonHome = pythonHome.getParentFile();
}
pythonHomePaths.add(Path.fromOSString(pythonHome.toString()));

} else if ("vmArgs".equals(name)) {
infoVmArgs = data;

Expand All @@ -410,10 +421,17 @@ public static InterpreterInfo fromString(String received, boolean askUserInOutPa
if (askUserInOutPath) {
toAsk.add(data);
}
//Select only if path is not child of a root path
if (defaultPaths.selectByDefault(data)) {
selection.add(data);

if (!defaultPaths.forceDeselect(data)) { // It's directly a project source folder (don't add it).
// If it's site-package, in python home or not under a source folder add it.
if (data.contains("site-packages")
|| (DefaultPathsForInterpreterInfo
.isChildOfRootPath(data, pythonHomePaths))
|| defaultPaths.selectByDefault(data)) {
selection.add(data);
}
}

}

} else {
Expand Down

0 comments on commit 066ffff

Please sign in to comment.