diff --git a/analysis/analysis-api-impl-base/src/org/jetbrains/kotlin/analysis/api/impl/base/util/LibraryUtils.kt b/analysis/analysis-api-impl-base/src/org/jetbrains/kotlin/analysis/api/impl/base/util/LibraryUtils.kt index b4fbae5904f..41581c7f5c2 100644 --- a/analysis/analysis-api-impl-base/src/org/jetbrains/kotlin/analysis/api/impl/base/util/LibraryUtils.kt +++ b/analysis/analysis-api-impl-base/src/org/jetbrains/kotlin/analysis/api/impl/base/util/LibraryUtils.kt @@ -18,29 +18,43 @@ object LibraryUtils { * * Note that, if [CoreJarFileSystem] is not given, a fresh instance will be used, which will create fresh instances of [VirtualFile], * resulting in potential hash mismatch (e.g., if used in scope membership check). + * + * By default, given [jar], the root, will be included. Pass [includeRoot = false] if not needed. + * Note that, thought, [JvmPackagePartProvider#addRoots] is checking if the root file is in the scope when loading Kotlin modules. + * Thus, if this util is used to populate files for the scope of the Kotlin module as a library, the root should be added too. */ fun getAllVirtualFilesFromJar( jar: Path, jarFileSystem: CoreJarFileSystem = CoreJarFileSystem(), + includeRoot: Boolean = true, ): Collection { - return jarFileSystem.refreshAndFindFileByPath(jar.toAbsolutePath().toString() + JAR_SEPARATOR) - ?.let { getAllVirtualFilesFromRoot(it) } ?: emptySet() + return jarFileSystem.refreshAndFindFileByPath(jar.toString() + JAR_SEPARATOR) + ?.let { getAllVirtualFilesFromRoot(it, includeRoot) } ?: emptySet() } /** * Get all [VirtualFile]s inside the given [dir] (of [Path]) + * + * Note that, if [CoreJarFileSystem] is not given, a fresh instance will be used, which will create fresh instances of [VirtualFile], + * resulting in potential hash mismatch (e.g., if used in scope membership check). */ fun getAllVirtualFilesFromDirectory( dir: Path, + includeRoot: Boolean = true, ): Collection { val fs = StandardFileSystems.local() - return fs.findFileByPath(dir.toAbsolutePath().toString())?.let { getAllVirtualFilesFromRoot(it) } ?: return emptySet() + return fs.findFileByPath(dir.toString()) + ?.let { getAllVirtualFilesFromRoot(it, includeRoot) } ?: emptySet() } private fun getAllVirtualFilesFromRoot( - root: VirtualFile + root: VirtualFile, + includeRoot: Boolean, ): Collection { val files = mutableSetOf() + if (includeRoot) { + files.add(root) + } VfsUtilCore.iterateChildrenRecursively( root, /*filter=*/{ true },