fix(core): tolerate AlreadyExists in FSUtil.ensureDir#36542
Open
BB-84C wants to merge 1 commit into
Open
Conversation
Recursive makeDirectory should be idempotent, but some Windows runtimes surface AlreadyExists for a directory that already exists. Since v1.17.15, Config.ensureGitignore calls fs.ensureDir on every resolved config directory, so startup crashed with 'Unexpected server error' whenever a project's .opencode directory already existed. ensureDir now treats AlreadyExists as success when the path is already a directory, while still failing when a file occupies the path. Fixes anomalyco#35828
Contributor
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #35828
Type of change
What does this PR do?
v1.17.15 added
fs.ensureDir(dir)toConfig.ensureGitignore, which runs for every resolved config directory duringConfig.loadInstanceState, and the call site is wrapped inEffect.orDie.FSUtil.ensureDircallsmakeDirectory(path, { recursive: true })with no error handling.Recursive
makeDirectoryis supposed to be idempotent, but on some Windows runtimes it reportsAlreadyExistsfor a directory that already exists. So once a project had a.opencodedirectory, startup died with "Unexpected server error" (works on 1.17.14, broke on 1.17.15).The fix makes
ensureDirtreatAlreadyExistsas success, but only when the path is actually a directory — a file sitting at that path still fails. This mirrors theNotFoundcatch already used a few lines down inwriteWithDirs, so behavior only changes for the exact reported case.How did you verify your code works?
AlreadyExistsresult frommakeDirectory: it fails without this patch and passes with it.ensureDirstill fails when a file occupies the path.packages/corefilesystem suite passes (29 tests) andbun typecheckforpackages/coreis clean.I couldn't reproduce the crash on my own machine — recursive mkdir is idempotent on my Bun build — which is why the regression test injects the error directly instead of depending on the platform quirk.
Screenshots / recordings
N/A — not a UI change.
Checklist