Skip to content

Commit a5ec387

Browse files
author
Tormod Alf Try Tufteland
committed
fix slow filewalk in some projects
As it is, with a `.gitignore`-file containing eg. `target`, it would ignore the folder `<the-repo>/target`, but not `<the-repo>/moduleA/target`. Added zero-or more glob dir-matcher `**` to fix this. Also, .gitignore may have a `/` suffix, which will not match a Path to the same folder. I tested: `defaultClassPathResolver(listOf(Paths.get("/path/to/problem/repo"))).classpath` Before change: 1 min, 45 s After change: 7 s It's still possible to decrease this even further. Some ideas for: - The code runs `mvn dependency:list -DincludeScope=test` for every `pom.xml` that is not in an ignored directory. For multimodule project, only the root-pom needs to be run, as this will find deps for all modules. - mvn commands are usually a lot slower than parsing the xml-files manually, and as long as one does not have any 'used undecleared'-deps, that would be sufficient
1 parent 94d7443 commit a5ec387

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

shared/src/main/kotlin/org/javacs/kt/classpath/DefaultClassPathResolver.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package org.javacs.kt.classpath
33
import org.javacs.kt.LOG
44
import java.nio.file.Path
55
import java.nio.file.PathMatcher
6-
import java.nio.file.Files
76
import java.nio.file.FileSystems
87

98
fun defaultClassPathResolver(workspaceRoots: Collection<Path>): ClassPathResolver =
@@ -14,37 +13,38 @@ fun defaultClassPathResolver(workspaceRoots: Collection<Path>): ClassPathResolve
1413

1514
/** Searches the workspace for all files that could provide classpath info. */
1615
private fun workspaceResolvers(workspaceRoot: Path): Sequence<ClassPathResolver> {
17-
val ignored: List<PathMatcher> = ignoredPathPatterns(workspaceRoot.resolve(".gitignore"))
16+
val ignored: List<PathMatcher> = ignoredPathPatterns(workspaceRoot, workspaceRoot.resolve(".gitignore"))
1817
return folderResolvers(workspaceRoot, ignored).asSequence()
1918
}
2019

2120
/** Searches the folder for all build-files. */
2221
private fun folderResolvers(root: Path, ignored: List<PathMatcher>): Collection<ClassPathResolver> =
2322
root.toFile()
2423
.walk()
25-
.onEnter { file -> ignored.none { it.matches(root.relativize(file.toPath())) } }
24+
.onEnter { file -> ignored.none { it.matches(file.toPath()) } }
2625
.mapNotNull { asClassPathProvider(it.toPath()) }
2726
.toList()
2827

2928
/** Tries to read glob patterns from a gitignore. */
30-
private fun ignoredPathPatterns(path: Path): List<PathMatcher> =
31-
path.toFile()
29+
private fun ignoredPathPatterns(root: Path, gitignore: Path): List<PathMatcher> =
30+
gitignore.toFile()
3231
.takeIf { it.exists() }
3332
?.readLines()
3433
?.map { it.trim() }
3534
?.filter { it.isNotEmpty() && !it.startsWith("#") }
35+
?.map { it.removeSuffix("/") }
3636
?.let { it + listOf(
3737
// Patterns that are ignored by default
3838
".git"
3939
) }
4040
?.mapNotNull { try {
41-
LOG.debug("Adding ignore pattern '{}' from {}", it, path)
42-
FileSystems.getDefault().getPathMatcher("glob:$it")
41+
LOG.debug("Adding ignore pattern '{}' from {}", it, gitignore)
42+
FileSystems.getDefault().getPathMatcher("glob:$root**/$it")
4343
} catch (e: Exception) {
4444
LOG.warn("Did not recognize gitignore pattern: '{}' ({})", it, e.message)
4545
null
4646
} }
47-
?: emptyList<PathMatcher>()
47+
?: emptyList()
4848

4949
/** Tries to create a classpath resolver from a file using as many sources as possible */
5050
private fun asClassPathProvider(path: Path): ClassPathResolver? =

0 commit comments

Comments
 (0)