Skip to content

Write HTML. Render video. Built for agents.

fix(engine): resolve the version by walking to our own package root (#4409)

WaterrrForever250c9bd509b6
diff --git a/packages/engine/src/utils/renderProvenance.test.ts b/packages/engine/src/utils/renderProvenance.test.ts
index 7c5a75b0a0..bc96888d1c 100644
--- a/packages/engine/src/utils/renderProvenance.test.ts
+++ b/packages/engine/src/utils/renderProvenance.test.ts
@@ -1,5 +1,5 @@
import { spawnSync } from "node:child_process";
-import { mkdirSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
@@ -11,6 +11,7 @@ import {
PROVENANCE_VERSION_TAG,
readRenderProvenance,
renderProvenanceArgs,
+ resolveOwnVersionFrom,
} from "./renderProvenance.js";
import {
applyFaststart,
@@ -265,3 +266,75 @@ describe.skipIf(!HAS_FFMPEG)("provenance survives a real encode", () => {
});
});
});
+
+describe("resolveOwnVersionFrom", () => {
+ let root: string;
+ const write = (rel: string, body: unknown) => {
+ const dir = join(root, rel);
+ mkdirSync(dir, { recursive: true });
+ writeFileSync(join(dir, "package.json"), JSON.stringify(body));
+ };
+
+ beforeEach(() => {
+ root = mkdtempSync(join(tmpdir(), "hf-version-"));
+ });
+ afterEach(() => {
+ rmSync(root, { recursive: true, force: true });
+ });
+
+ it("finds the version at the package root itself", () => {
+ write(".", { name: "hyperframes", version: "1.2.3" });
+ expect(resolveOwnVersionFrom(root)).toBe("1.2.3");
+ });
+
+ it("walks up from a nested build directory — the published <pkg>/dist/ layout", () => {
+ write(".", { name: "hyperframes", version: "0.8.71" });
+ mkdirSync(join(root, "dist"), { recursive: true });
+ expect(resolveOwnVersionFrom(join(root, "dist"))).toBe("0.8.71");
+ });
+
+ it("walks up from the repo src/utils depth too", () => {
+ write("packages/engine", { name: "@hyperframes/engine", version: "4.5.6" });
+ mkdirSync(join(root, "packages/engine/src/utils"), { recursive: true });
+ expect(resolveOwnVersionFrom(join(root, "packages/engine/src/utils"))).toBe("4.5.6");
+ });
+
+ it("IGNORES a foreign package.json above us — the hoisted-install trap", () => {
+ // This is the whole point of matching on name. A bare "first package.json
+ // found while walking up" returns 9.9.9 here: a confident wrong version,
+ // which is the same class of defect as the sentinel it replaces.
+ write(".", { name: "some-consumer-app", version: "9.9.9" });
+ mkdirSync(join(root, "node_modules/hyperframes/dist"), { recursive: true });
+ expect(resolveOwnVersionFrom(join(root, "node_modules/hyperframes/dist"))).toBe("unresolved");
+ });
+
+ it("prefers our own root over a foreign one further up", () => {
+ write(".", { name: "some-consumer-app", version: "9.9.9" });
+ write("node_modules/hyperframes", { name: "hyperframes", version: "0.8.71" });
+ mkdirSync(join(root, "node_modules/hyperframes/dist"), { recursive: true });
+ expect(resolveOwnVersionFrom(join(root, "node_modules/hyperframes/dist"))).toBe("0.8.71");
+ });
+
+ it("returns the sentinel, not a foreign version, when our root declares none", () => {
+ write(".", { name: "some-consumer-app", version: "9.9.9" });
+ write("node_modules/hyperframes", { name: "hyperframes" });
+ expect(resolveOwnVersionFrom(join(root, "node_modules/hyperframes"))).toBe("unresolved");
+ });
+
+ it("steps over a malformed package.json and keeps walking", () => {
+ write("pkg", { name: "hyperframes", version: "7.7.7" });
+ mkdirSync(join(root, "pkg/dist"), { recursive: true });
+ writeFileSync(join(root, "pkg/dist/package.json"), "{ not json");
+ expect(resolveOwnVersionFrom(join(root, "pkg/dist"))).toBe("7.7.7");
+ });
+
+ it("returns the sentinel when nothing matches", () => {
+ mkdirSync(join(root, "a/b/c"), { recursive: true });
+ expect(resolveOwnVersionFrom(join(root, "a/b/c"))).toBe("unresolved");
+ });
+
+ it("the sentinel is not semver-shaped, so it cannot be read as a version", () => {
+ expect(resolveOwnVersionFrom(root)).toBe("unresolved");
+ expect(/^\d+\.\d+\.\d+/.test("unresolved")).toBe(false);
+ });
+});
diff --git a/packages/engine/src/utils/renderProvenance.ts b/packages/engine/src/utils/renderProvenance.ts
index 63087137c0..9f1cc74522 100644
--- a/packages/engine/src/utils/renderProvenance.ts
+++ b/packages/engine/src/utils/renderProvenance.ts
@@ -1,4 +1,6 @@
-import { createRequire } from "node:module";
+import { readFileSync } from "node:fs";
+import { dirname, join } from "node:path";
+import { fileURLToPath } from "node:url";
import { readTagCI } from "./ffprobe.js";
/**
@@ -29,18 +31,94 @@ export const PROVENANCE_RENDERER_TAG = "hyperframes_renderer";
export const PROVENANCE_VERSION_TAG = "hyperframes_version";
export const PROVENANCE_RENDERER_NAME = "hyperframes";
-const UNKNOWN_VERSION = "0.0.0-dev";
+/**
+ * Deliberately not semver-shaped. A failed lookup must never break a render,
+ * but it must never be readable as a measurement either: the previous
+ * "0.0.0-dev" parses as a version, compares against one, and sorts below every
+ * real release, so a total resolution failure looked exactly like a dev build.
+ * It survived unnoticed across twenty published versions for that reason.
+ */
+const UNRESOLVED_VERSION = "unresolved";
+
+/** The package this file belongs to, whether built standalone or bundled. */
+const OWN_PACKAGE_NAME = /^(?:hyperframes|@hyperframes\/[^/]+)$/;
+
+/** Depth cap: a package root is a handful of levels up, never tens. */
+const MAX_WALK_DEPTH = 12;
+
+/**
+ * The walk, separated from `import.meta.url` so it is testable against real
+ * fixture layouts. A guard that cannot be exercised is a guard nobody has
+ * checked, and the name match below is the part worth exercising.
+ *
+ * @param startDir directory to begin from; the search moves upward.
+ */
+export function resolveOwnVersionFrom(startDir: string): string {
+ // Resolve by WALKING UP to our own package root, not by a fixed relative
+ // path, because this code runs from two different layouts and no single
+ // literal is correct in both:
+ //
+ // repo packages/engine/src/utils/ + ../../ -> packages/engine/package.json OK
+ // published <pkg>/dist/cli.js + ../../ -> node_modules/package.json overshoots
+ //
+ // The published CLI bundles the engine, so the file that actually runs sits
+ // at <pkg>/dist/, one level shallower than the source. The old code used the
+ // repo-correct path, missed in the published artifact, and its catch turned
+ // that miss into a sentinel.
+ //
+ // THE NAME CHECK IS THE FIX, NOT A REFINEMENT. A bare "first package.json
+ // found while walking up" resolves to whatever happens to sit above us in a
+ // hoisted install and returns a confident wrong version - the same class of
+ // defect as the sentinel, and harder to notice because the answer looks
+ // plausible. Only a package.json whose own name is ours may answer.
+ let dir = startDir;
+
+ for (let depth = 0; depth < MAX_WALK_DEPTH; depth++) {
+ const candidate = join(dir, "package.json");
+ let pkg: { name?: unknown; version?: unknown } | undefined;
+ try {
+ pkg = JSON.parse(readFileSync(candidate, "utf8")) as typeof pkg;
+ } catch {
+ // No package.json here, or unreadable/malformed. Either way this
+ // directory cannot answer; keep walking rather than give up, so one
+ // stray file between us and our root is not fatal.
+ pkg = undefined;
+ }
+ if (pkg && typeof pkg.name === "string" && OWN_PACKAGE_NAME.test(pkg.name)) {
+ if (typeof pkg.version === "string" && pkg.version.length > 0) return pkg.version;
+ // Our package root, but no usable version. Stop: walking past it would
+ // only find someone else's.
+ reportUnresolved(`found ${pkg.name} at ${candidate} but it declares no version`);
+ return UNRESOLVED_VERSION;
+ }
+ const parent = dirname(dir);
+ if (parent === dir) break;
+ dir = parent;
+ }
+
+ reportUnresolved(`no package.json matching ${OWN_PACKAGE_NAME} within ${MAX_WALK_DEPTH} levels`);
+ return UNRESOLVED_VERSION;
+}
+
+/**
+ * Make the failure audible. The point of this change is that a resolution
+ * failure stops being indistinguishable from data - a silent fallback is what
+ * let the previous sentinel ship unnoticed.
+ */
+function reportUnresolved(reason: string): void {
+ console.warn(
+ `[renderProvenance] could not resolve the engine version (${reason}); ` +
+ `stamping ${PROVENANCE_VERSION_TAG}="${UNRESOLVED_VERSION}". ` +
+ `This is a lookup failure, not a version - do not treat it as one.`,
+ );
+}
function readEngineVersion(): string {
try {
- // The engine ships as raw TS and exports "./package.json", so this
- // resolves without a build-time define. A failed read must never break a
- // render, hence the fallback rather than a throw.
- const version = (createRequire(import.meta.url)("../../package.json") as { version?: string })
- .version;
- return typeof version === "string" && version.length > 0 ? version : UNKNOWN_VERSION;
+ return resolveOwnVersionFrom(dirname(fileURLToPath(import.meta.url)));
} catch {
- return UNKNOWN_VERSION;
+ reportUnresolved("import.meta.url is not a file URL");
+ return UNRESOLVED_VERSION;
}
}