Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,69 @@ jobs:
report_paths: '**/build/test-results/test/*.xml'
annotate_only: true
job_summary: true

# The WebAssembly module, built and tested as the release's `wasm` job does, so that a change that breaks it shows
# in its pull request rather than in a release. The module itself is not kept.
wasm:
name: wasm
needs: build
runs-on: ubuntu-latest
timeout-minutes: 60
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

# The jars compile with Java 21 through JDK21_HOME, as in `native`. Installed before GraalVM, so that Gradle
# still runs on GraalVM. No problem matcher: `build` annotates the warnings.
- name: Install JDK 21 for the jars
id: jdk21
uses: actions/setup-java@77dd5b2ce225ea11469928ac317850245a74ae78 # v6.2.0
with:
distribution: temurin
java-version: '21'
problem-matcher: false

# The same release as in release.yml: Web Image is experimental, and its options and output change between
# releases.
- name: Install Oracle GraalVM 25 for the module
uses: graalvm/setup-graalvm@f70bc8032f90a104502824a54bcb94b6f01715bc # v1.6.8
with:
java-version: '25.0.4'
distribution: 'graalvm'
github-token: ${{ secrets.GITHUB_TOKEN }}

# Web Image runs wasm-opt from the PATH, and needs Binaryen 119 or later; Ubuntu's package is older.
- name: Install Binaryen
env:
BINARYEN_VERSION: version_133
BINARYEN_SHA256: 2dc9c7813f5375db93d96ead4b78222fcc3e2677bbb832297af4797782a37489
run: |
archive="binaryen-${BINARYEN_VERSION}-x86_64-linux.tar.gz"
curl --fail --silent --show-error --location --output "${RUNNER_TEMP}/${archive}" \
"https://github.com/WebAssembly/binaryen/releases/download/${BINARYEN_VERSION}/${archive}"
echo "${BINARYEN_SHA256} ${RUNNER_TEMP}/${archive}" | sha256sum --check
tar -xzf "${RUNNER_TEMP}/${archive}" -C "${RUNNER_TEMP}"
echo "${RUNNER_TEMP}/binaryen-${BINARYEN_VERSION}/bin" >> "$GITHUB_PATH"

# Node 24 has the WebAssembly GC and exception handling the module needs.
- name: Install Node.js 24 for the tests
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version: '24'
package-manager-cache: false

# The same cache as in `build`.
- name: Cache Gradle's downloads
uses: gradle/actions/setup-gradle@8da46b539cd8fe6ab27ca75128919a18d757604c # v6.4.1
with:
gradle-home-cache-excludes: |
caches/build-cache-1
caches/modules-2/files-2.1/com.jetbrains.intellij.idea
caches/*/transforms

# wasmTest: the smoke test, then every Java 21 and Java 25 test case's input against the runnable jar.
- name: Build and test the module
run: ./gradlew -Pwasm=true :open-java-format-wasm:wasmTest
env:
JDK21_HOME: ${{ steps.jdk21.outputs.path }}
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ jobs:
node-version: '24'
package-manager-cache: false

# wasmTest: the native image's smoke test, then every test case's input against the runnable jar.
# wasmTest: the smoke test, then every Java 21 and Java 25 test case's input against the runnable jar.
- name: Build and test the module
run: ./gradlew -Pwasm=true :open-java-format-wasm:wasmTest :open-java-format-wasm:wasmZip
env:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ unexecutable file.
### WebAssembly module

The playground on openjavaformat.dev runs the formatter in the browser, compiled to WebAssembly by
GraalVM's Web Image backend. Only the release workflow builds it, in its `wasm` job, and the zip goes
on the GitHub release; `./gradlew build` leaves it out. Web Image ships in Oracle GraalVM only, so the
GraalVM's Web Image backend. CI builds and tests it in its `wasm` job, the release workflow in its
own, and the zip goes on the GitHub release; `./gradlew build` leaves it out. Web Image ships in Oracle GraalVM only, so the
module needs Oracle GraalVM 25 behind `GRAALVM_HOME`, Binaryen 119 or later (`wasm-opt`) on the
`PATH`, and Node.js 24 or later for the tests:

Expand Down
8 changes: 5 additions & 3 deletions open-java-format-wasm/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ def wasmSmokeTest = tasks.register('wasmSmokeTest', Exec) {
}

def allJar = project(':open-java-format').tasks.named('allJar', Jar)
def java21 = javaToolchains.launcherFor { it.languageVersion.set(JavaLanguageVersion.of(21)) }

// The jar runs on the JDK the module is built with, so that both parse with the same javac, and so on the cases for
// Java 25 as well as those for 21. The ones for 28 need a newer javac than the module has.
def wasmDifferentialTest = tasks.register('wasmDifferentialTest', Exec) {
description = 'Formats every test case input with the module and with the runnable jar, and compares.'
group = 'verification'
Expand All @@ -116,10 +117,11 @@ def wasmDifferentialTest = tasks.register('wasmDifferentialTest', Exec) {
argumentProviders.add({
[
wrapper.get(),
java21.get().executablePath.asFile.path,
javaToolchains.launcherFor(oracleGraalVm).get().executablePath.asFile.path,
allJar.get().archiveFile.get().asFile.path,
project(':open-java-format').file('src/test/resources/test-cases').path,
layout.buildDirectory.dir('wasm-differential').get().asFile.path,
project(':open-java-format').file('src/test/resources/test-cases-java21').path,
project(':open-java-format').file('src/test/resources/test-cases-java25').path,
]
} as CommandLineArgumentProvider)
}
Expand Down
26 changes: 14 additions & 12 deletions open-java-format-wasm/src/test/js/differential.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

// The module against the runnable jar of the same build, on the input of every test case: both must give the same
// bytes, or the same diagnostics. The jar formats all the inputs in one run with --replace, so the JVM starts once.
// Usage: node differential.mjs <open-java-format.js> <java> <open-java-format-all.jar> <test-cases dir> <work dir>
// Usage: node differential.mjs <open-java-format.js> <java> <open-java-format-all.jar> <work dir> <test-cases dir>...

import { spawnSync } from "node:child_process";
import { mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { join, relative } from "node:path";
import { dirname, join, relative } from "node:path";
import { load } from "./load.mjs";

const [wrapperPath, java, jar, casesRoot, workDir] = process.argv.slice(2);
const [wrapperPath, java, jar, workDir, ...casesRoots] = process.argv.slice(2);

// As CaseFile.java reads a case: the input is what lies between the first two lines of six or more dashes.
const SEPARATOR = /-{6,}(?:\r\n|\r|\n|$)/g;
Expand All @@ -38,16 +38,18 @@ function caseFiles(dir) {
.sort();
}

// One .java file per case, named after it, so that the jar's diagnostics name the case too.
// One .java file per case, named after its folder and itself, so that the jar's diagnostics name the case too.
rmSync(workDir, { recursive: true, force: true });
const inputs = caseFiles(casesRoot).map(caseFile => {
const name = relative(casesRoot, caseFile).replaceAll(/[\\/]/g, "__").replace(/\.case$/, ".java");
const path = join(workDir, name);
const input = inputOf(readFileSync(caseFile, "utf8"));
mkdirSync(workDir, { recursive: true });
writeFileSync(path, input);
return { name, path, input };
});
const inputs = casesRoots.flatMap(casesRoot =>
caseFiles(casesRoot).map(caseFile => {
const name = relative(dirname(casesRoot), caseFile).replaceAll(/[\\/]/g, "__").replace(/\.case$/, ".java");
const path = join(workDir, name);
const input = inputOf(readFileSync(caseFile, "utf8"));
mkdirSync(workDir, { recursive: true });
writeFileSync(path, input);
return { name, path, input };
}),
);

const jvm = spawnSync(java, ["-jar", jar, "--replace", ...inputs.map(input => input.path)], {
encoding: "utf8",
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ include ':open-java-format-benchmarks'
include ':open-java-format-native'

// The WebAssembly module compiles against Oracle GraalVM, which nobody who only wants the jars has, so it is part of
// the build only with -Pwasm=true. The release workflow builds it that way; nothing else does.
// the build only with -Pwasm=true. CI's and the release's `wasm` jobs build it that way; nothing else does.
if (Boolean.parseBoolean(providers.gradleProperty('wasm').getOrElse('false'))) {
include ':open-java-format-wasm'
}
Expand Down
Loading