Skip to content

Commit 66c850d

Browse files
committed
Separate classpath from build script classpath
1 parent b4a1467 commit 66c850d

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

server/src/main/resources/kotlinDSLClassPathFinder.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import org.gradle.internal.classpath.ClassPath
33

44
allprojects { project ->
55
task kotlinLSPKotlinDSLDeps {
6-
def pattern = ~/^(?:gradle-(?:kotlin-dsl|base-services|core)|kotlin-(?:compiler|stdlib)).*\.jar/
6+
// def pattern = ~/^(?:gradle-(?:kotlin-dsl|base-services|plugins|core)|kotlin-(?:compiler|stdlib)).*\.jar/
7+
def pattern = ~/^.*\.jar/
78
doLast {
89
(fileTree("$gradle.gradleHomeDir/lib") + fileTree("$gradle.gradleUserHomeDir/caches/$gradle.gradleVersion/generated-gradle-jars"))
910
.findAll { it.name =~ pattern }

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import java.nio.file.Path
66
/** A source for creating class paths */
77
interface ClassPathResolver {
88
val resolverType: String
9+
910
val classpath: Set<Path> // may throw exceptions
1011
val classpathOrEmpty: Set<Path> // does not throw exceptions
1112
get() = try {
@@ -15,6 +16,16 @@ interface ClassPathResolver {
1516
emptySet<Path>()
1617
}
1718

19+
val buildScriptClasspath: Set<Path>
20+
get() = emptySet<Path>()
21+
val buildScriptClasspathOrEmpty: Set<Path>
22+
get() = try {
23+
buildScriptClasspath
24+
} catch (e: Exception) {
25+
LOG.warn("Could not resolve buildscript classpath using {}: {}", resolverType, e.message)
26+
emptySet<Path>()
27+
}
28+
1829
companion object {
1930
/** A default empty classpath implementation */
2031
val empty = object : ClassPathResolver {
@@ -39,10 +50,14 @@ internal class UnionClassPathResolver(val lhs: ClassPathResolver, val rhs: Class
3950
override val resolverType: String get() = "(${lhs.resolverType} + ${rhs.resolverType})"
4051
override val classpath get() = lhs.classpath + rhs.classpath
4152
override val classpathOrEmpty get() = lhs.classpathOrEmpty + rhs.classpathOrEmpty
53+
override val buildScriptClasspath get() = lhs.buildScriptClasspath + rhs.buildScriptClasspath
54+
override val buildScriptClasspathOrEmpty get() = lhs.buildScriptClasspathOrEmpty + rhs.buildScriptClasspathOrEmpty
4255
}
4356

4457
internal class FirstNonEmptyClassPathResolver(val lhs: ClassPathResolver, val rhs: ClassPathResolver) : ClassPathResolver {
4558
override val resolverType: String get() = "(${lhs.resolverType} or ${rhs.resolverType})"
4659
override val classpath get() = lhs.classpath.takeIf { it.isNotEmpty() } ?: rhs.classpath
4760
override val classpathOrEmpty get() = lhs.classpathOrEmpty.takeIf { it.isNotEmpty() } ?: rhs.classpathOrEmpty
61+
override val buildScriptClasspath get() = lhs.buildScriptClasspath.takeIf { it.isNotEmpty() } ?: rhs.buildScriptClasspath
62+
override val buildScriptClasspathOrEmpty get() = lhs.buildScriptClasspathOrEmpty.takeIf { it.isNotEmpty() } ?: rhs.buildScriptClasspathOrEmpty
4863
}

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,25 @@ import java.nio.file.Paths
1414

1515
internal class GradleClassPathResolver(private val path: Path, private val includeKotlinDSL: Boolean): ClassPathResolver {
1616
override val resolverType: String = "Gradle"
17+
private val projectDirectory: Path get() = path.getParent()
1718
override val classpath: Set<Path> get() {
18-
val projectDirectory = path.getParent()
19-
val scripts = listOf("projectClassPathFinder.gradle") + listOf("kotlinDSLClassPathFinder.gradle").takeIf { includeKotlinDSL }.orEmpty()
20-
val tasks = listOf("kotlinLSPProjectDeps") + listOf("kotlinLSPKotlinDSLDeps").takeIf { includeKotlinDSL }.orEmpty()
19+
val scripts = listOf("projectClassPathFinder.gradle")
20+
val tasks = listOf("kotlinLSPProjectDeps")
2121

2222
return readDependenciesViaGradleCLI(projectDirectory, scripts, tasks)
2323
.apply { if (isNotEmpty()) LOG.info("Successfully resolved dependencies for '${projectDirectory.fileName}' using Gradle") }
2424
}
25+
override val buildScriptClasspath: Set<Path> get() {
26+
return if (includeKotlinDSL) {
27+
val scripts = listOf("kotlinDSLClassPathFinder.gradle")
28+
val tasks = listOf("kotlinLSPKotlinDSLDeps")
29+
30+
return readDependenciesViaGradleCLI(projectDirectory, scripts, tasks)
31+
.apply { if (isNotEmpty()) LOG.info("Successfully resolved build script dependencies for '${projectDirectory.fileName}' using Gradle") }
32+
} else {
33+
emptySet()
34+
}
35+
}
2536

2637
companion object {
2738
/** Create a Gradle resolver if a file is a pom. */

0 commit comments

Comments
 (0)