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
45 changes: 45 additions & 0 deletions .github/workflows/ast-diff-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ on:
- "c/lib/kcl_lib_ast.c"
- "cpp/include/kcl_ast.hpp"
- "cpp/include/kcl_ast_json.hpp"
# Java and Kotlin share one decoder (`kotlin` compiles the same
# `com.kcl.ast` sources java does) but each has its own on top of it, so
# both roots are listed. `.java` under the kotlin source root is that
# shared decoder; the `.kt` files next to it are the writer and the
# `New*` builders, which are kotlin's own and are the part nothing else
# checks.
- "java/src/main/java/com/kcl/ast/**"
- "kotlin/src/main/java/com/kcl/ast/**"
- "kotlin/src/main/kotlin/com/kcl/ast/**"
- ".github/workflows/ast-diff-test.yaml"
workflow_dispatch:

Expand Down Expand Up @@ -86,6 +95,21 @@ jobs:
# layers around them.
- binding: c
- binding: cpp
# Java and Kotlin share a decoder but not a dumper: java is walked by
# reflection over `com.kcl.ast`'s declared fields, and kotlin goes
# through `AstWire.kt`, the writer it owns. Neither needs the JNI
# shim, so unlike java-test.yaml and kotlin-test.yaml these legs build
# nothing — `hack/dump/{java,kotlin}.sh` compile the AST sources
# directly.
#
# JDK 17 rather than the zulu 8 the build matrices use: the dumper
# compiles with `--release 8`, so the language level is pinned by the
# scripts and not by this choice, and 17 is the version the pinned
# kotlin-compiler 1.9.25 is known to run under. `cache: 'maven'` is
# the difference between this leg taking two seconds and taking a
# minute — the dumpers resolve their classpath through maven.
- binding: java
- binding: kotlin
# The only binding that cannot share the default runner. The Swiftly
# installer behind swift-actions/setup-swift@v3 cannot provision a
# Linux runner — the same limitation that keeps swift-test.yaml off
Expand Down Expand Up @@ -243,5 +267,26 @@ jobs:
working-directory: "lib/dotnet"
run: cargo build --release

# --- java / kotlin ----------------------------------------------------
# One JDK for both. The dumper scripts do not run maven's `compile` phase,
# so nothing here builds the JNI shim — the AST is plain Java over Jackson
# and, for kotlin, plain Kotlin over the same classes.
- name: Setup JDK
if: matrix.binding == 'java' || matrix.binding == 'kotlin'
uses: actions/setup-java@v6.0.1
with:
distribution: 'temurin'
java-version: '17'
cache: 'maven'
# The kotlin dumper needs `kotlin-compiler` 1.9.25, which is a *plugin*
# dependency and so is absent from a fresh local maven repository. The
# script falls back to `dependency:go-offline` on its own, but doing it
# here turns a silent minute-long resolve inside the harness into a step
# whose failure is attributable.
- name: Resolve the kotlin compiler
if: matrix.binding == 'kotlin'
working-directory: "lib/kotlin"
run: mvn -q dependency:go-offline

- name: Diff this binding against the golden capture
run: ruby hack/ast_diff.rb ${{ matrix.binding }}
61 changes: 58 additions & 3 deletions hack/ast_diff/bindings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,64 @@ def bindings
"catches the two drifting. Position is nested under `Node::pos`."
),

# ---------------------------------------------------------------- java
Binding.new(
name: "java",
mode: :reflect,
probe: lambda {
# A JDK is needed both to build the dumper and to run it, and
# `javac` alone is not enough: on a machine with the stub `javac`
# but no runtime, `javac -version` succeeds and the dumper then
# fails to start. Check the runtime, not the compiler.
javac = which("javac")
java = which("java")
return "no `javac` on PATH" unless javac
return "no `java` on PATH" unless java

out, _err, status = Open3.capture3(java, "-version")
return "probe failed: `java -version` exited #{status.exitstatus}\n#{out}" \
unless status.success?

nil
},
argv: ->(golden, out) { ["bash", "hack/dump/java.sh", golden, out] },
extra_ok: {},
note: "Jackson decoder over `com.kcl.ast.*`, walked by reflection; " \
"position is flat on `Node`. The tag is read out of the " \
"`@JsonSubTypes` table on the base class, so both the " \
"`As.PROPERTY` and `As.EXTERNAL_PROPERTY` spellings are covered."
),

# -------------------------------------------------------------- kotlin
Binding.new(
name: "kotlin",
mode: :wire,
probe: lambda {
java = which("java")
return "no `java` on PATH" unless java

out, _err, status = Open3.capture3(java, "-version")
return "probe failed: `java -version` exited #{status.exitstatus}\n#{out}" \
unless status.success?

# Deliberately *not* checking for the kotlin compiler jar. The probe
# answers "is the toolchain fundamentally absent?", and a missing
# `java` is; the compiler is a plugin dependency that
# `hack/dump/kotlin.sh` fetches itself when the local maven
# repository is cold. Probing for it here would report "no dumper
# written" for a binding that would have run fine — which on a fresh
# CI runner is exactly what would have happened.
nil
},
argv: ->(golden, out) { ["bash", "hack/dump/kotlin.sh", golden, out] },
extra_ok: {},
note: "Has a real serializer — `AstWire.kt`, the mirror of " \
"`dotnet/KclLib.AST/Wire.cs` — so this is a wire round-trip. The " \
"decoder is the same `com.kcl.ast` classes java uses, compiled " \
"from the kotlin source root; the writer and the `New*` builders " \
"in `AstBuild.kt` are kotlin's own and are what this checks."
),

# ---------------------------------------------------------------- zig
Binding.new(
name: "zig",
Expand Down Expand Up @@ -371,9 +429,6 @@ def bindings
# than quietly short. `reason` must name the blocker.
def self.unrunnable
{
"java" => "no JRE on this machine (`java -version` reports no runtime), and the " \
"loader is Jackson over com.kcl.ast.* — it needs a JVM to run at all",
"kotlin" => "no JRE on this machine; same Jackson loader, compiled by Maven",
"go" => "the Go binding has no typed AST package at all — go/api/client.go exposes " \
"ast_json as an opaque string, so there is no decoder to run the golden through"
}.freeze
Expand Down
37 changes: 37 additions & 0 deletions hack/ast_diff/canonical.rb
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,13 @@ module Canonical
# models it as a named struct (`Pos.swift:528`); the others keep the
# payload raw.
"intliteraltype" => nil,
# The other three `LiteralType` arms, same shape and same reason. These
# two entries are about the *arm*, which carries the `Int` / `Str` / …
# tag on the wire as an adjacent key; the payload it wraps is
# registered separately above, as `<arm>value`.
"boolliteraltype" => nil,
"floatliteraltype" => nil,
"strliteraltype" => nil,
"exprcontext" => nil,
"op" => nil,
"unaryop" => nil,
Expand All @@ -332,6 +339,36 @@ module Canonical
"configentryoperation" => nil,
"quantoperation" => nil,
"numberbinarysuffix" => nil,
# -- Java's wrapper objects for a payload the wire has no name for -----
# `Type` is `#[serde(tag = "type", content = "value")]` — adjacently
# tagged — so the wire object under `value` has no class of its own. The
# java binding gives it one: a plain holder per variant
# (`UnionType.UnionTypeValue`, `ListType.ListTypeValue`, …) with the
# payload's fields straight on it. Same for the literal/number payload
# arms, which java nests one level deeper again through
# `IntLiteralType.IntLiteralTypeValue` because the arm's own `value`
# field is a scalar and needs a box.
#
# These are untagged, so `nil`: the tag at the level above (`Union`,
# `Int`, …) is the one on the wire, and the wrapper is a Java
# implementation detail the comparator walks *through*. Registering them
# is what lets the comparison be about the payload's fields, which is
# the thing worth checking — the wrapper adds no information, so
# claiming a tag for it would only invent a difference.
#
# The counterpart is kotlin's `AstWire.kt`, which is the mirror of
# `dotnet/KclLib.AST/Wire.cs`: those bindings have a writer and collapse
# the wrapper on the way out, which is why the same class tree never
# needed naming there. java has no writer, so the `:reflect` walk meets
# the wrapper where it stands.
"uniontypevalue" => nil,
"listtypevalue" => nil,
"dicttypevalue" => nil,
"namedtypevalue" => nil,
"functiontypevalue" => nil,
"intliteraltypevalue" => nil,
"intnumberlitvalue" => nil,
"floatnumberlitvalue" => nil,
# The sealed base classes and the marker enums, which only ever appear
# as a wrapper or a scalar and never as a payload object of their own.
"kclstmt" => nil,
Expand Down
84 changes: 84 additions & 0 deletions hack/dump/java.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env bash
# Build and run the Java binding's AST dumper.
#
# hack/dump/java.sh <golden.json> <out.json>
#
# `mvn` is not used. The `compile` phase in java/pom.xml is bound to
# `python3 scripts/build.py`, which builds the Rust JNI shim, and the AST
# dumper needs none of that: `com.kcl.ast` is plain Java over Jackson, and the
# whole point of the exercise is to check the decoder, not the transport. So
# this compiles the AST sources plus the dumper against the Jackson jars
# directly.
#
# It compiles the *sources* rather than reusing `target/classes`, for the same
# reason `hack/dump/swift.sh` compiles the Swift sources and
# `hack/dump/wasm.sh` compiles the TypeScript: a build artifact may be older
# than the code, and a harness that silently checked yesterday's decoder would
# report agreement for a decoder that no longer exists.
#
# The classpath is resolved with `mvn dependency:build-classpath` rather than
# hand-listed, so the dumper cannot drift onto a different Jackson than the one
# the binding's pom declares.
set -euo pipefail

if [ "$#" -ne 2 ]; then
echo "usage: hack/dump/java.sh <golden.json> <out.json>" >&2
exit 2
fi

golden="$1"
out="$2"
root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
cd "$root"

# `java` is a stub on a machine with no JRE, so ask for the compiler by name
# and let the shell report it missing rather than running a broken launcher.
javac="$(command -v javac || true)"
java_bin="$(command -v java || true)"
if [ -z "$javac" ] || [ -z "$java_bin" ]; then
echo "no JDK on PATH (both javac and java are needed: javac to build the dumper, java to run it)" >&2
exit 3
fi
if ! "$java_bin" -version >/dev/null 2>&1; then
echo "\`java -version\` fails: there is a javac on PATH but no runtime behind it" >&2
"$java_bin" -version >&2 || true
exit 3
fi

build="$(mktemp -d "${TMPDIR:-/tmp}/kcl-ast-dump-java.XXXXXX")"
trap 'rm -rf "$build"' EXIT

# Online, deliberately. `dependency:build-classpath` is the only way to learn
# the Jackson version the pom actually declares without duplicating it here, and
# a cold CI runner has an empty local maven repository — asking maven to resolve
# that classpath offline would fail before the dumper ever ran, and it would
# fail as a *classpath* error rather than as a decoder error. The CI job sets
# `cache: 'maven'`, so the warm path is the normal one and this is the cold
# fallback.
mvn -q -f java/pom.xml \
dependency:build-classpath -Dmdep.outputFile="$build/cp.txt" >/dev/null

# `com.kcl.ast`, the `com.kcl.loader.*` types `JsonUtil` names, and
# `JsonUtil` itself — the binding's real entry point for turning `ast_json`
# into a tree, which is the code path a Java caller actually goes through.
#
# `com.kcl.util.SematicUtil` is deliberately left out: it is symbol/scope
# resolution over `LoadPackageResult`, which drags in the generated
# `com.kcl.api.Spec` (six figures of protoc output) and has nothing to do with
# decoding an AST. The dumper needs no part of it, and compiling it would put
# the generated protobuf on the harness's critical path for no gain.
find java/src/main/java/com/kcl/ast java/src/main/java/com/kcl/loader \
hack/dump/java -name '*.java' > "$build/sources.txt"
echo java/src/main/java/com/kcl/util/JsonUtil.java >> "$build/sources.txt"

# `--release 8` is the language level java/pom.xml declares, so a Java 9+ API
# in the dumper or in `com.kcl.ast` is a compile error here rather than
# something that passes on a new JDK and fails on the zulu 8 the build matrix
# uses. It is also a slightly stricter check than the default: compiling at the
# running JDK's level would happily accept `List.of` or `var`.
javac -nowarn --release 8 -encoding UTF-8 \
-cp "$(cat "$build/cp.txt")" \
-d "$build/classes" \
@"$build/sources.txt"

"$java_bin" -cp "$build/classes:$(cat "$build/cp.txt")" astdump.Dump "$golden" "$out"
Loading
Loading