Repository: intellij-rust/intellij-rust
Issue: java.lang.Throwable: Please fix org.rust.lang.core.macros.RsIndexableSetContributor#getAdditionalProjectRootsToIndex(Project)

## Environment

* **IntelliJ Rust plugin version:** 0.2.107.SNAPSHOT-192-dev
* **Rust toolchain version:** 1.37.0 (eae3437df 2019-08-13) x86_64-apple-darwin
* **IDE name and version:** CLion 2019.2.2 (CL-192.6603.37)
* **Operating system:** macOS 10.14.6

## Problem description

"Inspect Code" action triggers an exception:

```
java.lang.Throwable: Please fix org.rust.lang.core.macros.RsIndexableSetContributor#getAdditionalProjectRootsToIndex(Project).
Invalid file returned: file:///Users/jetbrains/git/intellij-rust/plugin/build/clion-sandbox/system/rust_expanded_macros/fIbJAImh/expansions
	at com.intellij.openapi.diagnostic.Logger.error(Logger.java:145)
	at com.intellij.util.indexing.IndexableSetContributor.filterOutNulls(IndexableSetContributor.java:57)
	at com.intellij.util.indexing.IndexableSetContributor.getProjectRootsToIndex(IndexableSetContributor.java:26)
	at com.intellij.util.indexing.FileBasedIndexScanRunnableCollectorImpl.lambda$collectScanRootRunnables$5(FileBasedIndexScanRunnableCollectorImpl.java:56)
	at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:932)
	at com.intellij.openapi.application.ReadAction.compute(ReadAction.java:57)
	at com.intellij.util.indexing.FileBasedIndexScanRunnableCollectorImpl.collectScanRootRunnables(FileBasedIndexScanRunnableCollectorImpl.java:38)
	at com.intellij.util.indexing.FileBasedIndexImpl.collectScanRootRunnables(FileBasedIndexImpl.java:2343)
	at com.intellij.util.indexing.FileBasedIndexImpl.iterateIndexableFiles(FileBasedIndexImpl.java:2335)
	at com.intellij.util.indexing.FileBasedIndexImpl.projectIndexableFiles(FileBasedIndexImpl.java:1080)
	at com.intellij.psi.stubs.StubIndexImpl$StubIdListContainerAction.<init>(StubIndexImpl.java:586)
	at com.intellij.psi.stubs.StubIndexImpl$2.<init>(StubIndexImpl.java:282)
	at com.intellij.psi.stubs.StubIndexImpl.processElements(StubIndexImpl.java:282)
	at com.intellij.psi.stubs.StubIndex.processElements(StubIndex.java:65)
	at org.rust.lang.core.stubs.index.RsIncludeMacroIndex$Companion.getIncludingModInternal(RsIncludeMacroIndex.kt:52)
	at org.rust.lang.core.stubs.index.RsIncludeMacroIndex$Companion.access$getIncludingModInternal(RsIncludeMacroIndex.kt:25)
	at org.rust.lang.core.stubs.index.RsIncludeMacroIndex$Companion$getIncludingMod$1.compute(RsIncludeMacroIndex.kt:41)
	at com.intellij.psi.util.CachedValuesManager.lambda$getCachedValue$0(CachedValuesManager.java:148)
	at com.intellij.psi.impl.PsiCachedValueImpl.doCompute(PsiCachedValueImpl.java:54)
	at com.intellij.util.CachedValueBase.lambda$getValueWithLock$1(CachedValueBase.java:240)
	at com.intellij.openapi.util.RecursionManager$1.doPreventingRecursion(RecursionManager.java:113)
	at com.intellij.openapi.util.RecursionManager.doPreventingRecursion(RecursionManager.java:71)
	at com.intellij.util.CachedValueBase.getValueWithLock(CachedValueBase.java:241)
	at com.intellij.psi.impl.PsiCachedValueImpl.getValue(PsiCachedValueImpl.java:43)
	at com.intellij.util.CachedValuesManagerImpl.getCachedValueFromExHolder(CachedValuesManagerImpl.java:72)
	at com.intellij.util.CachedValuesManagerImpl.getCachedValue(CachedValuesManagerImpl.java:45)
	at com.intellij.psi.util.CachedValuesManager.getCachedValue(CachedValuesManager.java:147)
	at com.intellij.psi.util.CachedValuesManager.getCachedValue(CachedValuesManager.java:129)
	at org.rust.lang.core.stubs.index.RsIncludeMacroIndex$Companion.getIncludingMod(RsIncludeMacroIndex.kt:39)
	at org.rust.lang.core.macros.RsExpandedElement$Companion.getContextImpl(RsExpandedElement.kt:32)
	at org.rust.lang.core.psi.ext.RsFunctionImplMixin.getContext(RsFunction.kt:160)
	at com.intellij.psi.util.PsiTreeUtil.getContextOfType(PsiTreeUtil.java:641)
	at com.intellij.psi.util.PsiTreeUtil.getContextOfType(PsiTreeUtil.java:620)
	at org.rust.lang.core.psi.ext.RsPathImplMixin.getContainingMod(RsPath.kt:112)
	at org.rust.lang.core.resolve.NameResolutionKt.processUnqualifiedPathResolveVariants(NameResolution.kt:464)
	at org.rust.lang.core.resolve.NameResolutionKt.processPathResolveVariants(NameResolution.kt:343)
	at org.rust.lang.core.resolve.ref.RsPathReferenceImplKt$resolvePath$result$1.invoke(RsPathReferenceImpl.kt:85)
	at org.rust.lang.core.resolve.ref.RsPathReferenceImplKt$resolvePath$result$1.invoke(RsPathReferenceImpl.kt)
	at org.rust.lang.core.resolve.ProcessorsKt.collectPathResolveVariants(Processors.kt:60)
	at org.rust.lang.core.resolve.ref.RsPathReferenceImplKt.resolvePath(RsPathReferenceImpl.kt:84)
	at org.rust.lang.core.resolve.ref.RsPathReferenceImplKt.resolvePath$default(RsPathReferenceImpl.kt:83)
	at org.rust.lang.core.resolve.ref.RsPathReferenceImpl$Resolver.invoke(RsPathReferenceImpl.kt:72)
	at org.rust.lang.core.resolve.ref.RsPathReferenceImpl$Resolver.invoke(RsPathReferenceImpl.kt:70)
	at org.rust.lang.core.resolve.ref.RsResolveCache$resolveWithCaching$$inlined$run$lambda$1.compute(RsResolveCache.kt:94)
	at com.intellij.openapi.util.RecursionManager$1.doPreventingRecursion(RecursionManager.java:113)
	at org.rust.lang.core.resolve.ref.RsResolveCache.resolveWithCaching(RsResolveCache.kt:94)
	at org.rust.lang.core.resolve.ref.RsPathReferenceImpl.advancedCachedMultiResolve(RsPathReferenceImpl.kt:63)
	at org.rust.lang.core.resolve.ref.RsPathReferenceImpl.advancedMultiResolve(RsPathReferenceImpl.kt:59)
	at org.rust.lang.core.resolve.ref.RsPathReferenceImpl.multiResolve(RsPathReferenceImpl.kt:51)
	at com.intellij.psi.PsiPolyVariantReferenceBase.resolve(PsiPolyVariantReferenceBase.java:47)
	at org.rust.lang.core.resolve.ref.RsReferenceBase.resolve(RsReferenceBase.kt:28)
	at org.rust.ide.inspections.RsWrongLifetimeParametersNumberInspection$buildVisitor$1.visitBaseType(RsWrongLifetimeParametersNumberInspection.kt:25)
	at org.rust.lang.core.psi.impl.RsBaseTypeImpl.accept(RsBaseTypeImpl.java:27)
	at org.rust.lang.core.psi.impl.RsBaseTypeImpl.accept(RsBaseTypeImpl.java:31)
	at com.intellij.codeInspection.InspectionEngine.acceptElements(InspectionEngine.java:75)
	at com.intellij.codeInspection.InspectionEngine.createVisitorAndAcceptElements(InspectionEngine.java:63)
	at com.intellij.codeInsight.daemon.impl.LocalInspectionsPass.runToolOnElements(LocalInspectionsPass.java:297)
	at com.intellij.codeInsight.daemon.impl.LocalInspectionsPass.lambda$null$5(LocalInspectionsPass.java:265)
	at com.intellij.util.AstLoadingFilter.forceAllowTreeLoading(AstLoadingFilter.java:156)
	at com.intellij.util.AstLoadingFilter.forceAllowTreeLoading(AstLoadingFilter.java:148)
	at com.intellij.codeInsight.daemon.impl.LocalInspectionsPass.lambda$null$6(LocalInspectionsPass.java:262)
	at com.intellij.util.AstLoadingFilter.disallowTreeLoading(AstLoadingFilter.java:127)
	at com.intellij.util.AstLoadingFilter.disallowTreeLoading(AstLoadingFilter.java:116)
	at com.intellij.codeInsight.daemon.impl.LocalInspectionsPass.lambda$visitPriorityElementsAndInit$7(LocalInspectionsPass.java:262)
	at com.intellij.concurrency.ApplierCompleter.execAndForkSubTasks(ApplierCompleter.java:133)
	at com.intellij.openapi.application.impl.ApplicationImpl.tryRunReadAction(ApplicationImpl.java:1101)
	at com.intellij.concurrency.ApplierCompleter.lambda$wrapInReadActionAndIndicator$1(ApplierCompleter.java:105)
	at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:591)
	at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:537)
	at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:59)
	at com.intellij.concurrency.ApplierCompleter.wrapInReadActionAndIndicator(ApplierCompleter.java:116)
	at com.intellij.concurrency.ApplierCompleter.compute(ApplierCompleter.java:99)
	at java.base/java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:746)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.helpCC(ForkJoinPool.java:1115)
	at java.base/java.util.concurrent.ForkJoinPool.awaitJoin(ForkJoinPool.java:1687)
	at java.base/java.util.concurrent.ForkJoinTask.get(ForkJoinTask.java:1039)
	at com.intellij.concurrency.JobLauncherImpl.invokeConcurrentlyUnderProgress(JobLauncherImpl.java:58)
	at com.intellij.concurrency.JobLauncher.invokeConcurrentlyUnderProgress(JobLauncher.java:58)
	at com.intellij.codeInsight.daemon.impl.LocalInspectionsPass.visitPriorityElementsAndInit(LocalInspectionsPass.java:268)
	at com.intellij.codeInsight.daemon.impl.LocalInspectionsPass.inspect(LocalInspectionsPass.java:200)
	at com.intellij.codeInsight.daemon.impl.LocalInspectionsPass.doInspectInBatch(LocalInspectionsPass.java:135)
	at com.intellij.codeInspection.ex.GlobalInspectionContextImpl.inspectFile(GlobalInspectionContextImpl.java:569)
	at com.intellij.codeInspection.ex.GlobalInspectionContextImpl.lambda$null$3(GlobalInspectionContextImpl.java:450)
	at com.intellij.openapi.project.DumbService.lambda$runReadActionInSmartMode$0(DumbService.java:108)
	at com.intellij.openapi.project.DumbService.lambda$runReadActionInSmartMode$1(DumbService.java:153)
	at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:936)
	at com.intellij.openapi.application.ReadAction.compute(ReadAction.java:57)
	at com.intellij.openapi.project.DumbService.runReadActionInSmartMode(DumbService.java:146)
	at com.intellij.openapi.project.DumbService.runReadActionInSmartMode(DumbService.java:108)
	at com.intellij.openapi.project.DumbService.tryRunReadActionInSmartMode(DumbService.java:126)
	at com.intellij.codeInspection.ex.GlobalInspectionContextImpl.lambda$runTools$8(GlobalInspectionContextImpl.java:441)
	at com.intellij.concurrency.JobLauncherImpl$1MyTask.lambda$call$0(JobLauncherImpl.java:260)
	at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:591)
	at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:537)
	at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:59)
	at com.intellij.concurrency.JobLauncherImpl$1MyTask.call(JobLauncherImpl.java:247)
	at com.intellij.concurrency.JobLauncherImpl$1MyTask.call(JobLauncherImpl.java:237)
	at java.base/java.util.concurrent.ForkJoinTask$AdaptedCallable.exec(ForkJoinTask.java:1448)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
	at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:177)
```

## Steps to reproduce

1. `git clone https://gitlab.com/lancelote/rust-book.git && cd rust-book && git checkout 1169706f0e580b73cfd8c386271a74a218983fe9`
2. File | Invalidate Caches ... | Invalidate
3. Code | Inspect Code | Whole Project

Changed files:
src/main/kotlin/org/rust/lang/core/macros/RsMacroExpansionCachesInvalidator.kt, src/main/kotlin/org/rust/lang/core/macros/MacroExpansionManager.kt

diff --git a/src/main/kotlin/org/rust/lang/core/macros/MacroExpansionManager.kt b/src/main/kotlin/org/rust/lang/core/macros/MacroExpansionManager.kt
index e3401b3fe..9c97a621b 100644
--- a/src/main/kotlin/org/rust/lang/core/macros/MacroExpansionManager.kt
+++ b/src/main/kotlin/org/rust/lang/core/macros/MacroExpansionManager.kt
@@ -38,6 +38,7 @@ import com.intellij.util.concurrency.QueueProcessor.ThreadToUse
 import com.intellij.util.indexing.FileBasedIndex
 import com.intellij.util.indexing.IndexableFileSet
 import com.intellij.util.io.createDirectories
+import com.intellij.util.io.createFile
 import com.intellij.util.io.delete
 import com.intellij.util.io.exists
 import org.jetbrains.annotations.TestOnly
@@ -52,10 +53,7 @@ import org.rust.lang.core.psi.RsPsiTreeChangeEvent.*
 import org.rust.lang.core.psi.ext.*
 import org.rust.lang.core.resolve.indexes.RsMacroCallIndex
 import org.rust.openapiext.*
-import org.rust.stdext.ThreadLocalDelegate
-import org.rust.stdext.newDeflaterDataOutputStream
-import org.rust.stdext.newInflaterDataInputStream
-import org.rust.stdext.randomLowercaseAlphabetic
+import org.rust.stdext.*
 import java.nio.file.Files
 import java.nio.file.Path
 import java.nio.file.Paths
@@ -88,6 +86,11 @@ interface MacroExpansionManager {
         @JvmStatic
         fun isExpansionFile(file: VirtualFile): Boolean =
             file.fileSystem == MacroExpansionFileSystem.getInstance()
+
+        @JvmStatic
+        fun invalidateCaches() {
+            getCorruptionMarkerFile().createFile()
+        }
     }
 }
 
@@ -107,10 +110,14 @@ inline fun <T> MacroExpansionManager.withExpansionState(
 val MACRO_LOG = Logger.getInstance("org.rust.macros")
 // The path is visible in indexation progress
 const val MACRO_EXPANSION_VFS_ROOT = "rust_expanded_macros"
+private const val CORRUPTION_MARKER_NAME = "corruption.marker"
 
 fun getBaseMacroDir(): Path =
     pluginDirInSystem().resolve("macros")
 
+private fun getCorruptionMarkerFile(): Path =
+    getBaseMacroDir().resolve(CORRUPTION_MARKER_NAME)
+
 @State(name = "MacroExpansionManager", storages = [
     Storage(StoragePathMacros.WORKSPACE_FILE, roamingType = RoamingType.DISABLED),
     Storage("misc.xml", roamingType = RoamingType.DISABLED, deprecated = true)
@@ -287,6 +294,7 @@ private class MacroExpansionServiceBuilder private constructor(
     companion object {
         fun prepare(dirs: Dirs): MacroExpansionServiceBuilder {
             val dataFile = dirs.dataFile
+            checkInvalidatedStorage()
             MacroExpansionFileSystemRootsLoader.loadProjectDirs()
             val loaded = load(dataFile)
 
@@ -321,6 +329,12 @@ private class MacroExpansionServiceBuilder private constructor(
                 null
             }
         }
+
+        private fun checkInvalidatedStorage() {
+            if (getCorruptionMarkerFile().exists()) {
+                getBaseMacroDir().cleanDirectory()
+            }
+        }
     }
 }
 
diff --git a/src/main/kotlin/org/rust/lang/core/macros/RsMacroExpansionCachesInvalidator.kt b/src/main/kotlin/org/rust/lang/core/macros/RsMacroExpansionCachesInvalidator.kt
index 69003947f..281942b4c 100644
--- a/src/main/kotlin/org/rust/lang/core/macros/RsMacroExpansionCachesInvalidator.kt
+++ b/src/main/kotlin/org/rust/lang/core/macros/RsMacroExpansionCachesInvalidator.kt
@@ -6,14 +6,13 @@
 package org.rust.lang.core.macros
 
 import com.intellij.ide.caches.CachesInvalidator
-import com.intellij.util.io.delete
 
 class RsMacroExpansionCachesInvalidator : CachesInvalidator() {
     override fun invalidateCaches() {
         try {
-            getBaseMacroDir().delete()
-        } catch (ignored: Exception) {
-
+            MacroExpansionManager.invalidateCaches()
+        } catch (e: Exception) {
+            MACRO_LOG.warn(e)
         }
     }