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: resolve absolute path stripping and event listener leaks on HMR
esengine7165e1622b44
diff --git a/desktop/src/native-fs.ts b/desktop/src/native-fs.tsindex 900fc1e16..d40817f5c 100644--- a/desktop/src/native-fs.ts+++ b/desktop/src/native-fs.ts@@ -23,6 +23,21 @@ import { resourceDir } from '@tauri-apps/api/path';import { open as shellOpen } from '@tauri-apps/plugin-shell';import type { NativeShell } from '@esengine/editor';+export function resolveFilePath(path: string): string {+ const normalized = path.replace(/\\/g, '/');+ const parts = normalized.split('/');+ const resolved: string[] = [];+ for (const part of parts) {+ if (part === '..') {+ resolved.pop();+ } else if (part && part !== '.') {+ resolved.push(part);+ }+ }+ const result = resolved.join('/');+ return normalized.startsWith('/') ? '/' + result : result;+}+export interface DirectoryEntry {name: string;isDirectory: boolean;@@ -182,14 +197,7 @@ export const nativeFS: NativeFS = {async readFile(path: string) {try {- const resolvedPath = path.replace(/\\/g, '/').split('/').reduce((acc, part) => {- if (part === '..') {- acc.pop();- } else if (part && part !== '.') {- acc.push(part);- }- return acc;- }, [] as string[]).join('/');+ const resolvedPath = resolveFilePath(path);return await readTextFile(resolvedPath);} catch (err) {const errStr = String(err);diff --git a/editor/src/DockLayoutManager.ts b/editor/src/DockLayoutManager.tsindex 570488475..a6702f4a6 100644--- a/editor/src/DockLayoutManager.ts+++ b/editor/src/DockLayoutManager.ts@@ -68,6 +68,7 @@ export class DockLayoutManager {private lastActivePanelId_: string | null = null;private detachContext_: DetachContext | null = null;private contextMenuHandler_: ((e: MouseEvent) => void) | null = null;+ private contextMenuContainer_: HTMLElement | null = null;private panelClosedUnlisten_: (() => void) | null = null;constructor(panelManager: PanelManager, store: EditorStore) {@@ -286,6 +287,7 @@ export class DockLayoutManager {};container.addEventListener('contextmenu', this.contextMenuHandler_);+ this.contextMenuContainer_ = container;}private resolvePanelIdFromTab(tab: HTMLElement): string | null {@@ -332,6 +334,11 @@ export class DockLayoutManager {clearTimeout(this.saveTimer_);this.saveTimer_ = null;}+ if (this.contextMenuHandler_ && this.contextMenuContainer_) {+ this.contextMenuContainer_.removeEventListener('contextmenu', this.contextMenuHandler_);+ this.contextMenuHandler_ = null;+ this.contextMenuContainer_ = null;+ }this.panelClosedUnlisten_?.();this.panelClosedUnlisten_ = null;this.activePanelDisposable_?.dispose();diff --git a/editor/src/Editor.ts b/editor/src/Editor.tsindex 98f4a9ecd..97cdcddf9 100644--- a/editor/src/Editor.ts+++ b/editor/src/Editor.ts@@ -108,6 +108,8 @@ export class Editor {private addressableWindow_: { element: HTMLElement; panel: AddressablePanel; keyHandler: (e: KeyboardEvent) => void } | null = null;private previewUrl_: string | null = null;private escapeHandler_: ((e: KeyboardEvent) => void) | null = null;+ private closeUnlisten_: (() => void) | null = null;+ private contextMenuHandler_: ((e: Event) => void) | null = null;private settingsDebounceTimer_: ReturnType<typeof setTimeout> | null = null;private mainWindowBridge_: MainWindowBridge | null = null;private windowManager_: WindowManager | null = null;@@ -1032,9 +1034,8 @@ export class Editor {getGlobalPathResolver().setProjectDir(projectDir);}- this.container_.addEventListener('contextmenu', (e) => {- e.preventDefault();- });+ this.contextMenuHandler_ = (e) => { e.preventDefault(); };+ this.container_.addEventListener('contextmenu', this.contextMenuHandler_);const previewUrlEl = this.container_.querySelector('.es-preview-url');previewUrlEl?.addEventListener('click', () => {@@ -1160,9 +1161,9 @@ export class Editor {}private setupCloseHandler(): void {- import('@tauri-apps/api/window').then(({ getCurrentWindow }) => {+ import('@tauri-apps/api/window').then(async ({ getCurrentWindow }) => {const mainWindow = getCurrentWindow();- mainWindow.onCloseRequested(async (event) => {+ this.closeUnlisten_ = await mainWindow.onCloseRequested(async (event) => {if (!this.store_.isDirty) return;event.preventDefault();try {@@ -1178,6 +1179,12 @@ export class Editor {}dispose(): void {+ this.closeUnlisten_?.();+ this.closeUnlisten_ = null;+ if (this.contextMenuHandler_) {+ this.container_.removeEventListener('contextmenu', this.contextMenuHandler_);+ this.contextMenuHandler_ = null;+ }if (this.addressableWindow_) {this.addressableWindow_.panel.dispose();this.addressableWindow_.element.remove();diff --git a/editor/src/__tests__/utils/path.test.ts b/editor/src/__tests__/utils/path.test.tsindex 61cb64816..aeefc5b83 100644--- a/editor/src/__tests__/utils/path.test.ts+++ b/editor/src/__tests__/utils/path.test.ts@@ -1,12 +1,14 @@import { describe, it, expect } from 'vitest';import {normalizePath,+ resolvePath,joinPath,getFileExtension,getDirName,getParentDir,isAbsolutePath,} from '../../utils/path';+import { resolveFilePath } from '../../../../desktop/src/native-fs';describe('Path utilities', () => {describe('normalizePath', () => {@@ -115,6 +117,51 @@ describe('Path utilities', () => {});});+ describe('resolvePath', () => {+ it('should preserve leading slash for absolute paths', () => {+ expect(resolvePath('/Users/yhh/Documents/Spaceshooter/src/main.ts'))+ .toBe('/Users/yhh/Documents/Spaceshooter/src/main.ts');+ });++ it('should resolve parent references in absolute paths', () => {+ expect(resolvePath('/Users/yhh/Documents/../Spaceshooter/src/main.ts'))+ .toBe('/Users/yhh/Spaceshooter/src/main.ts');+ });++ it('should resolve dot segments in absolute paths', () => {+ expect(resolvePath('/Users/yhh/./Documents/Spaceshooter/src/main.ts'))+ .toBe('/Users/yhh/Documents/Spaceshooter/src/main.ts');+ });++ it('should handle relative paths without adding a leading slash', () => {+ expect(resolvePath('src/main.ts')).toBe('src/main.ts');+ });++ it('should normalize backslashes', () => {+ expect(resolvePath('C:\\Users\\test\\file.ts')).toBe('C:/Users/test/file.ts');+ });+ });++ describe('resolveFilePath (desktop native-fs)', () => {+ it('should preserve leading slash for absolute paths', () => {+ expect(resolveFilePath('/Users/yhh/Documents/Spaceshooter/src/main.ts'))+ .toBe('/Users/yhh/Documents/Spaceshooter/src/main.ts');+ });++ it('should resolve parent references in absolute paths', () => {+ expect(resolveFilePath('/Users/yhh/Documents/../Spaceshooter/src/main.ts'))+ .toBe('/Users/yhh/Spaceshooter/src/main.ts');+ });++ it('should handle relative paths without adding a leading slash', () => {+ expect(resolveFilePath('src/main.ts')).toBe('src/main.ts');+ });++ it('should normalize backslashes', () => {+ expect(resolveFilePath('C:\\Users\\test\\file.ts')).toBe('C:/Users/test/file.ts');+ });+ });+describe('isAbsolutePath', () => {it('should recognize unix absolute paths', () => {expect(isAbsolutePath('/usr/local/bin')).toBe(true);