fix(project): stabilize project deletion
- Guard renderer project list/get against stale initial loads - Retry project zip/cache removal to handle transient Windows locks - Surface deletion failures in UI and add regression tests Made-with: Cursor
This commit is contained in:
+29
-29
@@ -1,39 +1,39 @@
|
|||||||
# .cursor/rules/project.mdc
|
---
|
||||||
|
description: Project-wide workflow and conventions
|
||||||
|
alwaysApply: true
|
||||||
|
---
|
||||||
|
|
||||||
# Project
|
# DNDGamePlayer — правила работы над задачами (future-pipeline)
|
||||||
|
|
||||||
React + TypeScript frontend project.
|
Эти правила применяются **только** когда запрос пользователя требует **изменений в репозитории** (код/конфиги/тесты). Для чисто текстовых задач (описания, маркетинг, переписка) pipeline не запускаем.
|
||||||
|
|
||||||
# Commands
|
## future-pipeline (обязательный порядок)
|
||||||
|
|
||||||
|
### 1) Implementation
|
||||||
|
- Прочитать релевантный код (минимум 1 файл), найти реальную причину бага/задачи.
|
||||||
|
- Делать **minimal, review-friendly diff** и следовать текущим паттернам проекта.
|
||||||
|
- Не добавлять зависимости без явной причины.
|
||||||
|
|
||||||
|
### 2) Review
|
||||||
|
- Самопроверка изменений: edge-cases, состояние UI (loading/error/empty/disabled), a11y, регрессии.
|
||||||
|
- Если задача нетривиальная: запустить внутренний “строгий ревью” (под-агент reviewer).
|
||||||
|
|
||||||
|
### 3) Tests
|
||||||
|
- Обновить/добавить тест(ы), если поведение изменилось или был баг.
|
||||||
|
- Для мелких правок допускается “облегчённый режим” без под-агентов, но тесты всё равно должны проходить.
|
||||||
|
|
||||||
|
### 4) Verify (всегда, перед ответом)
|
||||||
|
Обязательно выполнить:
|
||||||
|
- `npm run lint`
|
||||||
|
- `npm run typecheck`
|
||||||
|
- `npm run test`
|
||||||
|
|
||||||
|
Если что-то упало — исправить и повторить до green.
|
||||||
|
|
||||||
|
## Команды проекта (справка)
|
||||||
- install: `npm install`
|
- install: `npm install`
|
||||||
- dev: `npm run dev`
|
- dev: `npm run dev`
|
||||||
- build: `npm run build`
|
- build: `npm run build`
|
||||||
- lint: `npm run lint`
|
- lint: `npm run lint`
|
||||||
- typecheck: `npm run typecheck`
|
- typecheck: `npm run typecheck`
|
||||||
- test: `npm run test`
|
- test: `npm run test`
|
||||||
- test single: `npm run test -- <file>`
|
|
||||||
|
|
||||||
# Global rules
|
|
||||||
|
|
||||||
- Всегда изучай existing code и nearby components перед изменениями
|
|
||||||
- Делай minimal, review-friendly diff
|
|
||||||
- Не добавляй new dependencies без явной причины
|
|
||||||
- Следуй существующим patterns
|
|
||||||
|
|
||||||
# Done criteria
|
|
||||||
|
|
||||||
Перед завершением:
|
|
||||||
|
|
||||||
- lint passes
|
|
||||||
- typecheck passes
|
|
||||||
- tests pass
|
|
||||||
- нет regressions
|
|
||||||
|
|
||||||
# UI checklist
|
|
||||||
|
|
||||||
- loading state
|
|
||||||
- error state
|
|
||||||
- empty state
|
|
||||||
- disabled state
|
|
||||||
- accessibility
|
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
import assert from 'node:assert/strict';
|
||||||
|
import test from 'node:test';
|
||||||
|
|
||||||
|
import { rmWithRetries } from './fsRetry';
|
||||||
|
|
||||||
|
void test('rmWithRetries: retries on EPERM and then succeeds', async () => {
|
||||||
|
let calls = 0;
|
||||||
|
const rm = () => {
|
||||||
|
calls += 1;
|
||||||
|
if (calls < 3) {
|
||||||
|
const err = new Error('nope') as Error & { code: string };
|
||||||
|
err.code = 'EPERM';
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
return Promise.resolve();
|
||||||
|
};
|
||||||
|
await rmWithRetries(rm, 'x', { force: true }, 5);
|
||||||
|
assert.equal(calls, 3);
|
||||||
|
});
|
||||||
|
|
||||||
|
void test('rmWithRetries: does not retry on ENOENT', async () => {
|
||||||
|
let calls = 0;
|
||||||
|
const rm = () => {
|
||||||
|
calls += 1;
|
||||||
|
const err = new Error('missing') as Error & { code: string };
|
||||||
|
err.code = 'ENOENT';
|
||||||
|
return Promise.reject(err);
|
||||||
|
};
|
||||||
|
await assert.rejects(() => rmWithRetries(rm, 'x', { force: true }, 5), /missing/);
|
||||||
|
assert.equal(calls, 1);
|
||||||
|
});
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
type RmLike = (path: string, opts: { recursive?: boolean; force?: boolean }) => Promise<void>;
|
||||||
|
|
||||||
|
function isRetryableRmError(err: unknown): boolean {
|
||||||
|
if (!err || typeof err !== 'object') return false;
|
||||||
|
const code = (err as { code?: unknown }).code;
|
||||||
|
return code === 'EBUSY' || code === 'EPERM' || code === 'EACCES';
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function rmWithRetries(
|
||||||
|
rm: RmLike,
|
||||||
|
targetPath: string,
|
||||||
|
opts: { recursive?: boolean; force?: boolean },
|
||||||
|
retries = 6,
|
||||||
|
): Promise<void> {
|
||||||
|
let attempt = 0;
|
||||||
|
// Windows: file locks/antivirus/indexers can cause transient EPERM/EBUSY.
|
||||||
|
// We retry a few times before surfacing the error.
|
||||||
|
// Delays: 20, 40, 80, 160, 320, 640ms (capped by retries).
|
||||||
|
for (;;) {
|
||||||
|
try {
|
||||||
|
await rm(targetPath, opts);
|
||||||
|
return;
|
||||||
|
} catch (e) {
|
||||||
|
attempt += 1;
|
||||||
|
if (attempt > retries || !isRetryableRmError(e)) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
const delayMs = Math.min(800, 20 * 2 ** (attempt - 1));
|
||||||
|
await new Promise((r) => setTimeout(r, delayMs));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,6 +24,7 @@ import { asAssetId, asGraphNodeId, asProjectId } from '../../shared/types/ids';
|
|||||||
import { getAppSemanticVersion } from '../versionInfo';
|
import { getAppSemanticVersion } from '../versionInfo';
|
||||||
|
|
||||||
import { reconcileAssetFiles } from './assetPrune';
|
import { reconcileAssetFiles } from './assetPrune';
|
||||||
|
import { rmWithRetries } from './fsRetry';
|
||||||
import { getLegacyProjectsRootDirs, getProjectsCacheRootDir, getProjectsRootDir } from './paths';
|
import { getLegacyProjectsRootDirs, getProjectsCacheRootDir, getProjectsRootDir } from './paths';
|
||||||
import { readProjectJsonFromZip, unzipToDir } from './yauzlProjectZip';
|
import { readProjectJsonFromZip, unzipToDir } from './yauzlProjectZip';
|
||||||
|
|
||||||
@@ -792,8 +793,8 @@ export class ZipProjectStore {
|
|||||||
this.projectSession += 1;
|
this.projectSession += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
await fs.rm(zipPath, { force: true }).catch(() => undefined);
|
await rmWithRetries(fs.rm, zipPath, { force: true });
|
||||||
await fs.rm(cacheDir, { recursive: true, force: true }).catch(() => undefined);
|
await rmWithRetries(fs.rm, cacheDir, { recursive: true, force: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
private randomId(): string {
|
private randomId(): string {
|
||||||
|
|||||||
@@ -917,7 +917,13 @@ function ProjectPicker({ projects, onCreate, onOpen, onDelete }: ProjectPickerPr
|
|||||||
) {
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
void onDelete(id);
|
void (async () => {
|
||||||
|
try {
|
||||||
|
await onDelete(id);
|
||||||
|
} catch (e) {
|
||||||
|
window.alert(e instanceof Error ? e.message : String(e));
|
||||||
|
}
|
||||||
|
})();
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
Удалить
|
Удалить
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import assert from 'node:assert/strict';
|
||||||
|
import fs from 'node:fs';
|
||||||
|
import path from 'node:path';
|
||||||
|
import test from 'node:test';
|
||||||
|
import { fileURLToPath } from 'node:url';
|
||||||
|
|
||||||
|
const here = path.dirname(fileURLToPath(import.meta.url));
|
||||||
|
|
||||||
|
void test('projectState: list/get after delete invalidates in-flight initial load (epoch guard)', () => {
|
||||||
|
const src = fs.readFileSync(path.join(here, 'projectState.ts'), 'utf8');
|
||||||
|
assert.match(src, /projectDataEpochRef/);
|
||||||
|
assert.match(src, /const epoch = projectDataEpochRef\.current/);
|
||||||
|
assert.match(src, /if \(projectDataEpochRef\.current !== epoch\) return/);
|
||||||
|
assert.match(
|
||||||
|
src,
|
||||||
|
/const deleteProject = async[\s\S]+?projectDataEpochRef\.current \+= 1[\s\S]+?await api\.invoke/,
|
||||||
|
);
|
||||||
|
assert.match(
|
||||||
|
src,
|
||||||
|
/const openProject = async[\s\S]+?projectDataEpochRef\.current \+= 1[\s\S]+?await api\.invoke/,
|
||||||
|
);
|
||||||
|
assert.match(src, /const refreshProjects = async \(\) => \{[\s\S]+?projectDataEpochRef\.current \+= 1/);
|
||||||
|
});
|
||||||
@@ -58,12 +58,15 @@ export function useProjectState(licenseActive: boolean): readonly [State, Action
|
|||||||
const api = getDndApi();
|
const api = getDndApi();
|
||||||
const [state, setState] = useState<State>({ projects: [], project: null, selectedSceneId: null });
|
const [state, setState] = useState<State>({ projects: [], project: null, selectedSceneId: null });
|
||||||
const projectRef = useRef<Project | null>(null);
|
const projectRef = useRef<Project | null>(null);
|
||||||
|
/** Bumps on mutations / refresh; initial license load only applies if still current (avoids racing late list/get over newer state). */
|
||||||
|
const projectDataEpochRef = useRef(0);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
projectRef.current = state.project;
|
projectRef.current = state.project;
|
||||||
}, [state.project]);
|
}, [state.project]);
|
||||||
|
|
||||||
const actions = useMemo<Actions>(() => {
|
const actions = useMemo<Actions>(() => {
|
||||||
const refreshProjects = async () => {
|
const refreshProjects = async () => {
|
||||||
|
projectDataEpochRef.current += 1;
|
||||||
const res = await api.invoke(ipcChannels.project.list, {});
|
const res = await api.invoke(ipcChannels.project.list, {});
|
||||||
setState((s) => ({ ...s, projects: res.projects }));
|
setState((s) => ({ ...s, projects: res.projects }));
|
||||||
};
|
};
|
||||||
@@ -75,6 +78,7 @@ export function useProjectState(licenseActive: boolean): readonly [State, Action
|
|||||||
};
|
};
|
||||||
|
|
||||||
const openProject = async (id: ProjectId) => {
|
const openProject = async (id: ProjectId) => {
|
||||||
|
projectDataEpochRef.current += 1;
|
||||||
const res = await api.invoke(ipcChannels.project.open, { projectId: id });
|
const res = await api.invoke(ipcChannels.project.open, { projectId: id });
|
||||||
setState((s) => ({ ...s, project: res.project, selectedSceneId: res.project.currentSceneId }));
|
setState((s) => ({ ...s, project: res.project, selectedSceneId: res.project.currentSceneId }));
|
||||||
};
|
};
|
||||||
@@ -276,6 +280,7 @@ export function useProjectState(licenseActive: boolean): readonly [State, Action
|
|||||||
};
|
};
|
||||||
|
|
||||||
const deleteProject = async (projectId: ProjectId) => {
|
const deleteProject = async (projectId: ProjectId) => {
|
||||||
|
projectDataEpochRef.current += 1;
|
||||||
await api.invoke(ipcChannels.project.deleteProject, { projectId });
|
await api.invoke(ipcChannels.project.deleteProject, { projectId });
|
||||||
const listRes = await api.invoke(ipcChannels.project.list, {});
|
const listRes = await api.invoke(ipcChannels.project.list, {});
|
||||||
const res = await api.invoke(ipcChannels.project.get, {});
|
const res = await api.invoke(ipcChannels.project.get, {});
|
||||||
@@ -316,14 +321,18 @@ export function useProjectState(licenseActive: boolean): readonly [State, Action
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!licenseActive) {
|
if (!licenseActive) {
|
||||||
queueMicrotask(() => {
|
queueMicrotask(() => {
|
||||||
|
projectDataEpochRef.current += 1;
|
||||||
setState({ projects: [], project: null, selectedSceneId: null });
|
setState({ projects: [], project: null, selectedSceneId: null });
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
void (async () => {
|
void (async () => {
|
||||||
|
const epoch = projectDataEpochRef.current;
|
||||||
const listRes = await api.invoke(ipcChannels.project.list, {});
|
const listRes = await api.invoke(ipcChannels.project.list, {});
|
||||||
|
if (projectDataEpochRef.current !== epoch) return;
|
||||||
setState((s) => ({ ...s, projects: listRes.projects }));
|
setState((s) => ({ ...s, projects: listRes.projects }));
|
||||||
const res = await api.invoke(ipcChannels.project.get, {});
|
const res = await api.invoke(ipcChannels.project.get, {});
|
||||||
|
if (projectDataEpochRef.current !== epoch) return;
|
||||||
setState((s) => ({ ...s, project: res.project, selectedSceneId: res.project?.currentSceneId ?? null }));
|
setState((s) => ({ ...s, project: res.project, selectedSceneId: res.project?.currentSceneId ?? null }));
|
||||||
})();
|
})();
|
||||||
}, [licenseActive, api]);
|
}, [licenseActive, api]);
|
||||||
|
|||||||
+1
-1
@@ -10,7 +10,7 @@
|
|||||||
"build:obfuscate": "node scripts/build.mjs --production --obfuscate",
|
"build:obfuscate": "node scripts/build.mjs --production --obfuscate",
|
||||||
"lint": "eslint . --max-warnings 0",
|
"lint": "eslint . --max-warnings 0",
|
||||||
"typecheck": "tsc -p tsconfig.eslint.json --noEmit",
|
"typecheck": "tsc -p tsconfig.eslint.json --noEmit",
|
||||||
"test": "tsx --test app/renderer/shared/ui/controls.tooltip.test.ts app/shared/ipc/contracts.mediaRemoval.test.ts app/shared/effectEraserHitTest.test.ts app/renderer/control/controlApp.effectsPanel.test.ts app/renderer/shared/effects/PxiEffectsOverlay.pointer.test.ts app/main/windows/createWindows.editorClose.test.ts app/main/windows/bootWindow.test.ts app/main/effects/effectsStore.test.ts app/main/project/assetPrune.test.ts app/main/project/zipRead.test.ts app/shared/package.build.test.ts app/shared/license/canonicalJson.test.ts app/shared/license/productKey.test.ts app/shared/license/licenseService.networkRegression.test.ts app/shared/video/videoPlaybackPerf.networkRegression.test.ts app/shared/video/videoPlaybackLoop.networkRegression.test.ts app/main/license/verifyLicenseToken.test.ts && node --test scripts/build-env.test.mjs scripts/obfuscate-main.test.mjs",
|
"test": "tsx --test app/renderer/shared/ui/controls.tooltip.test.ts app/renderer/editor/state/projectState.race.test.ts app/shared/ipc/contracts.mediaRemoval.test.ts app/shared/effectEraserHitTest.test.ts app/renderer/control/controlApp.effectsPanel.test.ts app/renderer/shared/effects/PxiEffectsOverlay.pointer.test.ts app/main/windows/createWindows.editorClose.test.ts app/main/windows/bootWindow.test.ts app/main/effects/effectsStore.test.ts app/main/project/assetPrune.test.ts app/main/project/fsRetry.test.ts app/main/project/zipRead.test.ts app/shared/package.build.test.ts app/shared/license/canonicalJson.test.ts app/shared/license/productKey.test.ts app/shared/license/licenseService.networkRegression.test.ts app/shared/video/videoPlaybackPerf.networkRegression.test.ts app/shared/video/videoPlaybackLoop.networkRegression.test.ts app/main/license/verifyLicenseToken.test.ts && node --test scripts/build-env.test.mjs scripts/obfuscate-main.test.mjs",
|
||||||
"format": "prettier . --check",
|
"format": "prettier . --check",
|
||||||
"format:write": "prettier . --write",
|
"format:write": "prettier . --write",
|
||||||
"release:info": "node scripts/print-release-info.mjs",
|
"release:info": "node scripts/print-release-info.mjs",
|
||||||
|
|||||||
Reference in New Issue
Block a user