Repository: intellij-rust/intellij-rust
Issue: Running build throws exception

## Environment

* **Intellij-Rust plugin version:** nightly last night
* **Rust toolchain version:** i believe it's unrelated
* **IDE name and version:** CL-192.5438.15
* **Operating system:** Windows 10 1809

## Problem description

```
java.lang.Throwable: Runner Run is not registered
	at com.intellij.openapi.diagnostic.Logger.error(Logger.java:145)
	at com.intellij.execution.ui.RunContentManagerImpl.getContentManagerForRunner(RunContentManagerImpl.java:478)
	at com.intellij.execution.ui.RunContentManagerImpl.getReuseContent(RunContentManagerImpl.java:369)
	at com.intellij.execution.impl.ExecutionManagerKtImpl.startRunProfile(ExecutionManagerKtImpl.kt:34)
	at com.intellij.execution.runners.GenericProgramRunnerKt.startRunProfile(GenericProgramRunner.kt:50)
	at com.intellij.execution.runners.GenericProgramRunner.execute(GenericProgramRunner.kt:17)
	at com.intellij.execution.runners.BaseProgramRunner.execute(BaseProgramRunner.java:46)
	at com.intellij.execution.runners.BaseProgramRunner.execute(BaseProgramRunner.java:35)
	at com.intellij.execution.ProgramRunnerUtil.executeConfigurationAsync(ProgramRunnerUtil.java:92)
	at com.intellij.execution.ProgramRunnerUtil.executeConfiguration(ProgramRunnerUtil.java:40)
	at com.intellij.execution.ProgramRunnerUtil.executeConfiguration(ProgramRunnerUtil.java:170)
	at org.rust.cargo.runconfig.UtilsKt.buildProject(Utils.kt:63)
	at org.rust.cargo.runconfig.RsProjectTasksRunner.run(RsProjectTasksRunner.kt:16)
	at com.intellij.task.impl.ProjectTaskManagerImpl.lambda$run$7(ProjectTaskManagerImpl.java:189)
	at com.intellij.openapi.application.impl.ApplicationImpl$1.run(ApplicationImpl.java:294)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)
```

Running a build does not actually run a build but raises an exception. Re-run works.

Changed files:
src/main/kotlin/org/rust/cargo/toolchain/CargoCommandLine.kt, src/main/kotlin/org/rust/cargo/runconfig/Utils.kt

diff --git a/src/main/kotlin/org/rust/cargo/runconfig/Utils.kt b/src/main/kotlin/org/rust/cargo/runconfig/Utils.kt
index dd303677d..c375f77f9 100644
--- a/src/main/kotlin/org/rust/cargo/runconfig/Utils.kt
+++ b/src/main/kotlin/org/rust/cargo/runconfig/Utils.kt
@@ -5,13 +5,12 @@
 
 package org.rust.cargo.runconfig
 
-import com.intellij.execution.ExecutorRegistry
-import com.intellij.execution.ProgramRunnerUtil
 import com.intellij.execution.RunManager
 import com.intellij.execution.RunnerAndConfigurationSettings
-import com.intellij.execution.executors.DefaultRunExecutor
+import com.intellij.execution.impl.ExecutionManagerImpl
 import com.intellij.openapi.actionSystem.CommonDataKeys
 import com.intellij.openapi.actionSystem.DataContext
+import com.intellij.openapi.application.ApplicationManager
 import com.intellij.openapi.project.Project
 import org.rust.cargo.project.model.CargoProject
 import org.rust.cargo.project.model.cargoProjects
@@ -20,6 +19,7 @@ import org.rust.cargo.project.toolwindow.CargoToolWindow
 import org.rust.cargo.runconfig.command.CargoCommandConfiguration
 import org.rust.cargo.runconfig.command.CargoCommandConfigurationType
 import org.rust.cargo.toolchain.CargoCommandLine
+import org.rust.cargo.toolchain.run
 import org.rust.stdext.buildList
 
 fun CargoCommandLine.mergeWithDefault(default: CargoCommandConfiguration): CargoCommandLine =
@@ -55,12 +55,13 @@ fun Project.buildProject() {
         if (settings.useOffline) add("-Zoffline")
     }
 
+    // Initialize run content manager
+    ApplicationManager.getApplication().invokeAndWait {
+        ExecutionManagerImpl.getInstance(this).contentManager
+    }
+
     for (cargoProject in cargoProjects.allProjects) {
-        val cmd = CargoCommandLine.forProject(cargoProject, "build", arguments)
-        val runnerAndConfigurationSettings = RunManager.getInstance(this)
-            .createCargoCommandRunConfiguration(cmd)
-        val executor = ExecutorRegistry.getInstance().getExecutorById(DefaultRunExecutor.EXECUTOR_ID)
-        ProgramRunnerUtil.executeConfiguration(runnerAndConfigurationSettings, executor)
+        CargoCommandLine.forProject(cargoProject, "build", arguments).run(cargoProject, saveConfiguration = false)
     }
 }
 
diff --git a/src/main/kotlin/org/rust/cargo/toolchain/CargoCommandLine.kt b/src/main/kotlin/org/rust/cargo/toolchain/CargoCommandLine.kt
index 8202a512f..96aad2739 100644
--- a/src/main/kotlin/org/rust/cargo/toolchain/CargoCommandLine.kt
+++ b/src/main/kotlin/org/rust/cargo/toolchain/CargoCommandLine.kt
@@ -5,7 +5,6 @@
 
 package org.rust.cargo.toolchain
 
-import com.intellij.execution.ExecutorRegistry
 import com.intellij.execution.ProgramRunnerUtil
 import com.intellij.execution.RunManagerEx
 import com.intellij.execution.RunnerAndConfigurationSettings
@@ -132,26 +131,33 @@ fun CargoWorkspace.Target.launchCommand(): String? {
     }
 }
 
-fun CargoCommandLine.run(cargoProject: CargoProject, presentableName: String = command) {
+fun CargoCommandLine.run(
+    cargoProject: CargoProject,
+    presentableName: String = command,
+    saveConfiguration: Boolean = true
+) {
     val project = cargoProject.project
-    val runConfiguration =
-        if (project.cargoProjects.allProjects.size > 1)
-            createRunConfiguration(project, this, name = presentableName + " [" + cargoProject.presentableName + "]")
-        else
-            createRunConfiguration(project, this, name = presentableName)
-    val executor = ExecutorRegistry.getInstance().getExecutorById(DefaultRunExecutor.EXECUTOR_ID)
-    ProgramRunnerUtil.executeConfiguration(runConfiguration, executor)
+    val name = if (project.cargoProjects.allProjects.size > 1) {
+        "$presentableName [${cargoProject.presentableName}]"
+    } else {
+        presentableName
+    }
+    val configuration = createRunConfiguration(project, this, name, saveConfiguration)
+    val executor = DefaultRunExecutor.getRunExecutorInstance()
+    ProgramRunnerUtil.executeConfiguration(configuration, executor)
 }
 
 private fun createRunConfiguration(
     project: Project,
     cargoCommandLine: CargoCommandLine,
-    name: String? = null
+    name: String? = null,
+    saveConfiguration: Boolean
 ): RunnerAndConfigurationSettings {
     val runManager = RunManagerEx.getInstanceEx(project)
-
     return runManager.createCargoCommandRunConfiguration(cargoCommandLine, name).apply {
-        runManager.setTemporaryConfiguration(this)
+        if (saveConfiguration) {
+            runManager.setTemporaryConfiguration(this)
+        }
     }
 }