The correct way to ignore libraries, even if virtual env is situated inside project directory (PY-16899)

This commit is contained in:
Elizaveta Shashkova
2016-03-21 19:25:13 +03:00
parent 47d9f5f07e
commit 755871e3f3
2 changed files with 39 additions and 3 deletions
@@ -132,6 +132,18 @@ def _get_project_roots(project_roots_cache=[]):
return project_roots_cache[-1] # returns the project roots with case normalized
def _get_library_roots(library_roots_cache=[]):
# Note: the project_roots_cache is the same instance among the many calls to the method
if not library_roots_cache:
roots = os.getenv('LIBRARY_ROOTS', '').split(os.pathsep)
pydev_log.debug("LIBRARY_ROOTS %s\n" % roots)
new_roots = []
for root in roots:
new_roots.append(os.path.normcase(root))
library_roots_cache.append(new_roots)
return library_roots_cache[-1] # returns the project roots with case normalized
def not_in_project_roots(filename, filename_to_not_in_scope_cache={}):
# Note: the filename_to_not_in_scope_cache is the same instance among the many calls to the method
try:
@@ -146,6 +158,13 @@ def not_in_project_roots(filename, filename_to_not_in_scope_cache={}):
else: # for else (only called if the break wasn't reached).
filename_to_not_in_scope_cache[filename] = True
if not filename_to_not_in_scope_cache[filename]:
# additional check if interpreter is situated in a project directory
library_roots = _get_library_roots()
for root in library_roots:
if filename.startswith(root):
filename_to_not_in_scope_cache[filename] = True
# at this point it must be loaded.
return filename_to_not_in_scope_cache[filename]
@@ -16,9 +16,7 @@
package com.jetbrains.python.debugger;
import com.google.common.collect.Lists;
import com.intellij.execution.ExecutionException;
import com.intellij.execution.ExecutionResult;
import com.intellij.execution.Executor;
import com.intellij.execution.*;
import com.intellij.execution.configurations.*;
import com.intellij.execution.console.LanguageConsoleBuilder;
import com.intellij.execution.executors.DefaultDebugExecutor;
@@ -30,6 +28,7 @@ import com.intellij.execution.ui.RunContentDescriptor;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
@@ -68,6 +67,7 @@ public class PyDebugRunner extends GenericProgramRunner {
public static final String FILE_PARAM = "--file";
public static final String MODULE_PARAM = "--module";
public static final String IDE_PROJECT_ROOTS = "IDE_PROJECT_ROOTS";
public static final String LIBRARY_ROOTS = "LIBRARY_ROOTS";
public static final String PYTHON_ASYNCIO_DEBUG = "PYTHONASYNCIODEBUG";
@SuppressWarnings("SpellCheckingInspection")
public static final String GEVENT_SUPPORT = "GEVENT_SUPPORT";
@@ -301,6 +301,7 @@ public class PyDebugRunner extends GenericProgramRunner {
}
addProjectRootsToEnv(project, cmd);
addSdkRootsToEnv(project, cmd);
final String[] debuggerArgs = new String[]{
CLIENT_PARAM, "127.0.0.1",
@@ -347,4 +348,20 @@ public class PyDebugRunner extends GenericProgramRunner {
commandLine.getEnvironment().put(IDE_PROJECT_ROOTS, StringUtil.join(roots, File.pathSeparator));
}
private static void addSdkRootsToEnv(@NotNull Project project, @NotNull GeneralCommandLine commandLine) {
final RunManager runManager = RunManager.getInstance(project);
final RunnerAndConfigurationSettings selectedConfiguration = runManager.getSelectedConfiguration();
if (selectedConfiguration != null) {
final RunConfiguration configuration = selectedConfiguration.getConfiguration();
if (configuration instanceof AbstractPythonRunConfiguration) {
AbstractPythonRunConfiguration runConfiguration = (AbstractPythonRunConfiguration)configuration;
List<String> roots = Lists.newArrayList();
for (VirtualFile contentRoot : runConfiguration.getSdk().getSdkModificator().getRoots(OrderRootType.CLASSES)) {
roots.add(contentRoot.getPath());
}
commandLine.getEnvironment().put(LIBRARY_ROOTS, StringUtil.join(roots, File.pathSeparator));
}
}
}
}