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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(export): prune obsolete cooked assets and packs
yhh0b706120b24f
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex d54505fab..b150dc766 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,6 +16,8 @@ published separately; it ships inside the editor.### Fixed+- **Repeated exports remove obsolete cooked assets and packs.** Web and mini-game exports compare the previous and current asset manifests, deleting old texture hashes, removed assets, and packs no longer referenced by the build. Shared current packs, project sources, and unlisted output files are preserved.+- **Repeated Web exports drop obsolete runtime files.** Rebuilding into the same output replaces the shipped SDK and WASM trees, removing modules the project no longer needs and old SDK chunks. Turning off source maps removes maps from the previous build. Output-root configuration files are preserved, and a runtime output overlapping its input is rejected before removing that tree.- **Editor export results report failed builds accurately.** The automation export door returns `exported: false` when the exporter fails, retaining errors such as missing optional runtime modules instead of marking a partial package as successfully exported. Preflight refusals remain distinguishable from attempted builds.diff --git a/pipeline/src/export/assetCleanup.ts b/pipeline/src/export/assetCleanup.tsnew file mode 100644index 000000000..918253548--- /dev/null+++ b/pipeline/src/export/assetCleanup.ts@@ -0,0 +1,41 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+import { readFile, rm } from 'node:fs/promises';+import path from 'node:path';+import type { AddressableManifest } from 'esengine';+import { containsPath, isInsideRoot, realPathOf } from '../fs/pathSandbox';++/** Physical files owned by the exported manifest; shared pack files count once. */+export async function exportedAssetFiles(output: string): Promise<Set<string>> {+ let manifest: AddressableManifest;+ try {+ manifest = JSON.parse(await readFile(path.join(output, 'asset-manifest.json'), 'utf8'));+ } catch (err) {+ if ((err as NodeJS.ErrnoException).code === 'ENOENT' || err instanceof SyntaxError) return new Set();+ throw err;+ }+ const files = new Set<string>();+ const base = path.resolve(output);+ for (const group of Object.values(manifest.groups ?? {})) {+ for (const asset of Object.values(group.assets ?? {})) {+ const file = asset.pack?.file ?? asset.path;+ if (typeof file !== 'string' || !file || path.isAbsolute(file) || /^[a-z]+:/i.test(file)) continue;+ const target = path.resolve(output, file);+ if (target !== base && containsPath(base, target)) files.add(target);+ }+ }+ return files;+}++/** Remove only previously shipped asset files absent from the current manifest. */+export async function pruneExportedAssets(output: string, project: string, previous: ReadonlySet<string>): Promise<void> {+ const current = await exportedAssetFiles(output);+ const stale = [...previous].filter((file) => !current.has(file));+ if (stale.length === 0) return;+ if (containsPath(realPathOf(output), realPathOf(project))) {+ throw new Error('asset cleanup requires a build output separate from the project source');+ }+ for (const file of stale) {+ if (isInsideRoot(output, file)) await rm(file, { force: true });+ }+}diff --git a/pipeline/src/export/exportGame.ts b/pipeline/src/export/exportGame.tsindex 25ad84fdb..0625a7196 100644--- a/pipeline/src/export/exportGame.ts+++ b/pipeline/src/export/exportGame.ts@@ -31,6 +31,7 @@ import { cookAssets, type CookManifest, type Inclusion } from '../assets/cookAssimport { cookWorlds, streamedScenes } from '../world/cookWorld';import { buildAddressableManifest } from '../assets/addressableManifest';import { packAssets } from '../assets/assetPacks';+import { exportedAssetFiles, pruneExportedAssets } from './assetCleanup';import type { DebugChannelConfig, PackagedGameConfig } from 'esengine';import { packagedDebugChannel } from './debugChannel';import { packagedHotUpdate } from './hotUpdateConfig';@@ -896,6 +897,7 @@ async function produceExport(opts: ExportGameOptions): Promise<ExportGameResult>/** Desktop, Steam channel: the per-build checklist written beside the scripts. */let steamChecklist: string | undefined;await mkdir(payloadDir, { recursive: true });+ const previousAssets = await exportedAssetFiles(payloadDir);// One answer for the whole package: the bundles esbuild writes here and the// SDK tree copied in beside them.const sourceMaps = opts.sourcemap ?? false;@@ -972,6 +974,7 @@ async function produceExport(opts: ExportGameOptions): Promise<ExportGameResult>}}await writeFile(path.join(payloadDir, 'asset-manifest.json'), await buildAddressableManifest(payloadDir));+ if (cook.failed.length === 0) await pruneExportedAssets(payloadDir, opts.root, previousAssets);// A scene that declared itself streamed is cut here, once its assets are staged// and its references are in the form the runtime resolves. The entry scene it// leaves behind is the PERSISTENT world; the places arrive by residency.diff --git a/pipeline/src/export/exportMiniGame.ts b/pipeline/src/export/exportMiniGame.tsindex 7a180ae4d..7ab4090ea 100644--- a/pipeline/src/export/exportMiniGame.ts+++ b/pipeline/src/export/exportMiniGame.ts@@ -50,6 +50,7 @@ import { esengineAlias } from '../bundle/esengineResolve';import { officialPackagesPlugin } from '../bundle/officialPackages';import { explainBundleErrors, type BundleMessage } from '../bundle/bundleDiagnostics';import { scanSideModuleIds, sideModuleFiles, textureDecoderBytes } from '../bundle/sideModuleScan';+import { exportedAssetFiles, pruneExportedAssets } from './assetCleanup';import { OPEN_DATA_DIR, isEsModule } from './miniGameExportProfile';import type { RpkSigningKey } from './rpk';import { loadProjectModules, sideModuleDeclarations, stageProjectModules } from './projectModules';@@ -360,6 +361,7 @@ export async function exportMiniGame(profile: MiniGameExportProfile, opts: {}await mkdir(absOut, { recursive: true });+ const previousAssets = await exportedAssetFiles(absOut);// Every switchable scene ships; the exporter's caller (exportGame) discovers// them from the project's scenes dir. Absent (tests / direct calls): entry only.@@ -450,6 +452,7 @@ export async function exportMiniGame(profile: MiniGameExportProfile, opts: {progress({ phase: 'Building manifest' });try {await writeFile(path.join(absOut, 'asset-manifest.json'), await buildAddressableManifest(absOut));+ if (cook.failed.length === 0) await pruneExportedAssets(absOut, opts.root, previousAssets);await rm(path.join(absOut, 'assets.manifest.json'), { force: true });} catch (err) {errors.push(`manifest: ${err instanceof Error ? err.message : String(err)}`);diff --git a/pipeline/tests/export-asset-cleanup.test.ts b/pipeline/tests/export-asset-cleanup.test.tsnew file mode 100644index 000000000..92b23485f--- /dev/null+++ b/pipeline/tests/export-asset-cleanup.test.ts@@ -0,0 +1,52 @@+// SPDX-License-Identifier: Apache-2.0+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team+import { describe, expect, it } from 'vitest';+import { mkdtemp, mkdir, readFile, rm, symlink, writeFile } from 'node:fs/promises';+import os from 'node:os';+import path from 'node:path';+import { exportedAssetFiles, pruneExportedAssets } from '../src/export/assetCleanup';++describe('exported asset ownership', () => {+ it('does not follow an output directory link to delete project files', async () => {+ const root = await mkdtemp(path.join(os.tmpdir(), 'export-link-'));+ const out = path.join(root, 'dist'), source = path.join(root, 'source');+ try {+ await mkdir(out); await mkdir(source);+ await writeFile(path.join(source, 'texture.bin'), 'SOURCE');+ await symlink(source, path.join(out, 'linked'), 'junction');+ await pruneExportedAssets(out, root, new Set([path.join(out, 'linked/texture.bin')]));+ expect(await readFile(path.join(source, 'texture.bin'), 'utf8')).toBe('SOURCE');+ } finally { await rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 50 }); }+ });++ it('keeps shared current packs and ignores URLs and paths outside the output', async () => {+ const root = await mkdtemp(path.join(os.tmpdir(), 'export-cleanup-'));+ const out = path.join(root, 'dist');+ try {+ await mkdir(out);+ await writeFile(path.join(root, 'source.txt'), 'SOURCE');+ await writeFile(path.join(out, 'shared.pack'), 'PACK');+ await writeFile(path.join(out, 'old.bin'), 'OLD');+ const manifest = (assets: object) => writeFile(path.join(out, 'asset-manifest.json'), JSON.stringify({ groups: { main: { assets } } }));+ await manifest({ a: { path: 'gone.bin', pack: { file: 'shared.pack' } }, b: { path: 'old.bin' },+ c: { path: '../source.txt' }, d: { path: 'https://cdn.example/a.bin' }, e: { path: '.' } });+ const previous = await exportedAssetFiles(out);+ expect(previous.size).toBe(2);+ await manifest({ remaining: { path: 'new.bin', pack: { file: 'shared.pack' } } });+ await pruneExportedAssets(out, root, previous);+ expect(await readFile(path.join(out, 'shared.pack'), 'utf8')).toBe('PACK');+ expect(await readFile(path.join(root, 'source.txt'), 'utf8')).toBe('SOURCE');+ await expect(readFile(path.join(out, 'old.bin'))).rejects.toMatchObject({ code: 'ENOENT' });+ } finally { await rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 50 }); }+ });++ it('refuses asset deletion when the output contains the project source', async () => {+ const root = await mkdtemp(path.join(os.tmpdir(), 'export-overlap-'));+ try {+ const source = path.join(root, 'source.bin');+ await writeFile(source, 'SOURCE');+ await expect(pruneExportedAssets(root, root, new Set([source]))).rejects.toThrow(/separate from the project source/);+ expect(await readFile(source, 'utf8')).toBe('SOURCE');+ } finally { await rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 50 }); }+ });+});diff --git a/pipeline/tests/export-side-modules.test.ts b/pipeline/tests/export-side-modules.test.tsindex 9b2e95106..d88a87d0d 100644--- a/pipeline/tests/export-side-modules.test.ts+++ b/pipeline/tests/export-side-modules.test.ts@@ -5,7 +5,7 @@* ask for, and no others. A dropped module is a 404 at boot.*/import { describe, it, expect } from 'vitest';-import { mkdtempSync, mkdirSync, writeFileSync, existsSync, rmSync } from 'node:fs';+import { mkdtempSync, mkdirSync, writeFileSync, readFileSync, existsSync, rmSync } from 'node:fs';import { tmpdir } from 'node:os';import path from 'node:path';import { fileURLToPath } from 'node:url';@@ -53,10 +53,12 @@ const run = (f: Fixture,runtime?: Parameters<typeof exportGame>[0]['runtime'],features?: Parameters<typeof exportGame>[0]['features'],+ packAssets?: boolean,) => exportGame({root: f.root, entryScene: 'scenes/main.esscene', hostsDir: HOSTS, packagesDir: OFFICIAL_PACKAGES,sdkDistDir: path.join(f.root, '_sdk'), wasmDir: path.join(f.root, '_wasm'),outDir: f.out, ...(runtime ? { runtime } : {}), ...(features ? { features } : {}),+ ...(packAssets === undefined ? {} : { packAssets }),});/** The module artifact base names the package actually carries. */@@ -68,6 +70,56 @@ const entities = (...components: Array<{ type: string; data?: unknown }>) =>[{ id: 0, components }];describe('web package side modules', () => {+ it('removes old texture hashes after editing and unreferencing a sprite texture', async () => {+ const tex = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa';+ const f = setup({ version: '1.0', name: 'Main', entities: entities({ type: 'Sprite', data: { texture: `@uuid:${tex}` } }) }, (root) => {+ mkdirSync(path.join(root, 'assets'));+ writeFileSync(path.join(root, 'assets/hero.png'), 'PNGDATA');+ writeFileSync(path.join(root, 'assets/hero.png.meta'), meta(tex, 'texture'));+ });+ const texturePath = () => JSON.parse(readFileSync(path.join(f.out, 'asset-manifest.json'), 'utf8')).groups.main.assets[tex].path as string;+ try {+ expect((await run(f)).ok).toBe(true);+ const first = texturePath();+ writeFileSync(path.join(f.out, 'host-config.json'), 'KEEP');+ writeFileSync(path.join(f.out, 'assets/custom.txt'), 'KEEP');+ writeFileSync(path.join(f.root, 'assets/hero.png'), 'CHANGEDPNGDATA');+ expect((await run(f)).ok).toBe(true);+ const second = texturePath();+ expect(second).not.toBe(first);+ expect(existsSync(path.join(f.out, first))).toBe(false);+ expect(existsSync(path.join(f.out, second))).toBe(true);+ writeFileSync(path.join(f.root, 'scenes/main.esscene'), JSON.stringify({ version: '1.0', entities: [] }));+ expect((await run(f)).ok).toBe(true);+ expect(existsSync(path.join(f.out, second))).toBe(false);+ expect(existsSync(path.join(f.out, 'assets/custom.txt'))).toBe(true);+ expect(existsSync(path.join(f.out, 'host-config.json'))).toBe(true);+ expect(readFileSync(path.join(f.root, 'assets/hero.png'), 'utf8')).toBe('CHANGEDPNGDATA');+ } finally { rmSync(f.root, { recursive: true, force: true, maxRetries: 10, retryDelay: 50 }); }+ }, 60_000);++ it('removes an old pack when exporting its materials as separate files', async () => {+ const a = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', b = 'bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb';+ const f = setup({ version: '1.0', entities: [{ id: 0, components: [{ type: 'Sprite', data: { material: `@uuid:${a}` } }] },+ { id: 1, components: [{ type: 'Sprite', data: { material: `@uuid:${b}` } }] }] }, (root) => {+ mkdirSync(path.join(root, 'assets'));+ for (const [name, uuid, value] of [['a', a, 1], ['b', b, 2]]) {+ writeFileSync(path.join(root, `assets/${name}.esmaterial`), JSON.stringify({ shader: 'builtin:sprite', properties: { value } }));+ writeFileSync(path.join(root, `assets/${name}.esmaterial.meta`), meta(uuid as string, 'material'));+ }+ });+ try {+ expect((await run(f)).ok).toBe(true);+ const old = JSON.parse(readFileSync(path.join(f.out, 'asset-manifest.json'), 'utf8')).groups.main.assets[a].pack.file as string;+ expect(existsSync(path.join(f.out, old))).toBe(true);+ expect((await run(f, undefined, undefined, false)).ok).toBe(true);+ expect(existsSync(path.join(f.out, old))).toBe(false);+ const current = JSON.parse(readFileSync(path.join(f.out, 'asset-manifest.json'), 'utf8')).groups.main.assets[a];+ expect(current.pack).toBeUndefined();+ expect(existsSync(path.join(f.out, current.path))).toBe(true);+ } finally { rmSync(f.root, { recursive: true, force: true, maxRetries: 10, retryDelay: 50 }); }+ }, 60_000);+it('drops a removed solver and preserves output-root files on re-export', async () => {const f = setup({ version: '1.0', name: 'Main', entities: entities({ type: 'RigidBody2D', data: {} }) });try {diff --git a/pipeline/tests/export-wechat.test.ts b/pipeline/tests/export-wechat.test.tsindex 2f915aa52..da5f3d097 100644--- a/pipeline/tests/export-wechat.test.ts+++ b/pipeline/tests/export-wechat.test.ts@@ -64,6 +64,27 @@ beforeAll(() => {afterAll(() => rmSync(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 50 }));describe('exportGame (wechat)', () => {+ it('removes the old texture file when re-exporting changed art', async () => {+ const dir = path.join(root, 'dist-wechat-recook');+ const source = path.join(root, 'assets/hero.png');+ const original = readFileSync(source);+ const run = () => exportGame({ root, entryScene: 'scenes/main.esscene',+ hostsDir: 'unused-for-wechat', packagesDir: OFFICIAL_PACKAGES,+ sdkDistDir: path.join(root, '_sdk'), wasmDir: path.join(root, '_wxwasm'), outDir: dir, platform: 'wechat' });+ try {+ expect((await run()).ok).toBe(true);+ const old = JSON.parse(readFileSync(path.join(dir, 'asset-manifest.json'), 'utf8')).groups.main.assets[TEX].path as string;+ writeFileSync(path.join(dir, 'project.private.config.json'), 'KEEP');+ writeFileSync(source, 'CHANGEDPNGDATA');+ expect((await run()).ok).toBe(true);+ const current = JSON.parse(readFileSync(path.join(dir, 'asset-manifest.json'), 'utf8')).groups.main.assets[TEX].path as string;+ expect(current).not.toBe(old);+ expect(existsSync(path.join(dir, old))).toBe(false);+ expect(existsSync(path.join(dir, current))).toBe(true);+ expect(readFileSync(path.join(dir, 'project.private.config.json'), 'utf8')).toBe('KEEP');+ } finally { writeFileSync(source, original); }+ }, 60_000);+it('assembles a MiniGame matching the initWeChatRuntime contract', async () => {const res = await exportGame({root,diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex c6f98b609..ab0c2433d 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';* the reader recognises anyway.*/export const NOTED = {+ 'fix(export): prune obsolete cooked assets and packs':+ { note: 'Repeated exports remove obsolete cooked assets and packs.' },'fix(export): remove obsolete web runtime files on rebuild':{ note: 'Repeated Web exports drop obsolete runtime files.' },'fix(editor): report failed exports accurately':