private @NotNull ConsoleProcessCreationResult createProcessUsingTargetsAPI(@NotNull Sdk sdk) throws ExecutionException {
    int ideServerPort;
    try {
      ideServerPort = NetUtils.findAvailableSocketPort();
    }
    catch (IOException e) {
      throw new ExecutionException(e);
    }

    HelpersAwareTargetEnvironmentRequest helpersAwareRequest = PythonCommandLineState.getPythonTargetInterpreter(myProject, sdk);
    TargetEnvironmentRequest targetEnvironmentRequest = helpersAwareRequest.getTargetEnvironmentRequest();
    TargetEnvironment.LocalPortBinding ideServerPortBinding = new TargetEnvironment.LocalPortBinding(ideServerPort, null);
    targetEnvironmentRequest.getLocalPortBindings().add(ideServerPortBinding);
    Function<TargetEnvironment, HostPort> ideServerHostPortOnTarget = targetEnvironment -> {
      ResolvedPortBinding resolvedPortBinding = targetEnvironment.getLocalPortBindings().get(ideServerPortBinding);
      if (resolvedPortBinding == null) {
        throw new IllegalStateException(MessageFormat.format("Local port binding \"{0}\" must be registered", ideServerPortBinding));
      }
      return resolvedPortBinding.getTargetEndpoint();
    };

    addProjectAndModuleToRequest(targetEnvironmentRequest);

    PythonConsoleRunParams runParams = createConsoleRunParams(myWorkingDir, sdk, myEnvironmentVariables);
    PythonExecution pythonConsoleExecution = createPythonConsoleExecution(ideServerHostPortOnTarget, runParams, helpersAwareRequest);
    pythonConsoleExecution.setCharset(CONSOLE_CHARSET);
    pythonConsoleExecution.getEnvs().put(PythonEnvUtil.PYTHONIOENCODING, environment -> CONSOLE_CHARSET.name());
    List<String> interpreterOptions;
    if (!StringUtil.isEmptyOrSpaces(runParams.getInterpreterOptions())) {
      interpreterOptions = ParametersListUtil.parse(runParams.getInterpreterOptions());
    }
    else {
      interpreterOptions = Collections.emptyList();
    }

    // extra debug options are defined at least for IronPython
    PythonSdkFlavor flavor = PythonSdkFlavor.getFlavor(sdk);
    if (flavor != null) {
      interpreterOptions.addAll(flavor.getExtraDebugOptions());
    }

    // TODO [Targets API] We should pass the proper progress indicator here
    TargetEnvironment targetEnvironment = targetEnvironmentRequest.prepareEnvironment(TargetProgressIndicator.EMPTY);

    myResolvedStatementsToExecute = myStatementsToExecuteFunction.apply(targetEnvironment);

    // TODO [Targets API] [regression] We should create PTY process when `PtyCommandLine.isEnabled()`
    //  (see the legacy method `doCreateConsoleCmdLine()`)

    // TODO [Targets API] Path mappings of SDK's remote additional data is not applied here
    boolean usePty = PtyCommandLine.isEnabled() && !SystemInfo.isWindows;
    TargetedCommandLine targetedCommandLine = PythonScripts.buildTargetedCommandLine(pythonConsoleExecution,
                                                                                     targetEnvironment,
                                                                                     sdk,
                                                                                     interpreterOptions
    );

    // The environment is now prepared and ide server port should be resolved, so let's start the server
    ResolvedPortBinding resolvedServerPortBinding = targetEnvironment.getLocalPortBindings().get(ideServerPortBinding);
    String ideServerHost;
    if (resolvedServerPortBinding != null) {
      ideServerHost = resolvedServerPortBinding.getLocalEndpoint().getHost();
    }
    else {
      LOG.error("The resolution of the local port binding for \"" + ideServerPort + "\" port cannot be found in the prepared environment" +
                ", falling back to \"localhost\" for the server socket binding on the local machine");
      ideServerHost = "localhost";
    }
    PydevConsoleCommunicationServer communicationServer = new PydevConsoleCommunicationServer(myProject, ideServerHost, ideServerPort);
    myPydevConsoleCommunication = communicationServer;
    try {
      communicationServer.serve();
    }
    catch (Exception e) {
      communicationServer.close();
      throw new ExecutionException(e.getMessage(), e);
    }
    Process process = targetEnvironment.createProcess(targetedCommandLine, new EmptyProgressIndicator());
    communicationServer.setPythonConsoleProcess(process);
    String commandLineString = StringUtil.join(targetedCommandLine.getCommandPresentation(targetEnvironment), " ");
    PyRemotePathMapper pathMapper =
      PydevConsoleRunnerUtil.createTargetEnvironmentPathMapper(myProject, sdk, myConsoleSettings, targetEnvironment);
    boolean isMostlySilentProcess = true;
    ProcessHandler processHandler = PyCustomProcessHandlerProvider.createProcessHandler(process, targetEnvironment, commandLineString,
                                                                                        targetedCommandLine.getCharset(), pathMapper,
                                                                                        isMostlySilentProcess);
    return new TargetProcessHandlerFactory(processHandler, process, commandLineString, communicationServer, targetEnvironment);
  }
private @NotNull ConsoleProcessCreationResult createProcessUsingTargetsAPI(@NotNull Sdk sdk)