From 755871e3f3f3bd0dc922d97645822dab71810003 Mon Sep 17 00:00:00 2001 From: Elizaveta Shashkova Date: Mon, 21 Mar 2016 19:21:56 +0300 Subject: [PATCH] The correct way to ignore libraries, even if virtual env is situated inside project directory (PY-16899) --- .../pydev/_pydevd_bundle/pydevd_utils.py | 19 +++++++++++++++ .../python/debugger/PyDebugRunner.java | 23 ++++++++++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/python/helpers/pydev/_pydevd_bundle/pydevd_utils.py b/python/helpers/pydev/_pydevd_bundle/pydevd_utils.py index 4fe32e1cb496..ef5c96f312a7 100644 --- a/python/helpers/pydev/_pydevd_bundle/pydevd_utils.py +++ b/python/helpers/pydev/_pydevd_bundle/pydevd_utils.py @@ -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] diff --git a/python/src/com/jetbrains/python/debugger/PyDebugRunner.java b/python/src/com/jetbrains/python/debugger/PyDebugRunner.java index fb2270d17bd2..a25b47238dc7 100644 --- a/python/src/com/jetbrains/python/debugger/PyDebugRunner.java +++ b/python/src/com/jetbrains/python/debugger/PyDebugRunner.java @@ -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 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)); + } + } + } }