Repository: intellij-rust/intellij-rust
Issue: Exception after project opening while Cargo toolwindow initialization

<!--
Hello and thank you for the issue!
If you would like to report a bug, we have added some points below that you can fill out.
Feel free to remove all the irrelevant text to request a new feature.
-->

## Environment

* **IntelliJ Rust plugin version:** 0.2.119.SNAPSHOT-193-dev
* **Rust toolchain version:** 1.31.0 (abe02cefd 2018-12-04) x86_64-apple-darwin
* **IDE name and version:** IntelliJ IDEA 2019.3.3 Ultimate Edition (IU-193.6494.35)
* **Operating system:** macOS 10.13.6

## Problem description

<details>

```
failed to init toolwindow org.rust.cargo.project.toolwindow.CargoToolWindowFactory

java.lang.IllegalArgumentException: window with id="Cargo" is already registered
	at com.intellij.openapi.wm.impl.ToolWindowManagerImpl.registerToolWindow(ToolWindowManagerImpl.java:1091)
	at com.intellij.openapi.wm.impl.ToolWindowManagerImpl.initToolWindow(ToolWindowManagerImpl.java:474)
	at com.intellij.openapi.wm.impl.ToolWindowManagerImpl$6.run(ToolWindowManagerImpl.java:453)
	at com.intellij.openapi.wm.impl.CommandProcessor.runNext(CommandProcessor.java:78)
	at com.intellij.openapi.wm.impl.CommandProcessor.run(CommandProcessor.java:59)
	at com.intellij.openapi.wm.impl.commands.FinalizableCommand.finish(FinalizableCommand.java:36)
	at com.intellij.openapi.wm.impl.commands.ApplyWindowInfoCmd.run(ApplyWindowInfoCmd.java:50)
	at com.intellij.openapi.wm.impl.CommandProcessor.runNext(CommandProcessor.java:78)
	at com.intellij.openapi.wm.impl.CommandProcessor.run(CommandProcessor.java:59)
	at com.intellij.openapi.wm.impl.commands.FinalizableCommand.finish(FinalizableCommand.java:36)
	at com.intellij.openapi.wm.impl.ToolWindowsPane$AddDockedComponentCmd.run(ToolWindowsPane.java:750)
	at com.intellij.openapi.wm.impl.CommandProcessor.runNext(CommandProcessor.java:78)
	at com.intellij.openapi.wm.impl.CommandProcessor.run(CommandProcessor.java:59)
	at com.intellij.openapi.wm.impl.commands.FinalizableCommand.finish(FinalizableCommand.java:36)
	at com.intellij.openapi.wm.impl.ToolWindowsPane$AddToolStripeButtonCmd.run(ToolWindowsPane.java:990)
	at com.intellij.openapi.wm.impl.CommandProcessor.runNext(CommandProcessor.java:78)
	at com.intellij.openapi.wm.impl.CommandProcessor.run(CommandProcessor.java:59)
	at com.intellij.openapi.wm.impl.commands.FinalizableCommand.finish(FinalizableCommand.java:36)
	at com.intellij.openapi.wm.impl.ToolWindowsPane$AddToolStripeButtonCmd.run(ToolWindowsPane.java:990)
	at com.intellij.openapi.wm.impl.CommandProcessor.runNext(CommandProcessor.java:78)
	at com.intellij.openapi.wm.impl.CommandProcessor.run(CommandProcessor.java:59)
	at com.intellij.openapi.wm.impl.commands.FinalizableCommand.finish(FinalizableCommand.java:36)
	at com.intellij.openapi.wm.impl.ToolWindowsPane$AddToolStripeButtonCmd.run(ToolWindowsPane.java:990)
	at com.intellij.openapi.wm.impl.CommandProcessor.runNext(CommandProcessor.java:78)
	at com.intellij.openapi.wm.impl.CommandProcessor.run(CommandProcessor.java:59)
	at com.intellij.openapi.wm.impl.CommandProcessor.flush(CommandProcessor.java:32)
	at com.intellij.openapi.wm.impl.ToolWindowManagerImpl.flushCommands(ToolWindowManagerImpl.java:585)
	at com.intellij.openapi.wm.impl.ToolWindowManagerImpl.access$3100(ToolWindowManagerImpl.java:74)
	at com.intellij.openapi.wm.impl.ToolWindowManagerImpl$InitToolWindowsActivity.lambda$runActivity$0(ToolWindowManagerImpl.java:2289)
	at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:776)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:727)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:746)
	at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:908)
	at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:781)
	at com.intellij.ide.IdeEventQueue.lambda$dispatchEvent$8(IdeEventQueue.java:424)
	at com.intellij.openapi.progress.impl.CoreProgressManager.computePrioritized(CoreProgressManager.java:687)
	at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:423)
	at com.intellij.ide.IdeEventQueue.pumpEventsForHierarchy(IdeEventQueue.java:965)
	at com.intellij.openapi.progress.util.ProgressWindow.startBlocking(ProgressWindow.java:186)
	at com.intellij.openapi.progress.util.ProgressWindow.startBlocking(ProgressWindow.java:172)
	at com.intellij.openapi.application.impl.ApplicationImpl.runProcessWithProgressSynchronously(ApplicationImpl.java:455)
	at com.intellij.openapi.progress.impl.CoreProgressManager.runProcessWithProgressSynchronously(CoreProgressManager.java:453)
	at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcessWithProgressSynchronously(ProgressManagerImpl.java:82)
	at com.intellij.openapi.progress.impl.CoreProgressManager.runProcessWithProgressSynchronously(CoreProgressManager.java:250)
	at com.intellij.platform.ProjectUiFrameAllocator$run$1.run(ProjectFrameAllocator.kt:64)
	at com.intellij.openapi.application.TransactionGuardImpl.runSyncTransaction(TransactionGuardImpl.java:83)
	at com.intellij.openapi.application.TransactionGuardImpl.submitTransactionAndWait(TransactionGuardImpl.java:149)
	at com.intellij.platform.ProjectUiFrameAllocator.run(ProjectFrameAllocator.kt:62)
	at com.intellij.platform.PlatformProjectOpenProcessor.openExistingProject(PlatformProjectOpenProcessor.java:234)
	at com.intellij.ide.RecentProjectsManagerBase.openProject(RecentProjectsManagerBase.kt:281)
	at com.intellij.ide.RecentProjectsManagerBase.reopenLastProjectsOnStart(RecentProjectsManagerBase.kt:382)
	at com.intellij.idea.IdeStarter$main$4.run(ApplicationLoader.kt:535)
	at com.intellij.openapi.application.TransactionGuardImpl.runSyncTransaction(TransactionGuardImpl.java:83)
	at com.intellij.openapi.application.TransactionGuardImpl.lambda$submitTransaction$1(TransactionGuardImpl.java:107)
	at com.intellij.openapi.application.TransactionGuardImpl.submitTransaction(TransactionGuardImpl.java:116)
	at com.intellij.openapi.application.TransactionGuard.submitTransaction(TransactionGuard.java:121)
	at com.intellij.idea.IdeStarter.main(ApplicationLoader.kt:525)
	at com.intellij.idea.ApplicationLoader$startApp$8$1.run(ApplicationLoader.kt:240)
	at com.intellij.openapi.application.TransactionGuardImpl.performUserActivity(TransactionGuardImpl.java:193)
	at com.intellij.idea.ApplicationLoader$startApp$8.run(ApplicationLoader.kt:239)
	at java.base/java.util.concurrent.CompletableFuture$UniRun.tryFire(CompletableFuture.java:783)
	at java.base/java.util.concurrent.CompletableFuture$Completion.run(CompletableFuture.java:478)
	at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:776)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:727)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:746)
	at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:908)
	at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:781)
	at com.intellij.ide.IdeEventQueue.lambda$dispatchEvent$8(IdeEventQueue.java:424)
	at com.intellij.openapi.progress.impl.CoreProgressManager.computePrioritized(CoreProgressManager.java:698)
	at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:423)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
```

</details>

## Steps to reproduce
Just launch `Run` run configuration in our project

<!--
Please include as much of your codebase as needed to reproduce the error.
If the relevant files are large, please provide a link to a public repository or a [Gist](https://gist.github.com/).
-->            

Changed files:
src/main/kotlin/org/rust/cargo/project/toolwindow/CargoToolWindow.kt, src/main/kotlin/org/rust/cargo/project/model/impl/CargoProjectImpl.kt

diff --git a/src/main/kotlin/org/rust/cargo/project/model/impl/CargoProjectImpl.kt b/src/main/kotlin/org/rust/cargo/project/model/impl/CargoProjectImpl.kt
index b1711fb97..ebdf48a69 100644
--- a/src/main/kotlin/org/rust/cargo/project/model/impl/CargoProjectImpl.kt
+++ b/src/main/kotlin/org/rust/cargo/project/model/impl/CargoProjectImpl.kt
@@ -49,7 +49,7 @@ import org.rust.cargo.project.settings.RustProjectSettingsService.RustSettingsCh
 import org.rust.cargo.project.settings.RustProjectSettingsService.RustSettingsListener
 import org.rust.cargo.project.settings.rustSettings
 import org.rust.cargo.project.settings.toolchain
-import org.rust.cargo.project.toolwindow.CargoToolWindow.Companion.initializeToolWindowIfNeeded
+import org.rust.cargo.project.toolwindow.CargoToolWindow.Companion.initializeToolWindow
 import org.rust.cargo.project.workspace.CargoWorkspace
 import org.rust.cargo.project.workspace.PackageOrigin
 import org.rust.cargo.project.workspace.StandardLibrary
@@ -93,7 +93,7 @@ open class CargoProjectsServiceImpl(
 
             subscribe(CargoProjectsService.CARGO_PROJECTS_TOPIC, object : CargoProjectsService.CargoProjectsListener {
                 override fun cargoProjectsUpdated(projects: Collection<CargoProject>) {
-                    initializeToolWindowIfNeeded(project)
+                    initializeToolWindow(project)
                 }
             })
         }
diff --git a/src/main/kotlin/org/rust/cargo/project/toolwindow/CargoToolWindow.kt b/src/main/kotlin/org/rust/cargo/project/toolwindow/CargoToolWindow.kt
index ada71a5f3..5dad7bfcf 100644
--- a/src/main/kotlin/org/rust/cargo/project/toolwindow/CargoToolWindow.kt
+++ b/src/main/kotlin/org/rust/cargo/project/toolwindow/CargoToolWindow.kt
@@ -8,8 +8,8 @@ package org.rust.cargo.project.toolwindow
 import com.intellij.ide.DefaultTreeExpander
 import com.intellij.ide.TreeExpander
 import com.intellij.openapi.actionSystem.*
-import com.intellij.openapi.diagnostic.Logger
 import com.intellij.openapi.application.invokeLater
+import com.intellij.openapi.diagnostic.Logger
 import com.intellij.openapi.project.DumbAware
 import com.intellij.openapi.project.Project
 import com.intellij.openapi.ui.SimpleToolWindowPanel
@@ -41,6 +41,7 @@ class CargoToolWindowFactory : ToolWindowFactory, Condition<Project>, DumbAware
     }
 
     override fun value(project: Project): Boolean {
+        if (CargoToolWindow.isRegistered(project)) return false
         val cargoProjects = project.cargoProjects
         return cargoProjects.hasAtLeastOneValidProject || cargoProjects.suggestManifests().any()
     }
@@ -125,11 +126,9 @@ class CargoToolWindow(
 
         private val LOG: Logger = Logger.getInstance(CargoToolWindow::class.java)
 
-        fun initializeToolWindowIfNeeded(project: Project) {
+        fun initializeToolWindow(project: Project) {
             try {
                 val manager = ToolWindowManager.getInstance(project) as? ToolWindowManagerEx ?: return
-                val window = manager.getToolWindow(ID)
-                if (window != null) return
                 for (bean in ToolWindowEP.EP_NAME.extensionList) {
                     if (ID == bean.id) {
                         manager.initToolWindow(bean)
@@ -139,5 +138,10 @@ class CargoToolWindow(
                 LOG.error("Unable to initialize $ID tool window", e)
             }
         }
+
+        fun isRegistered(project: Project): Boolean {
+            val manager = ToolWindowManager.getInstance(project) ?: return false
+            return manager.getToolWindow(ID) != null
+        }
     }
 }