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.

fix(assets): adopting a file never replaces the sidecar its creator wrote

esenginefeff4f2a3c2f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 19745c7b0..06408150d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -355,6 +355,15 @@ published separately; it ships inside the editor.
### Fixed
+- **A new prefab, material, animation clip or graph keeps the id it was created
+ with.** Each was written file first and `.meta` second, and the project watcher
+ adopts a file it finds without a `.meta` by minting one of its own. When it looked
+ in between, the new asset was registered under an id nobody had been given — a
+ prefab just created could be missing from the asset list — and when its write
+ landed last, the id the editor had handed back referred to nothing on disk. New
+ assets now land their `.meta` first, through one door that mints it, and adopting
+ a file never replaces a `.meta` that appeared after it looked.
+
- **Play started while the editor was still preparing it gets a whole game.**
Opening a project prepares the play realm in the background, and preparing it
deletes the copy it is about to replace. A Play pressed before that finished
diff --git a/pipeline/src/assets/assetMeta.ts b/pipeline/src/assets/assetMeta.ts
index e6124f707..50a9e5e51 100644
--- a/pipeline/src/assets/assetMeta.ts
+++ b/pipeline/src/assets/assetMeta.ts
@@ -67,14 +67,6 @@ export function mintMeta(type: string, importer?: Record<string, unknown>): Reco
};
}
-/** Write `<absFile>.meta` for @p absFile. Pass `type` to override the
- * extension-derived one (the create door knows its type explicitly). */
-export async function writeMeta(absFile: string, type: string,
- importer?: Record<string, unknown>): Promise<void> {
- await writeFile(absFile + META_EXT,
- JSON.stringify(mintMeta(type, importer), null, 2) + '\n', 'utf8');
-}
-
/**
* Adopt an orphan: mint `<absFile>.meta` iff the file has none and it resolves
* to a known asset type. Returns what happened, so callers can count adoptions /
@@ -85,6 +77,14 @@ export async function adoptOrphan(absFile: string,
if (existsSync(absFile + META_EXT)) return 'has-meta';
const type = await metaTypeForFile(absFile);
if (!type) return 'unknown-type';
- await writeMeta(absFile, type, importer);
+ // Exclusive: the file's creator may have written its sidecar since the check, and
+ // the uuid it handed out is the one refs point at.
+ try {
+ await writeFile(absFile + META_EXT, JSON.stringify(mintMeta(type, importer), null, 2) + '\n',
+ { encoding: 'utf8', flag: 'wx' });
+ } catch (err) {
+ if ((err as NodeJS.ErrnoException).code === 'EEXIST') return 'has-meta';
+ throw err;
+ }
return 'adopted';
}
diff --git a/pipeline/tests/adopt-orphan-race.test.ts b/pipeline/tests/adopt-orphan-race.test.ts
new file mode 100644
index 000000000..22fa280c8
--- /dev/null
+++ b/pipeline/tests/adopt-orphan-race.test.ts
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file adopt-orphan-race.test.ts
+ * @brief Adopting a file never replaces the sidecar its creator wrote.
+ *
+ * A scan that saw a new file before its `.meta` minted one of its own; when the
+ * creator's `.meta` landed in between, the scan's overwrote it, and the uuid the
+ * creator had handed back referred to nothing on disk.
+ */
+import { describe, it, expect, beforeEach, afterEach } from 'vitest';
+import { mkdtemp, rm, writeFile, readFile } from 'node:fs/promises';
+import { writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import path from 'node:path';
+import { adoptOrphan } from '../src/assets/assetMeta';
+
+let dir: string;
+beforeEach(async () => { dir = await mkdtemp(path.join(tmpdir(), 'es-adopt-')); });
+afterEach(async () => { await rm(dir, { recursive: true, force: true }); });
+
+describe('adopting an orphan', () => {
+ it('leaves a sidecar that appeared after it looked', async () => {
+ const file = path.join(dir, 'Enemy.esprefab');
+ await writeFile(file, '{}');
+ const adopting = adoptOrphan(file);
+ const creators = JSON.stringify({ uuid: 'c0ffee00-0000-4000-8000-000000000000', version: '2.0', type: 'prefab', importer: {} });
+ writeFileSync(`${file}.meta`, creators);
+ expect(await adopting).toBe('has-meta');
+ expect(await readFile(`${file}.meta`, 'utf8')).toBe(creators);
+ });
+
+ it('still mints one for a file that has none', async () => {
+ const file = path.join(dir, 'Enemy.esprefab');
+ await writeFile(file, '{}');
+ expect(await adoptOrphan(file)).toBe('adopted');
+ expect(JSON.parse(await readFile(`${file}.meta`, 'utf8')).type).toBe('prefab');
+ });
+});