Check for script file validity on cache changes, fixes #EA-90318, #EA-87672

This commit is contained in:
Ilya Chernikov
2016-11-28 10:54:27 +01:00
parent 0e7413b5b1
commit 4e6e3a3403
2 changed files with 41 additions and 25 deletions
@@ -60,7 +60,7 @@ class KotlinScriptExternalImportsProvider(val project: Project, private val scri
val uncached = hashSetOf<String>() val uncached = hashSetOf<String>()
files.forEach { file -> files.forEach { file ->
val path = getFilePath(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) val scriptDef = scriptDefinitionProvider.findScriptDefinition(file)
if (scriptDef != null) { if (scriptDef != null) {
val deps = scriptDef.getDependenciesFor(file, project, null) val deps = scriptDef.getDependenciesFor(file, project, null)
@@ -83,34 +83,43 @@ class KotlinScriptExternalImportsProvider(val project: Project, private val scri
fun <TF: Any> updateExternalImportsCache(files: Iterable<TF>): Iterable<TF> = cacheLock.write { fun <TF: Any> updateExternalImportsCache(files: Iterable<TF>): Iterable<TF> = cacheLock.write {
files.mapNotNull { file -> files.mapNotNull { file ->
val path = getFilePath(file) val path = getFilePath(file)
val scriptDef = scriptDefinitionProvider.findScriptDefinition(file) if (!isValidFile(file)) {
if (scriptDef != null) { if (cache.remove(path) != null || cacheOfNulls.remove(path)) {
val oldDeps = cache[path] log.info("[kts] removed deps for invalid file $path")
val deps = scriptDef.getDependenciesFor(file, project, oldDeps) file
when { } // cleared
deps != null && (oldDeps == null || else null // unknown
!deps.classpath.isSamePathListAs(oldDeps.classpath) || !deps.sources.isSamePathListAs(oldDeps.sources)) -> { }
// changed or new else {
log.info("[kts] updated/new cached deps for $path: ${deps.classpath.joinToString(File.pathSeparator)}") val scriptDef = scriptDefinitionProvider.findScriptDefinition(file)
cache.put(path, deps) if (scriptDef != null) {
cacheOfNulls.remove(path) val oldDeps = cache[path]
file val deps = scriptDef.getDependenciesFor(file, project, oldDeps)
} when {
deps != null -> { deps != null && (oldDeps == null ||
// same as before !deps.classpath.isSamePathListAs(oldDeps.classpath) || !deps.sources.isSamePathListAs(oldDeps.sources)) -> {
log.info("[kts] unchanged deps for $path") // changed or new
null log.info("[kts] updated/new cached deps for $path: ${deps.classpath.joinToString(File.pathSeparator)}")
} cache.put(path, deps)
else -> { cacheOfNulls.remove(path)
if (cache.remove(path) != null || cacheOfNulls.remove(path)) {
log.info("[kts] removed deps for $path")
file 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
} }
} }
@@ -35,6 +35,13 @@ fun <TF> getFilePath(file: TF): String = when (file) {
else -> throw IllegalArgumentException("Unsupported file type $file") else -> throw IllegalArgumentException("Unsupported file type $file")
} }
fun <TF> 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 <TF> getFile(file: TF): File? = when (file) { fun <TF> getFile(file: TF): File? = when (file) {
is PsiFile -> file.originalFile.run { File(virtualFile?.path) } is PsiFile -> file.originalFile.run { File(virtualFile?.path) }
is VirtualFile -> File(file.path) is VirtualFile -> File(file.path)