Skip to content

fix(config): use writeWithDirs in ensureGitignore to prevent ENOENT crash#31339

Open
weiconghe wants to merge 1 commit into
anomalyco:devfrom
weiconghe:fix/ensure-gitignore-missing-dir
Open

fix(config): use writeWithDirs in ensureGitignore to prevent ENOENT crash#31339
weiconghe wants to merge 1 commit into
anomalyco:devfrom
weiconghe:fix/ensure-gitignore-missing-dir

Conversation

@weiconghe
Copy link
Copy Markdown
Contributor

@weiconghe weiconghe commented Jun 8, 2026

Issue for this PR

Closes #31341

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Problem: ensureGitignore in packages/opencode/src/config/config.ts uses fs.writeFileString to create .gitignore in each config directory. writeFileString does not create parent directories — when a config directory returned by ConfigPaths.directories() does not exist on disk, it throws ENOENT. The call site in loadInstanceState uses Effect.orDie, making this error fatal and crashing the server process on startup.

This is reproducible on Windows when OPENCODE_CONFIG_DIR is 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.writeFileString to fs.writeWithDirs in ensureGitignore. writeWithDirs calls mkdir -p on the parent path before writing, so the directory is created automatically.

Relationship to #30897: Both PRs fix the same crash. #30897 catches NotFound and silently skips — this means no .gitignore is created in the config directory. This PR takes the approach of actually creating the directory and the .gitignore file, which ensures the gitignore protection is in place even for custom config paths.

How did you verify your code works?

  1. Set OPENCODE_CONFIG_DIR to a non-existent path
  2. Before fix: OpenCode crashes with ENOENT
  3. After fix: OpenCode starts normally, .gitignore is created in the new directory
  4. Added a test case that creates a non-existent config dir and verifies .gitignore is written
  5. Ran existing config tests — all pass:
    bun test packages/opencode/test/config/config.test.ts
    

Screenshots / recordings

N/A

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

…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.
@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. needs:issue labels Jun 8, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 8, 2026

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 8, 2026

The following comment was made by an LLM, it may be inaccurate:

Potential duplicate found:

PR #30897: fix: handle NotFound error in ensureGitignore
#30897

This appears to be addressing the same issue — a crash in ensureGitignore related to missing directories/file not found errors. Both PRs target the same root cause (the ensureGitignore function failing when the config directory doesn't exist), though they may propose different solutions. You should check if #30897 was already merged or if it's still open, and determine if the approaches complement or conflict with each other.

@github-actions github-actions Bot removed needs:compliance This means the issue will auto-close after 2 hours. needs:issue labels Jun 8, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 8, 2026

Thanks for updating your PR! It now meets our contributing guidelines. 👍

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash on startup when OPENCODE_CONFIG_DIR points to a non-existent directory

1 participant