Skip to content

Commit 6584369

Browse files
authored
Merge branch 'main' into aido/627/hermes-doc-sync
2 parents b3865ed + 5623d6b commit 6584369

File tree

11 files changed

+74
-9
lines changed

11 files changed

+74
-9
lines changed

Earthfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ clean-spelling-list:
2525
check-spelling:
2626
DO cspell-ci+CHECK
2727

28+
# check-earthly-names : ensure Cargo/WASM names match Earthly outputs.
29+
check-earthly-names:
30+
FROM python:3.12-slim
31+
32+
WORKDIR /work
33+
COPY . .
34+
RUN python3 scripts/earthly_name_consistency.py
35+
2836
# repo-docs : target to store the documentation from the root of the repo.
2937
repo-docs:
3038
# Create artifacts of extra files we embed inside the documentation when its built.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Guardrail script that keeps Cargo-produced WASM modules aligned with the Earthly
4+
artifact names we publish from each module directory.
5+
6+
It crawls every `Cargo.toml`, looks for `cdylib` targets (the only ones that
7+
output `.wasm`), extracts the declared library name, and compares it with the
8+
first Earthly `--out=...` or `SAVE ARTIFACT ...` directive found nearby.
9+
10+
Any mismatch causes a non-zero exit code so CI/just/earthly targets can enforce
11+
the contract described in the `Cargo.toml` comments.
12+
"""
13+
14+
import re
15+
import sys
16+
import tomllib
17+
from pathlib import Path
18+
19+
ROOT = Path(__file__).resolve().parents[2]
20+
# Regex captures artifact names from statements like `--out=foo.wasm` or
21+
# `SAVE ARTIFACT ... foo.wasm`.
22+
EARTHLY_RE = re.compile(r"(?:--out=|SAVE ARTIFACT )([A-Za-z0-9_-]+)\.wasm")
23+
24+
25+
def earthly_name(dir):
26+
"""Extract the WASM artifact name from the module's Earthfile."""
27+
ef = dir / "Earthfile"
28+
if not ef.exists():
29+
return None
30+
match = EARTHLY_RE.search(ef.read_text())
31+
return match.group(1) if match else None
32+
33+
34+
def cargo_name(path):
35+
"""Return the cdylib name declared in Cargo.toml, if any."""
36+
data = tomllib.loads(path.read_text())
37+
lib = data.get("lib", {})
38+
# Only modules compiled as `cdylib` produce WASM artifacts we care about.
39+
if "cdylib" not in lib.get("crate-type", []):
40+
return None
41+
return lib.get("name") or data["package"]["name"]
42+
43+
44+
def normalize(name: str | None):
45+
if not name:
46+
return None
47+
# Earthly (snake_case) and Cargo (kebab-case) often differ only by
48+
# punctuation; normalize both sides before comparing.
49+
return name.replace("-", "_").lower()
50+
51+
52+
errors = []
53+
for cargo in ROOT.rglob("Cargo.toml"):
54+
name = cargo_name(cargo)
55+
if not name:
56+
continue
57+
e_name = earthly_name(cargo.parent)
58+
# Only flag an error when both sources provide a name and the normalized
59+
# identifiers still differ. (Missing Earthfile entries are ignored so the
60+
# script can be run in repos that mix Rust and non-Rust modules.)
61+
if e_name and normalize(e_name) != normalize(name):
62+
errors.append(f"{cargo.parent}: Cargo '{name}' vs Earthly '{e_name}'")
63+
64+
if errors:
65+
print("\n".join(errors))
66+
sys.exit(1)

wasm/examples/rust/cardano/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ edition = "2024"
55

66
[lib]
77
# The name of the built .wasm file.
8-
# Keep it consistent with Earthly build output name.
98
name = "cardano_rs"
109
crate-type = ["cdylib"]
1110

wasm/examples/rust/cardano_age/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ edition = "2024"
55

66
[lib]
77
# The name of the built .wasm file.
8-
# Keep it consistent with Earthly build output name.
98
name = "cardano_age_rs"
109
crate-type = ["cdylib"]
1110

wasm/examples/rust/next_century/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ edition = "2024"
55

66
[lib]
77
# The name of the built .wasm file.
8-
# Keep it consistent with Earthly build output name.
98
name = "next_century_rs"
109
crate-type = ["cdylib"]
1110

wasm/examples/rust/sqlite_version/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ edition = "2024"
55

66
[lib]
77
# The name of the built .wasm file.
8-
# Keep it consistent with Earthly build output name.
98
name = "sqlite_version_rs"
109
crate-type = ["cdylib"]
1110

wasm/integration-test/cardano/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ edition = "2024"
55

66
[lib]
77
# The name of the built .wasm file.
8-
# Keep it consistent with Earthly build output name.
98
name = "cardano"
109
crate-type = ["cdylib"]
1110

wasm/integration-test/http_reply/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ edition = "2024"
55

66
[lib]
77
# The name of the built .wasm file.
8-
# Keep it consistent with Earthly build output name.
98
name = "http"
109
crate-type = ["cdylib"]
1110

wasm/integration-test/ipfs/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ edition = "2024"
55

66
[lib]
77
# The name of the built .wasm file.
8-
# Keep it consistent with Earthly build output name.
98
name = "ipfs"
109
crate-type = ["cdylib"]
1110

wasm/integration-test/sqlite/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ edition = "2024"
55

66
[lib]
77
# The name of the built .wasm file.
8-
# Keep it consistent with Earthly build output name.
98
name = "sqlite"
109
crate-type = ["cdylib"]
1110

0 commit comments

Comments
 (0)