From 9f9596a1e528d0fa0b51c2b050f84d4e258e4fec Mon Sep 17 00:00:00 2001 From: Alexander Koshevoy Date: Mon, 22 Apr 2019 11:03:04 +0300 Subject: [PATCH] IDEA-CR-46638: PY-34574 Add content and source roots of module and its dependencies to PYTHONPATH for managed servers (cherry picked from commit 929b60044214484c79ed2cd32e6ea7c1a9220b73) GitOrigin-RevId: 52d13161ed691e4d8b1eeff4b935b2744bc9c35a --- .../python/run/PythonCommandLineState.java | 47 ++++++++++++++----- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/python/src/com/jetbrains/python/run/PythonCommandLineState.java b/python/src/com/jetbrains/python/run/PythonCommandLineState.java index 48b69c74086a..838abf4d7a92 100644 --- a/python/src/com/jetbrains/python/run/PythonCommandLineState.java +++ b/python/src/com/jetbrains/python/run/PythonCommandLineState.java @@ -219,7 +219,8 @@ public abstract class PythonCommandLineState extends CommandLineState { ProcessHandler processHandler = processStarter.start(myConfig, commandLine); // attach extensions - PythonRunConfigurationExtensionsManager.Companion.getInstance().attachExtensionsToProcess(myConfig, processHandler, getRunnerSettings()); + PythonRunConfigurationExtensionsManager.Companion.getInstance() + .attachExtensionsToProcess(myConfig, processHandler, getRunnerSettings()); return processHandler; } @@ -400,19 +401,34 @@ public abstract class PythonCommandLineState extends CommandLineState { PythonSdkFlavor.setupEncodingEnvs(envs, charset); } - private static void buildPythonPath(Project project, GeneralCommandLine commandLine, PythonRunParams config, boolean isDebug) { - Sdk pythonSdk = PythonSdkType.findSdkByPath(config.getSdkHome()); + public static void buildPythonPath(@NotNull Project project, + @NotNull GeneralCommandLine commandLine, + @NotNull PythonRunParams config, + boolean isDebug) { + Module module = getModule(project, config); + buildPythonPath(module, commandLine, config.getSdkHome(), config.isPassParentEnvs(), config.shouldAddContentRoots(), + config.shouldAddSourceRoots(), isDebug); + } + + public static void buildPythonPath(@Nullable Module module, + @NotNull GeneralCommandLine commandLine, + @Nullable String sdkHome, + boolean passParentEnvs, + boolean shouldAddContentRoots, + boolean shouldAddSourceRoots, + boolean isDebug) { + Sdk pythonSdk = PythonSdkType.findSdkByPath(sdkHome); if (pythonSdk != null) { List pathList = Lists.newArrayList(); pathList.addAll(getAddedPaths(pythonSdk)); - pathList.addAll(collectPythonPath(project, config, isDebug)); - initPythonPath(commandLine, config.isPassParentEnvs(), pathList, config.getSdkHome()); + pathList.addAll(collectPythonPath(module, sdkHome, shouldAddContentRoots, shouldAddSourceRoots, isDebug)); + initPythonPath(commandLine, passParentEnvs, pathList, sdkHome); } } - public static void initPythonPath(GeneralCommandLine commandLine, + public static void initPythonPath(@NotNull GeneralCommandLine commandLine, boolean passParentEnvs, - List pathList, + @NotNull List pathList, final String interpreterPath) { final PythonSdkFlavor flavor = PythonSdkFlavor.getFlavor(interpreterPath); if (flavor != null) { @@ -423,7 +439,8 @@ public abstract class PythonCommandLineState extends CommandLineState { } } - public static List getAddedPaths(Sdk pythonSdk) { + @NotNull + public static List getAddedPaths(@NotNull Sdk pythonSdk) { List pathList = new ArrayList<>(); final SdkAdditionalData sdkAdditionalData = pythonSdk.getSdkAdditionalData(); if (sdkAdditionalData instanceof PythonSdkAdditionalData) { @@ -462,10 +479,18 @@ public abstract class PythonCommandLineState extends CommandLineState { @VisibleForTesting public static Collection collectPythonPath(Project project, PythonRunParams config, boolean isDebug) { final Module module = getModule(project, config); - final HashSet pythonPath = - Sets.newLinkedHashSet(collectPythonPath(module, config.shouldAddContentRoots(), config.shouldAddSourceRoots())); + return collectPythonPath(module, config.getSdkHome(), config.shouldAddContentRoots(), config.shouldAddSourceRoots(), isDebug); + } - if (isDebug && PythonSdkFlavor.getFlavor(config.getSdkHome()) instanceof JythonSdkFlavor) { + @NotNull + public static Collection collectPythonPath(@Nullable Module module, + @Nullable String sdkHome, + boolean shouldAddContentRoots, + boolean shouldAddSourceRoots, + boolean isDebug) { + final HashSet pythonPath = Sets.newLinkedHashSet(collectPythonPath(module, shouldAddContentRoots, shouldAddSourceRoots)); + + if (isDebug && PythonSdkFlavor.getFlavor(sdkHome) instanceof JythonSdkFlavor) { //that fixes Jython problem changing sys.argv on execfile, see PY-8164 pythonPath.add(PythonHelpersLocator.getHelperPath("pycharm")); pythonPath.add(PythonHelpersLocator.getHelperPath("pydev"));