Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

test(assets): the cook says when a package names what it does not carry

esengine45cce919eca1
diff --git a/desktop/tests/document-ref-cook-deps.test.ts b/desktop/tests/document-ref-cook-deps.test.ts
index 30cd3faad..d11d22fe0 100644
--- a/desktop/tests/document-ref-cook-deps.test.ts
+++ b/desktop/tests/document-ref-cook-deps.test.ts
@@ -143,5 +143,34 @@ describe('assets named only inside another document are cooked', () => {
const font = manifest.entries.find((e) => e.uuid === FNT)!;
const stagedFnt = readFileSync(path.join(res.outDir, font.path), 'utf8');
expect(stagedFnt).toContain('file="assets/fonts/tiny.png"');
+
+ // And the package is self-consistent: nothing it staged names a path it lacks.
+ expect(res.warnings.filter((w) => /does not carry/.test(w))).toEqual([]);
+ });
+
+ // The backstop for the type nobody has wired up yet: whatever the document is,
+ // a path it names that the package does not carry is a 404 waiting to happen.
+ it('reports a staged document that names an asset the package lacks', async () => {
+ const lone = mkdtempSync(path.join(tmpdir(), 'estella-dangling-'));
+ const prev = root;
+ root = lone;
+ try {
+ writeAsset('assets/env/gone.esenv', 'environment', ESENV, JSON.stringify({
+ version: 1, irradiance: new Array(27).fill(0), specular: 'nowhere.png',
+ faceSize: 8, mipCount: 1, maxRange: 8,
+ }));
+ writeAsset('assets/scenes/main.esscene', 'scene', SCENE, JSON.stringify({
+ version: '1.0', name: 'm',
+ entities: [{
+ id: 1, name: 'Sky', parent: null, children: [],
+ components: [{ type: 'Light2D', data: { type: 2, environment: 'assets/env/gone.esenv' } }],
+ }],
+ }));
+ const res = await cookAssets(lone, { entryScenes: ['assets/scenes/main.esscene'], outDir: 'build' });
+ expect(res.warnings.some((w) => /gone\.esenv: names 'nowhere\.png'/.test(w))).toBe(true);
+ } finally {
+ root = prev;
+ rmSync(lone, { recursive: true, force: true });
+ }
});
});
diff --git a/pipeline/src/assets/cookAssets.ts b/pipeline/src/assets/cookAssets.ts
index 8af354565..3bebcb6a4 100644
--- a/pipeline/src/assets/cookAssets.ts
+++ b/pipeline/src/assets/cookAssets.ts
@@ -29,7 +29,8 @@ import { readTextureCookSettings } from '../project/importSettings';
// Single-source content hash (sdk/src/asset/contentHash.ts). Imported as source —
// no hand-mirrored copy — so the cook and the runtime agree by construction.
import { contentHashHex } from '../../../sdk/src/asset/contentHash';
-import { resolveRelativePath } from '../../../sdk/src/asset/documentRef';
+import { getAssetTypeEntry } from '../../../sdk/src/assetTypes';
+import { resolveRelativePath, resolveDocumentRef } from '../../../sdk/src/asset/documentRef';
// Single source for the folder→delivery-group model, shared with the editor Play
// realm (sdk/src/asset/assetGroups.ts) so cook and editor never disagree.
import { resolveAssetGroup, resolveAtlas, type AssetGroupsConfig } from '../../../sdk/src/asset/assetGroups';
@@ -257,6 +258,30 @@ function rewriteTilemapRefs(
return new TextEncoder().encode(JSON.stringify(json, null, 2) + '\n');
}
+/**
+ * Asset paths a staged document names that the package does not carry. Three
+ * defects of this shape were each a different type, and each was found by a game
+ * drawing the wrong thing — a document's own references being the one place
+ * nothing checked. Only KNOWN asset extensions count, so ordinary strings do not.
+ */
+function unresolvedDocumentRefs(
+ docPath: string,
+ text: string,
+ shipped: ReadonlySet<string>,
+): string[] {
+ const missing = new Set<string>();
+ for (const [, ref] of text.matchAll(/"([^"\n]{3,200})"/g)) {
+ // By EXTENSION, not by looksLikeAssetPath: that one wants a directory in the
+ // string, and a document naming a sibling — the exact shape of every defect
+ // this is here for — has none.
+ if (ref.includes('://') || !/\.[A-Za-z0-9]+$/.test(ref) || !getAssetTypeEntry(ref)) continue;
+ // Resolved the way a loader will: refs travel in LOGICAL space, so a sibling
+ // is a sibling of the document's project path, not of where staging put it.
+ if (!shipped.has(resolveDocumentRef(docPath, ref))) missing.add(ref);
+ }
+ return [...missing];
+}
+
export interface CookResult {
ok: boolean;
/** Absolute output dir the assets + manifest were staged into. */
@@ -691,6 +716,22 @@ export async function cookAssets(
}
manifestEntries.sort((a, b) => a.path.localeCompare(b.path));
+ // Is the package self-consistent? A document still naming a path the package
+ // lacks is a 404 the game shows as a missing texture or absent lighting rather
+ // than as an error — and the type it happens on need not be one anyone wired up.
+ const shipped = new Set(manifestEntries.map((e) => e.sourcePath));
+ for (const entry of manifestEntries) {
+ const type = getAssetTypeEntry(entry.sourcePath);
+ if (type?.contentType !== 'json' && type?.contentType !== 'text') continue;
+ let text: string;
+ try {
+ text = (await readFile(path.join(absOut, entry.path))).toString('utf8');
+ } catch { continue; }
+ for (const ref of unresolvedDocumentRefs(entry.sourcePath, text, shipped)) {
+ warnings.push(`${entry.sourcePath}: names '${ref}', which the package does not carry`);
+ }
+ }
+
const manifestPath = path.join(absOut, MANIFEST);
await writeFile(
manifestPath,