diff --git a/platform/platform-impl/src/com/intellij/execution/wsl/ijent/nio/toggle/IjentInProjectStarter.kt b/platform/platform-impl/src/com/intellij/execution/wsl/ijent/nio/toggle/IjentInProjectStarter.kt index 13656a39b311..7003d55d1202 100644 --- a/platform/platform-impl/src/com/intellij/execution/wsl/ijent/nio/toggle/IjentInProjectStarter.kt +++ b/platform/platform-impl/src/com/intellij/execution/wsl/ijent/nio/toggle/IjentInProjectStarter.kt @@ -17,11 +17,15 @@ import kotlinx.coroutines.launch /** Starts the IJent if a project on WSL is opened. */ internal class IjentInProjectStarter : ProjectActivity { override suspend fun execute(project: Project): Unit = coroutineScope { - val service = serviceAsync() - if (!service.isInitialized) { + val ijentWslNioFsToggler = IjentWslNioFsToggler.instanceAsync() + if (!ijentWslNioFsToggler.isAvailable) { return@coroutineScope } + launch { + ijentWslNioFsToggler.enableForAllWslDistributions() + } + val allWslDistributions = async(Dispatchers.IO) { serviceAsync().installedDistributions } diff --git a/platform/platform-impl/src/com/intellij/execution/wsl/ijent/nio/toggle/IjentNioFsStrategy.kt b/platform/platform-impl/src/com/intellij/execution/wsl/ijent/nio/toggle/IjentNioFsStrategy.kt index 6ea4156a414c..638fa57add89 100644 --- a/platform/platform-impl/src/com/intellij/execution/wsl/ijent/nio/toggle/IjentNioFsStrategy.kt +++ b/platform/platform-impl/src/com/intellij/execution/wsl/ijent/nio/toggle/IjentNioFsStrategy.kt @@ -11,6 +11,7 @@ import com.intellij.platform.ijent.community.impl.nio.IjentNioFileSystemProvider import com.intellij.platform.ijent.community.impl.nio.telemetry.TracingFileSystemProvider import com.intellij.util.containers.forEachGuaranteed import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.job import kotlinx.coroutines.launch import java.net.URI @@ -26,13 +27,13 @@ internal class IjentWslNioFsToggleStrategy( ) { private val ownFileSystems = OwnFileSystems(multiRoutingFileSystemProvider) - val isInitialized: Boolean = true - - fun initialize() { + init { coroutineScope.coroutineContext.job.invokeOnCompletion { ownFileSystems.unregisterAll() } + } + suspend fun enableForAllWslDistributions() { val listener = BiConsumer, Set> { old, new -> // TODO The code is race prone. Frequent creations and deletions of WSL containers may break the state. for (distro in new - old) { @@ -51,8 +52,7 @@ internal class IjentWslNioFsToggleStrategy( wslDistributionManager.removeWslDistributionsChangeListener(listener) } - // The function may be called under a read lock, so it's better to postpone the execution. - coroutineScope.launch { + coroutineScope { for (distro in wslDistributionManager.installedDistributions) { launch { handleWslDistributionAddition(distro) diff --git a/platform/platform-impl/src/com/intellij/execution/wsl/ijent/nio/toggle/IjentWslNioFsToggler.kt b/platform/platform-impl/src/com/intellij/execution/wsl/ijent/nio/toggle/IjentWslNioFsToggler.kt index 3d0c63fa07b1..686e2d089fea 100644 --- a/platform/platform-impl/src/com/intellij/execution/wsl/ijent/nio/toggle/IjentWslNioFsToggler.kt +++ b/platform/platform-impl/src/com/intellij/execution/wsl/ijent/nio/toggle/IjentWslNioFsToggler.kt @@ -36,6 +36,13 @@ class IjentWslNioFsToggler(private val coroutineScope: CoroutineScope) { } } + val isAvailable: Boolean get() = strategy != null + + suspend fun enableForAllWslDistributions() { + strategy ?: error("Not available") + strategy.enableForAllWslDistributions() + } + fun enable(distro: WSLDistribution, ijentId: IjentId) { strategy?.enable(distro, ijentId) } @@ -63,12 +70,6 @@ class IjentWslNioFsToggler(private val coroutineScope: CoroutineScope) { } } - init { - strategy?.initialize() - } - - val isInitialized: Boolean get() = strategy?.isInitialized ?: false - @TestOnly fun unregisterAll() { coroutineScope.cancel() diff --git a/platform/platform-impl/src/com/intellij/execution/wsl/ijent/nio/toggle/IjentWslNioFsVmOptionsSetter.kt b/platform/platform-impl/src/com/intellij/execution/wsl/ijent/nio/toggle/IjentWslNioFsVmOptionsSetter.kt index 17240d4f49f0..d2bea3272aa1 100644 --- a/platform/platform-impl/src/com/intellij/execution/wsl/ijent/nio/toggle/IjentWslNioFsVmOptionsSetter.kt +++ b/platform/platform-impl/src/com/intellij/execution/wsl/ijent/nio/toggle/IjentWslNioFsVmOptionsSetter.kt @@ -140,7 +140,7 @@ object IjentWslNioFsVmOptionsSetter { val changedOptions = ensureInVmOptions() when { changedOptions.isEmpty() -> { - IjentWslNioFsToggler.instanceAsync() // Implicitly activates IJent FS for all WSL distributions. + IjentWslNioFsToggler.instanceAsync().enableForAllWslDistributions() } PluginManagerCore.isRunningFromSources() || AppMode.isDevServer() ->