fix(config): use writeWithDirs in ensureGitignore to prevent ENOENT crash#31339
fix(config): use writeWithDirs in ensureGitignore to prevent ENOENT crash#31339weiconghe wants to merge 1 commit into
Conversation
…arent directory ensureGitignore used writeFileString which fails with ENOENT when the parent directory does not exist (e.g. when OPENCODE_CONFIG_DIR points to a directory that was deleted by an auto-update). Changed to writeWithDirs which auto-creates the parent directory before writing. Reproduced on Windows where OPENCODE_CONFIG_DIR was set to a path inside the app installation directory. After each auto-update, the directory was wiped but the env var persisted, causing a startup crash with ENOENT on every boot. Test: verifies .gitignore is created successfully when OPENCODE_CONFIG_DIR points to a non-existent directory.
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
|
The following comment was made by an LLM, it may be inaccurate: Potential duplicate found: PR #30897: This appears to be addressing the same issue — a crash in |
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
Issue for this PR
Closes #31341
Type of change
What does this PR do?
Problem:
ensureGitignoreinpackages/opencode/src/config/config.tsusesfs.writeFileStringto create.gitignorein each config directory.writeFileStringdoes not create parent directories — when a config directory returned byConfigPaths.directories()does not exist on disk, it throws ENOENT. The call site inloadInstanceStateusesEffect.orDie, making this error fatal and crashing the server process on startup.This is reproducible on Windows when
OPENCODE_CONFIG_DIRis set (e.g. as a user environment variable) and the directory gets removed — the most common case being OpenCode Desktop auto-update, where the NSIS installer wipes the installation directory.Impact: OpenCode Desktop crashes on every startup after auto-update, with no way to recover except manually creating the directory or deleting the environment variable.
Fix: Changed
fs.writeFileStringtofs.writeWithDirsinensureGitignore.writeWithDirscallsmkdir -pon the parent path before writing, so the directory is created automatically.Relationship to #30897: Both PRs fix the same crash. #30897 catches
NotFoundand silently skips — this means no.gitignoreis created in the config directory. This PR takes the approach of actually creating the directory and the.gitignorefile, which ensures the gitignore protection is in place even for custom config paths.How did you verify your code works?
OPENCODE_CONFIG_DIRto a non-existent path.gitignoreis created in the new directory.gitignoreis writtenScreenshots / recordings
N/A
Checklist