From 4e6e3a340358438d2cbbc1e9dac7c41fe43247ae Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Mon, 28 Nov 2016 10:54:27 +0100 Subject: [PATCH] Check for script file validity on cache changes, fixes #EA-90318, #EA-87672 --- .../KotlinScriptExternalImportsProvider.kt | 59 +++++++++++-------- .../jetbrains/kotlin/script/scriptFileUtil.kt | 7 +++ 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/script/KotlinScriptExternalImportsProvider.kt b/compiler/frontend/src/org/jetbrains/kotlin/script/KotlinScriptExternalImportsProvider.kt index e90b81c2344..2ace7dfdf14 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/script/KotlinScriptExternalImportsProvider.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/script/KotlinScriptExternalImportsProvider.kt @@ -60,7 +60,7 @@ class KotlinScriptExternalImportsProvider(val project: Project, private val scri val uncached = hashSetOf() files.forEach { file -> val path = getFilePath(file) - if (!cache.containsKey(path) && !cacheOfNulls.contains(path) && !uncached.contains(path)) { + if (isValidFile(file) && !cache.containsKey(path) && !cacheOfNulls.contains(path) && !uncached.contains(path)) { val scriptDef = scriptDefinitionProvider.findScriptDefinition(file) if (scriptDef != null) { val deps = scriptDef.getDependenciesFor(file, project, null) @@ -83,34 +83,43 @@ class KotlinScriptExternalImportsProvider(val project: Project, private val scri fun updateExternalImportsCache(files: Iterable): Iterable = cacheLock.write { files.mapNotNull { file -> val path = getFilePath(file) - val scriptDef = scriptDefinitionProvider.findScriptDefinition(file) - if (scriptDef != null) { - val oldDeps = cache[path] - val deps = scriptDef.getDependenciesFor(file, project, oldDeps) - when { - deps != null && (oldDeps == null || - !deps.classpath.isSamePathListAs(oldDeps.classpath) || !deps.sources.isSamePathListAs(oldDeps.sources)) -> { - // changed or new - log.info("[kts] updated/new cached deps for $path: ${deps.classpath.joinToString(File.pathSeparator)}") - cache.put(path, deps) - cacheOfNulls.remove(path) - file - } - deps != null -> { - // same as before - log.info("[kts] unchanged deps for $path") - null - } - else -> { - if (cache.remove(path) != null || cacheOfNulls.remove(path)) { - log.info("[kts] removed deps for $path") + if (!isValidFile(file)) { + if (cache.remove(path) != null || cacheOfNulls.remove(path)) { + log.info("[kts] removed deps for invalid file $path") + file + } // cleared + else null // unknown + } + else { + val scriptDef = scriptDefinitionProvider.findScriptDefinition(file) + if (scriptDef != null) { + val oldDeps = cache[path] + val deps = scriptDef.getDependenciesFor(file, project, oldDeps) + when { + deps != null && (oldDeps == null || + !deps.classpath.isSamePathListAs(oldDeps.classpath) || !deps.sources.isSamePathListAs(oldDeps.sources)) -> { + // changed or new + log.info("[kts] updated/new cached deps for $path: ${deps.classpath.joinToString(File.pathSeparator)}") + cache.put(path, deps) + cacheOfNulls.remove(path) file - } // cleared - else null // same as before + } + deps != null -> { + // same as before + log.info("[kts] unchanged deps for $path") + null + } + else -> { + if (cache.remove(path) != null || cacheOfNulls.remove(path)) { + log.info("[kts] removed deps for $path") + file + } // cleared + else null // same as before + } } } + else null // not a script } - else null // not a script } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/script/scriptFileUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/script/scriptFileUtil.kt index b88d1f4a73a..be32c3b484a 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/script/scriptFileUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/script/scriptFileUtil.kt @@ -35,6 +35,13 @@ fun getFilePath(file: TF): String = when (file) { else -> throw IllegalArgumentException("Unsupported file type $file") } +fun isValidFile(file: TF): Boolean = when (file) { + is PsiFile -> file.isValid + is VirtualFile -> file.isValid + is File -> file.exists() && file.isFile + else -> throw IllegalArgumentException("Unsupported file type $file") +} + fun getFile(file: TF): File? = when (file) { is PsiFile -> file.originalFile.run { File(virtualFile?.path) } is VirtualFile -> File(file.path)