Skip to content

fix(include): initialize missing configs safely - #74

Open
randomcat4 wants to merge 1 commit into
cordiverse:mainfrom
randomcat4:agent/fix-include-initialization
Open

fix(include): initialize missing configs safely#74
randomcat4 wants to merge 1 commit into
cordiverse:mainfrom
randomcat4:agent/fix-include-initialization

Conversation

@randomcat4

Copy link
Copy Markdown

What this fixes

initial is intended to create an Include config when the target file does not exist. The current startup path schedules that write through the normal debounced writer and immediately reads the file again, so first startup can still fail with ENOENT.

The same catch block also treats parsing and permission failures as a missing file. If an existing JSON or YAML file is malformed, startup rejects but the scheduled write can still replace the original file with initial.

Root cause

Service.init catches every error from the first read, calls the deferred writeFile() helper, and then reads again without waiting for the write. This both races missing-file initialization and turns unrelated read failures into destructive recovery attempts.

Changes

  • Only use initial when the first read fails with ENOENT.
  • Await the existing temporary-file-and-rename writer before continuing startup.
  • Force the subsequent read so the newly written content is parsed into the in-memory configuration.
  • Preserve the original error for malformed or otherwise unreadable existing files.

Tests

  • A missing JSON config is created and fully loaded before plugin startup resolves.
  • A malformed existing JSON config rejects startup and remains byte-for-byte unchanged.

Local verification:

  • Include workspace: 2 test files, 12 tests passed.
  • Full esbuild build and TypeScript declaration build passed.
  • ESLint passed.
  • The full Vitest run passed 19 test files and 139 tests; the only suite that could not be collected on Windows hit the existing HMR fixture path issue in fix(hmr): resolve test fixture paths on Windows #58.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant