Skip to content

Commit 750bc71

Browse files
committed
Fix snapshot version mismatch between JAR and npm/pip packages
CI runs two Gradle invocations: `build` then `snapshot publish`. Each independently called LocalDateTime.now() to generate versioned timestamps, so the version baked into the JAR version.txt didn't match the version published to npm/PyPI. Both modules now read the version.txt file from disk (written during `build`) before falling back to generating a fresh timestamp.
1 parent 66b51ea commit 750bc71

2 files changed

Lines changed: 20 additions & 26 deletions

File tree

rewrite-javascript/build.gradle.kts

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,20 @@ extensions.configure<NodeExtension> {
5555
nodeProjectDir.set(projectDir.resolve("rewrite"))
5656
}
5757

58-
// Generate a timestamped version for CI builds, or use the regular version for local development
59-
val datedSnapshotVersion = if (System.getenv("CI") != null) {
60-
project.version.toString().replace(
61-
"SNAPSHOT",
62-
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss"))
63-
)
58+
// Read the version from the version.txt written on disk by a prior build, or generate a fresh
59+
// timestamped version. This ensures the second Gradle invocation (`snapshot publish`) publishes
60+
// the same version that the first invocation (`build`) baked into the JAR.
61+
val versionTxt = file("src/main/resources/META-INF/rewrite-javascript-version.txt")
62+
val datedSnapshotVersion: String = if (System.getenv("CI") != null) {
63+
versionTxt.takeIf { it.exists() }?.readText()?.trim()?.takeIf { it.isNotEmpty() }
64+
?: project.version.toString().replace(
65+
"SNAPSHOT",
66+
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd-HHmmss"))
67+
)
6468
} else {
6569
project.version.toString()
6670
}
6771

68-
// Helper function to extract version from the JAR if it exists
69-
fun extractVersionFromJar(): String? {
70-
val jarTask = tasks.named("jar", Jar::class).get()
71-
val jarFile = jarTask.archiveFile.get().asFile
72-
if (!jarFile.exists()) return null
73-
74-
return zipTree(jarFile).matching {
75-
include("META-INF/rewrite-javascript-version.txt")
76-
}.singleFile.readText().trim()
77-
}
78-
7972
val npmVersion = tasks.register<NpmTask>("npmVersion") {
8073
val versionDir = layout.buildDirectory.file("tmp/npmVersion")
8174
inputs.file("rewrite/package.json")
@@ -89,9 +82,7 @@ val npmVersion = tasks.register<NpmTask>("npmVersion") {
8982
}
9083
}
9184

92-
// Use version from JAR if available (second Gradle invocation), otherwise use generated version
93-
val versionToUse = provider { extractVersionFromJar() ?: datedSnapshotVersion }
94-
args = listOf("version", "--no-git-tag-version", versionToUse.get())
85+
args = listOf("version", "--no-git-tag-version", datedSnapshotVersion)
9586
workingDir = versionDir
9687
}
9788

@@ -160,8 +151,7 @@ val npmPack = tasks.register<Tar>("npmPack") {
160151
}
161152

162153
archiveBaseName = "openrewrite-rewrite"
163-
// Use version from JAR if available (second Gradle invocation), otherwise use generated version
164-
archiveVersion = provider { extractVersionFromJar() ?: datedSnapshotVersion }.get()
154+
archiveVersion = datedSnapshotVersion
165155
compression = Compression.GZIP
166156
archiveExtension = "tgz"
167157
destinationDirectory = layout.buildDirectory.dir("distributions")

rewrite-python/build.gradle.kts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,15 @@ tasks.withType<Test> {
213213
// Generate a PEP 440 compliant version for CI builds
214214
// Snapshots use .dev suffix: 8.71.0.dev20260112145318
215215
// Releases use clean version: 8.71.0
216+
// Read from version.txt on disk if it exists (second Gradle invocation), so the published pip
217+
// package version matches what was baked into the JAR by the first invocation.
218+
val pythonVersionTxt = file("src/main/resources/META-INF/rewrite-python-version.txt")
216219
val pythonVersion: String = if (System.getenv("CI") != null) {
217-
project.version.toString().replace(
218-
"-SNAPSHOT",
219-
".dev${LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))}"
220-
)
220+
pythonVersionTxt.takeIf { it.exists() }?.readText()?.trim()?.takeIf { it.isNotEmpty() }
221+
?: project.version.toString().replace(
222+
"-SNAPSHOT",
223+
".dev${LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))}"
224+
)
221225
} else {
222226
project.version.toString().replace("-SNAPSHOT", ".dev0")
223227
}

0 commit comments

Comments
 (0)