private @NotNull PythonScriptExecution prepareDebuggerScriptExecution(@NotNull Project project,
                                                                        @NotNull Function<TargetEnvironment, HostPort> serverPortOnTarget,
                                                                        @NotNull PythonCommandLineState pyState,
                                                                        @NotNull PythonExecution originalPythonScript,
                                                                        @Nullable RunProfile runProfile,
                                                                        @NotNull HelpersAwareTargetEnvironmentRequest request) {
    PythonScriptExecution debuggerScript = PythonScripts.prepareHelperScriptExecution(PythonHelper.DEBUGGER, request);

    TargetEnvironmentRequest targetEnvironmentRequest = request.getTargetEnvironmentRequest();
    PythonScripts.extendEnvs(debuggerScript, originalPythonScript.getEnvs(), targetEnvironmentRequest.getTargetPlatform());

    debuggerScript.setWorkingDir(originalPythonScript.getWorkingDir());

    originalPythonScript.accept(new PythonExecution.Visitor() {
      @Override
      public void visit(@NotNull PythonScriptExecution pythonScriptExecution) {
        // do nothing
      }

      @Override
      public void visit(@NotNull PythonModuleExecution pythonModuleExecution) {
        // add module flag only after command line parameters
        debuggerScript.addParameter(MODULE_PARAM);
      }
    });

    configureDebugParameters(project, pyState, debuggerScript, false);

    // TODO [Targets API] This workaround is required until Cython extensions are uploaded using Targets API
    boolean isLocalTarget = targetEnvironmentRequest instanceof LocalTargetEnvironmentRequest;
    configureDebugEnvironment(project, new TargetEnvironmentController(debuggerScript.getEnvs(), request), runProfile,
                              isLocalTarget);

    configureClientModeDebugConnectionParameters(debuggerScript, serverPortOnTarget);

    originalPythonScript.accept(new PythonExecution.Visitor() {
      @Override
      public void visit(@NotNull PythonScriptExecution pythonScriptExecution) {
        Function<TargetEnvironment, String> scriptPath = pythonScriptExecution.getPythonScriptPath();
        if (scriptPath != null) {
          debuggerScript.addParameter(scriptPath);
        }
        else {
          throw new IllegalArgumentException("Python script path must be set");
        }
      }

      @Override
      public void visit(@NotNull PythonModuleExecution pythonModuleExecution) {
        String moduleName = pythonModuleExecution.getModuleName();
        if (moduleName != null) {
          debuggerScript.addParameter(moduleName);
        }
        else {
          throw new IllegalArgumentException("Python module name must be set");
        }
      }
    });

    debuggerScript.getParameters().addAll(originalPythonScript.getParameters());

    return debuggerScript;
  }
private @NotNull PythonScriptExecution prepareDebuggerScriptExecution(@NotNull Project project,
                                                                        @NotNull Function<TargetEnvironment, HostPort> serverPortOnTarget,
                                                                        @NotNull PythonCommandLineState pyState,
                                                                        @NotNull PythonExecution originalPythonScript,
                                                                        @Nullable RunProfile runProfile,
                                                                        @NotNull HelpersAwareTargetEnvironmentRequest request)