Skip to content

Commit 694fc2b

Browse files
committed
Key the app build on skills/ and migrations contents; skip wasm in the app image
include_dir! doesn't tell cargo or kache when the embedded folders change, so a skills or migrations edit could reuse a stale compile. build.rs now reruns on those folders and passes a content hash through rustc-env, which the crate reads, so the compile key follows the contents. The app image serves no Compare page (the site's Worker does), so its frontend build sets VISUALCI_SKIP_WASM=1 and wasm.mjs skips the wasm build on purpose.
1 parent 0848df2 commit 694fc2b

5 files changed

Lines changed: 50 additions & 1 deletion

File tree

‎app/build.rs‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,36 @@
1+
//! The folders app/ embeds at compile time (database/migrations through `laravel::db::migrations!`, the repository's
2+
//! skills/ through `include_dir!`) aren't inputs Cargo or a compile cache (kache in CI) can see: a new file in one would
3+
//! leave the crate's key unchanged and could ship a binary without it. So each folder's contents are hashed into an
4+
//! environment variable the code that embeds it reads (`env!`), which makes them part of the key.
5+
use std::collections::hash_map::DefaultHasher;
6+
use std::fs;
7+
use std::hash::Hasher;
8+
use std::path::Path;
9+
10+
fn hash_dir(hasher: &mut DefaultHasher, root: &Path, dir: &Path) {
11+
let mut entries: Vec<_> = fs::read_dir(dir)
12+
.unwrap_or_else(|e| panic!("{}: {e}", dir.display()))
13+
.map(|entry| entry.expect("a directory entry").path())
14+
.collect();
15+
entries.sort();
16+
for path in entries {
17+
hasher.write(path.strip_prefix(root).unwrap().to_string_lossy().as_bytes());
18+
if path.is_dir() {
19+
hash_dir(hasher, root, &path);
20+
} else {
21+
hasher.write(&fs::read(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display())));
22+
}
23+
}
24+
}
25+
26+
fn contents_hash(var: &str, dir: &str) {
27+
println!("cargo::rerun-if-changed={dir}");
28+
let mut hasher = DefaultHasher::new();
29+
hash_dir(&mut hasher, Path::new(dir), Path::new(dir));
30+
println!("cargo::rustc-env={var}={:016x}", hasher.finish());
31+
}
32+
133
fn main() {
2-
println!("cargo::rerun-if-changed=database/migrations");
34+
contents_hash("VISUALCI_MIGRATIONS_HASH", "database/migrations");
35+
contents_hash("VISUALCI_SKILLS_HASH", "../skills");
336
}

‎app/src/lib.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ pub mod testing;
2424
use laravel::Application;
2525
use laravel::foundation::ApplicationBuilder;
2626

27+
/// The migrations' contents hash (build.rs): reading it makes them part of this crate's compile key, so a new
28+
/// migration rebuilds it.
29+
const _MIGRATIONS_HASH: &str = env!("VISUALCI_MIGRATIONS_HASH");
30+
2731
pub fn configure() -> ApplicationBuilder {
2832
Application::configure()
2933
.with_migrations(laravel::db::migrations!("database/migrations"))

‎app/src/skills.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
use include_dir::{Dir, include_dir};
55

66
static SKILLS: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/../skills");
7+
/// The skills' contents hash (build.rs): reading it makes them part of this crate's compile key, so an edited skill
8+
/// rebuilds it.
9+
const _SKILLS_HASH: &str = env!("VISUALCI_SKILLS_HASH");
710

811
/// One skill: its name, description and SKILL.md.
912
pub struct Skill {

‎docker/app.Dockerfile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ COPY frontend/ ./
1111
# 1 in CI's production image: the app's Plausible snippet (frontend/analytics.ts).
1212
ARG VISUALCI_ANALYTICS=0
1313
ENV VISUALCI_ANALYTICS=${VISUALCI_ANALYTICS}
14+
# The site's Compare two PDFs page (and its WebAssembly) is served by the site's Worker, never by the app.
15+
ENV VISUALCI_SKIP_WASM=1
1416
RUN if [ -f pnpm-lock.yaml ]; then pnpm build; else npm run build; fi
1517

1618
# cargo-chef: the dependencies build in a layer of their own, rebuilt only when a Cargo.toml or Cargo.lock changes.

‎frontend/scripts/wasm.mjs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//
55
// Needs Rust's wasm32-unknown-unknown target (`rustup target add wasm32-unknown-unknown`). Without Rust it warns and
66
// leaves the page's tool unable to start, unless VISUALCI_REQUIRE_WASM=1 (the production site's build), where it fails.
7+
// VISUALCI_SKIP_WASM=1 skips it quietly, for a build that serves no site page (the app's image).
78
import { spawnSync } from 'node:child_process'
89
import { copyFile, mkdir, stat } from 'node:fs/promises'
910
import { join } from 'node:path'
@@ -14,6 +15,12 @@ const target = 'wasm32-unknown-unknown'
1415
const out = join(root, 'public/visualci-pdf.wasm')
1516
const required = process.env.VISUALCI_REQUIRE_WASM === '1'
1617

18+
// The app's image (docker/app.Dockerfile) serves no site page, so it builds without the wasm on purpose.
19+
if (process.env.VISUALCI_SKIP_WASM === '1') {
20+
console.log('wasm: skipped (VISUALCI_SKIP_WASM=1: this build serves no Compare two PDFs page)')
21+
process.exit(0)
22+
}
23+
1724
const build = spawnSync('cargo', ['build', '--release', '--locked', '-p', 'visualci-wasm', '--target', target], {
1825
cwd: repo,
1926
stdio: 'inherit',

0 commit comments

Comments
 (0)