Skip to content

Commit b2b8602

Browse files
authored
Fix ZipException when generating metadata in a Gradle daemon. (#2)
2 parents c30a5e3 + c8bea73 commit b2b8602

3 files changed

Lines changed: 26 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# AtPlug releases
22

33
## [Unreleased]
4+
### Fixed
5+
- The Gradle daemon would throw `ZipException` during the second invocation of metadata generation, now fixed.
46

57
## [0.1.0] - 2022-02-16
6-
* Migration to open source is WIP.
8+
- Migration to open source is WIP.

atplug-plugin-gradle/src/main/java/com/diffplug/atplug/tooling/PlugGenerator.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class PlugGenerator internal constructor(toSearches: List<File>, toLinkAgainst:
4343
private val metadataGeneratorFor: KFunction<Function<Any, String>>
4444

4545
init {
46+
System.setProperty("atplug.generate", "true")
4647
// create a classloader which looks in toSearch first, then each of the jars in toLinkAgainst
4748
val urls = (toSearches + toLinkAgainst).map { it.toURI().toURL() }.toTypedArray()
4849
val parent: ClassLoader? =
@@ -74,6 +75,7 @@ class PlugGenerator internal constructor(toSearches: List<File>, toLinkAgainst:
7475
}
7576
} finally {
7677
classLoader.close()
78+
System.setProperty("atplug.generate", "")
7779
}
7880
}
7981

atplug-runtime/src/main/java/com/diffplug/atplug/PlugRegistry.kt

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import java.net.URL
1212
import java.nio.charset.StandardCharsets
1313
import java.util.*
1414
import java.util.jar.Manifest
15+
import java.util.zip.ZipException
1516

1617
interface PlugRegistry {
1718
fun <T> registerSocket(socketClass: Class<T>, socketOwner: SocketOwner<T>)
@@ -70,12 +71,26 @@ interface PlugRegistry {
7071
for (service in
7172
services.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()) {
7273
val servicePath = service.trim { it <= ' ' }
73-
if (servicePath.isNotEmpty()) {
74-
val asString = manifestUrl.toExternalForm()
75-
val component = parseComponent(asString, servicePath)
76-
synchronized(this) {
77-
data.putDescriptor(component.provides, component)
78-
owners.get(component.provides)?.doRegister(component)
74+
try {
75+
if (servicePath.isNotEmpty()) {
76+
val asString = manifestUrl.toExternalForm()
77+
val component = parseComponent(asString, servicePath)
78+
synchronized(this) {
79+
data.putDescriptor(component.provides, component)
80+
owners.get(component.provides)?.doRegister(component)
81+
}
82+
}
83+
} catch (e: ZipException) {
84+
// When a JVM loads a jar, it mmaps the jar. If that jar changes
85+
// (as it does when generating plugin metadata in a Gradle daemon)
86+
// then you get ZipException after the change. The accuracy of the
87+
// registry is irrelevant during metadata generation - the registry
88+
// exists during metadata generation only because the `SocketOwner`s
89+
// register themselves in their constructors. Therefore, it is safe to
90+
// ignore these errors during metadata generation.
91+
val prop = System.getProperty("atplug.generate")
92+
if (prop != "true") {
93+
throw e
7994
}
8095
}
8196
}

0 commit comments

Comments
 (0)