Skip to content

Commit 7a7bfa4

Browse files
author
Maksim Ryzhikov
committed
add option to use runtimeClasspath in GradleClassPathResolver
fwcd#590
1 parent 8c40b4f commit 7a7bfa4

File tree

3 files changed

+58
-15
lines changed

3 files changed

+58
-15
lines changed

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

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,44 @@ import java.nio.file.Path
66
import java.nio.file.PathMatcher
77
import java.nio.file.FileSystems
88

9-
fun defaultClassPathResolver(workspaceRoots: Collection<Path>, db: Database? = null): ClassPathResolver {
9+
data class ResolverOptions(
10+
// Whether to use the compile classpath or the runtime classpath during classpath resolution
11+
val useCompileClasspath: Boolean,
12+
) {
13+
companion object {
14+
fun default(): ResolverOptions {
15+
return ResolverOptions(useCompileClasspath = true)
16+
}
17+
}
18+
}
19+
20+
val DefaultResolverOptions = ResolverOptions.default()
21+
22+
fun defaultClassPathResolver(
23+
workspaceRoots: Collection<Path>,
24+
db: Database? = null,
25+
resolverOptions: ResolverOptions = DefaultResolverOptions,
26+
): ClassPathResolver {
1027
val childResolver = WithStdlibResolver(
1128
ShellClassPathResolver.global(workspaceRoots.firstOrNull())
12-
.or(workspaceRoots.asSequence().flatMap { workspaceResolvers(it) }.joined)
29+
.or(workspaceRoots.asSequence().flatMap { workspaceResolvers(it, resolverOptions) }.joined)
1330
).or(BackupClassPathResolver)
1431

1532
return db?.let { CachedClassPathResolver(childResolver, it) } ?: childResolver
1633
}
1734

1835
/** Searches the workspace for all files that could provide classpath info. */
19-
private fun workspaceResolvers(workspaceRoot: Path): Sequence<ClassPathResolver> {
36+
private fun workspaceResolvers(workspaceRoot: Path, resolverOptions: ResolverOptions): Sequence<ClassPathResolver> {
2037
val ignored: List<PathMatcher> = ignoredPathPatterns(workspaceRoot, workspaceRoot.resolve(".gitignore"))
21-
return folderResolvers(workspaceRoot, ignored).asSequence()
38+
return folderResolvers(workspaceRoot, ignored, resolverOptions).asSequence()
2239
}
2340

2441
/** Searches the folder for all build-files. */
25-
private fun folderResolvers(root: Path, ignored: List<PathMatcher>): Collection<ClassPathResolver> =
42+
private fun folderResolvers(root: Path, ignored: List<PathMatcher>, resolverOptions: ResolverOptions): Collection<ClassPathResolver> =
2643
root.toFile()
2744
.walk()
2845
.onEnter { file -> ignored.none { it.matches(file.toPath()) } }
29-
.mapNotNull { asClassPathProvider(it.toPath()) }
46+
.mapNotNull { asClassPathProvider(it.toPath(), resolverOptions) }
3047
.toList()
3148

3249
/** Tries to read glob patterns from a gitignore. */
@@ -51,7 +68,7 @@ private fun ignoredPathPatterns(root: Path, gitignore: Path): List<PathMatcher>
5168
?: emptyList()
5269

5370
/** Tries to create a classpath resolver from a file using as many sources as possible */
54-
private fun asClassPathProvider(path: Path): ClassPathResolver? =
71+
private fun asClassPathProvider(path: Path, resolverOptions: ResolverOptions): ClassPathResolver? =
5572
MavenClassPathResolver.maybeCreate(path)
56-
?: GradleClassPathResolver.maybeCreate(path)
73+
?: GradleClassPathResolver.maybeCreate(path, resolverOptions)
5774
?: ShellClassPathResolver.maybeCreate(path)

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

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@ import java.nio.file.Files
1010
import java.nio.file.Path
1111
import java.nio.file.Paths
1212

13-
internal class GradleClassPathResolver(private val path: Path, private val includeKotlinDSL: Boolean): ClassPathResolver {
13+
internal class GradleClassPathResolver(
14+
private val path: Path,
15+
private val includeKotlinDSL: Boolean,
16+
private val useCompileClasspath: Boolean
17+
): ClassPathResolver {
1418
override val resolverType: String = "Gradle"
1519
private val projectDirectory: Path get() = path.parent
1620

1721
override val classpath: Set<ClassPathEntry> get() {
1822
val scripts = listOf("projectClassPathFinder.gradle")
1923
val tasks = listOf("kotlinLSPProjectDeps")
2024

21-
return readDependenciesViaGradleCLI(projectDirectory, scripts, tasks)
25+
return readDependenciesViaGradleCLI(projectDirectory, scripts, tasks, useCompileClasspath)
2226
.apply { if (isNotEmpty()) LOG.info("Successfully resolved dependencies for '${projectDirectory.fileName}' using Gradle") }
2327
.map { ClassPathEntry(it, null) }.toSet()
2428
}
@@ -38,9 +42,15 @@ internal class GradleClassPathResolver(private val path: Path, private val inclu
3842

3943
companion object {
4044
/** Create a Gradle resolver if a file is a pom. */
41-
fun maybeCreate(file: Path): GradleClassPathResolver? =
45+
fun maybeCreate(file: Path, options: ResolverOptions): GradleClassPathResolver? =
4246
file.takeIf { file.endsWith("build.gradle") || file.endsWith("build.gradle.kts") }
43-
?.let { GradleClassPathResolver(it, includeKotlinDSL = file.toString().endsWith(".kts")) }
47+
?.let {
48+
GradleClassPathResolver(
49+
path = it,
50+
includeKotlinDSL = file.endsWith(".kts"),
51+
useCompileClasspath = options.useCompileClasspath,
52+
)
53+
}
4454
}
4555
}
4656

@@ -73,13 +83,26 @@ private fun getGradleCommand(workspace: Path): Path {
7383
}
7484
}
7585

76-
private fun readDependenciesViaGradleCLI(projectDirectory: Path, gradleScripts: List<String>, gradleTasks: List<String>): Set<Path> {
86+
private fun readDependenciesViaGradleCLI(
87+
projectDirectory: Path,
88+
gradleScripts: List<String>,
89+
gradleTasks: List<String>,
90+
useCompileClasspath: Boolean = true,
91+
): Set<Path> {
7792
LOG.info("Resolving dependencies for '{}' through Gradle's CLI using tasks {}...", projectDirectory.fileName, gradleTasks)
7893

7994
val tmpScripts = gradleScripts.map { gradleScriptToTempFile(it, deleteOnExit = false).toPath().toAbsolutePath() }
8095
val gradle = getGradleCommand(projectDirectory)
8196

82-
val command = listOf(gradle.toString()) + tmpScripts.flatMap { listOf("-I", it.toString()) } + gradleTasks + listOf("--console=plain")
97+
val command = mutableListOf<String>().apply {
98+
add(gradle.toString())
99+
addAll(tmpScripts.flatMap { listOf("-I", it.toString()) })
100+
addAll(gradleTasks)
101+
add("--console=plain")
102+
103+
if (useCompileClasspath) add("-PuseCompileClasspath=1")
104+
}.toList()
105+
83106
val dependencies = findGradleCLIDependencies(command, projectDirectory)
84107
?.also { LOG.debug("Classpath for task {}", it) }
85108
.orEmpty()

shared/src/main/resources/projectClassPathFinder.gradle

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,11 @@ allprojects { project ->
5252
}
5353
} else if (project.hasProperty('sourceSets')) {
5454
// Print the list of all dependencies jar files.
55+
def useCompileClasspath = project.hasProperty("useCompileClasspath")
56+
5557
sourceSets.forEach {
56-
it.compileClasspath.forEach {
58+
def classPathSource = useCompileClasspath ? it.compileClasspath : it.runtimeClasspath
59+
classPathSource.forEach {
5760
System.out.println "kotlin-lsp-gradle $it"
5861
}
5962
}

0 commit comments

Comments
 (0)