Skip to content

Commit 0d9d099

Browse files
authored
fix: make InMemoryCache compatible with isolated projects (#1736)
Signed-off-by: Jendrik Johannes <jendrik@onepiece.software>
1 parent e3bc5ad commit 0d9d099

6 files changed

Lines changed: 24 additions & 36 deletions

File tree

src/functionalTest/groovy/com/autonomousapps/jvm/projects/IncludedBuildWithDivergingPluginClasspathsProject.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package com.autonomousapps.jvm.projects
44

55
import com.autonomousapps.AbstractProject
66
import com.autonomousapps.kit.GradleProject
7+
import com.autonomousapps.kit.gradle.GradleProperties
78
import com.autonomousapps.kit.gradle.Plugin
89
import com.autonomousapps.kit.gradle.dependencies.Plugins
910

@@ -28,6 +29,7 @@ final class IncludedBuildWithDivergingPluginClasspathsProject extends AbstractPr
2829

2930
return newGradleProjectBuilder()
3031
.withRootProject { root ->
32+
root.gradleProperties += GradleProperties.enableIsolatedProjects()
3133
root.withBuildScript { bs ->
3234
bs.plugins.add(Plugin.javaLibrary)
3335
bs.additions = printServiceObject

src/main/kotlin/com/autonomousapps/internal/analyzer/AndroidProjectAnalyzer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ internal abstract class AndroidAnalyzer(
132132

133133
final override fun registerFindDeclaredProcsTask(): TaskProvider<FindDeclaredProcsTask> =
134134
project.tasks.register("findDeclaredProcs$taskNameSuffix", FindDeclaredProcsTask::class.java) {
135-
it.inMemoryCacheProvider.set(InMemoryCache.register(project))
135+
InMemoryCache.register(it.inMemoryCacheProvider, project)
136136
kaptConf()?.let { configuration ->
137137
it.setKaptArtifacts(configuration.incoming.artifacts)
138138
}

src/main/kotlin/com/autonomousapps/internal/analyzer/DependencyAnalyzer.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ internal abstract class AbstractDependencyAnalyzer(
399399
androidLintTask: TaskProvider<FindAndroidLinters>?,
400400
): TaskProvider<ExplodeJarTask> {
401401
return project.tasks.register("explodeJar$taskNameSuffix", ExplodeJarTask::class.java) { t ->
402-
t.inMemoryCache.set(InMemoryCache.register(project))
402+
InMemoryCache.register(t.inMemoryCache, project)
403403
t.compileClasspath.setFrom(
404404
project.configurations.getByName(compileConfigurationName)
405405
.artifactsFor(attributeValueJar)
@@ -414,7 +414,7 @@ internal abstract class AbstractDependencyAnalyzer(
414414

415415
final override fun registerFindKotlinMagicTask(artifactsReport: TaskProvider<ArtifactsReportTask>): TaskProvider<FindKotlinMagicTask> {
416416
return project.tasks.register("findKotlinMagic$taskNameSuffix", FindKotlinMagicTask::class.java) { t ->
417-
t.inMemoryCacheProvider.set(InMemoryCache.register(project))
417+
InMemoryCache.register(t.inMemoryCacheProvider, project)
418418
t.compileClasspath.setFrom(
419419
project.configurations.getByName(compileConfigurationName)
420420
.artifactsFor(attributeValueJar)
@@ -527,7 +527,7 @@ internal abstract class AbstractDependencyAnalyzer(
527527
"synthesizeDependencies$taskNameSuffix",
528528
SynthesizeDependenciesTask::class.java
529529
) { t ->
530-
t.inMemoryCache.set(InMemoryCache.register(project))
530+
531531
t.projectPath.set(project.path)
532532
t.compileDependencies.set(graphViewTask.flatMap { it.outputNodes })
533533
t.physicalArtifacts.set(artifactsReport.flatMap { it.output })

src/main/kotlin/com/autonomousapps/internal/analyzer/JvmProjectAnalyzer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ internal abstract class JvmAnalyzer(
7272

7373
final override fun registerFindDeclaredProcsTask(): TaskProvider<FindDeclaredProcsTask> {
7474
return project.tasks.register("findDeclaredProcs$taskNameSuffix", FindDeclaredProcsTask::class.java) {
75-
it.inMemoryCacheProvider.set(InMemoryCache.register(project))
75+
InMemoryCache.register(it.inMemoryCacheProvider, project)
7676
kaptConf()?.let { configuration ->
7777
it.setKaptArtifacts(configuration.incoming.artifacts)
7878
}

src/main/kotlin/com/autonomousapps/internal/analyzer/KmpProjectAnalyzer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ internal class KmpProjectAnalyzer(
6969

7070
override fun registerFindDeclaredProcsTask(): TaskProvider<FindDeclaredProcsTask> {
7171
return project.tasks.register("findDeclaredProcs$taskNameSuffix", FindDeclaredProcsTask::class.java) {
72-
it.inMemoryCacheProvider.set(InMemoryCache.register(project))
72+
InMemoryCache.register(it.inMemoryCacheProvider, project)
7373
kaptConf()?.let { configuration ->
7474
it.setKaptArtifacts(configuration.incoming.artifacts)
7575
}

src/main/kotlin/com/autonomousapps/services/InMemoryCache.kt

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
package com.autonomousapps.services
66

7-
import com.autonomousapps.DependencyAnalysisPlugin
87
import com.autonomousapps.Flags.cacheSize
98
import com.autonomousapps.model.internal.intermediates.producer.AnnotationProcessorDependency
109
import com.autonomousapps.model.internal.intermediates.ExplodingJar
@@ -59,38 +58,25 @@ public abstract class InMemoryCache : BuildService<InMemoryCache.Params> {
5958
// To share service across the whole build tree - https://github.com/gradle/gradle/issues/14697
6059
private fun Gradle.rootBuild(): Gradle = parent?.rootBuild() ?: this
6160

62-
/**
63-
* Determines the build on which to register the service. In a composite build, the root build is used to share the
64-
* cache across builds if the root build used that same classloader for loading the plugin as the current build.
65-
* See: https://github.com/gradle/gradle/issues/17559
66-
*/
67-
private fun Project.serviceHoldingBuild(): Gradle {
68-
val thisBuild = gradle
69-
val rootBuild = thisBuild.rootBuild()
70-
71-
if (thisBuild == rootBuild) {
72-
return thisBuild
73-
}
74-
75-
val thisPluginInstance = thisBuild.rootProject.plugins.findPlugin(DependencyAnalysisPlugin.ID)
76-
val rootPluginInstance = rootBuild.rootProject.plugins.findPlugin(DependencyAnalysisPlugin.ID)
77-
78-
if (thisPluginInstance == null || rootPluginInstance == null) {
79-
return thisBuild
61+
private fun Gradle.registerService(cacheSize: Long): Provider<InMemoryCache> = sharedServices
62+
.registerIfAbsent(SHARED_SERVICES_IN_MEMORY_CACHE, InMemoryCache::class.java) {
63+
it.parameters.cacheSize.set(cacheSize)
8064
}
8165

82-
return if (thisPluginInstance::class.java == rootPluginInstance::class.java) {
83-
rootBuild // shared cache in the root if plugin was loaded with same classloader
84-
} else {
85-
thisBuild
66+
fun register(property: Property<InMemoryCache>, project: Project) {
67+
val cacheSize = project.cacheSize(DEFAULT_CACHE_VALUE)
68+
69+
// In a composite build, the root build is used to share the cache across builds if the root build used
70+
// the same classloader for loading the plugin as the current build.
71+
// See: https://github.com/gradle/gradle/issues/17559
72+
val rootService = project.gradle.rootBuild().registerService(cacheSize)
73+
try {
74+
property.set(rootService)
75+
} catch (_: IllegalArgumentException) {
76+
// Root service is not of valid type as it was loaded with a different classloader.
77+
// Fall back to an isolated service.
78+
property.set(project.gradle.registerService(cacheSize))
8679
}
8780
}
88-
89-
fun register(project: Project): Provider<InMemoryCache> = project
90-
.serviceHoldingBuild()
91-
.sharedServices
92-
.registerIfAbsent(SHARED_SERVICES_IN_MEMORY_CACHE, InMemoryCache::class.java) {
93-
it.parameters.cacheSize.set(project.cacheSize(DEFAULT_CACHE_VALUE))
94-
}
9581
}
9682
}

0 commit comments

Comments
 (0)