-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix(server): tolerate existing workspace dirs #978
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
49 changes: 49 additions & 0 deletions
49
packages/browseros-agent/apps/server/src/lib/ensure-directory.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 BrowserOS | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| import type { Stats } from 'node:fs' | ||
| import { mkdir as defaultMkdir, stat as defaultStat } from 'node:fs/promises' | ||
|
|
||
| interface EnsureDirectoryDeps { | ||
| mkdir?: typeof defaultMkdir | ||
| stat?: typeof defaultStat | ||
| } | ||
|
|
||
| export async function ensureDirectory( | ||
| path: string, | ||
| deps: EnsureDirectoryDeps = {}, | ||
| ): Promise<void> { | ||
| const mkdir = deps.mkdir ?? defaultMkdir | ||
| try { | ||
| await mkdir(path, { recursive: true }) | ||
| return | ||
| } catch (err) { | ||
| if (!isAlreadyExistsError(err)) throw err | ||
| const info = await statExistingDirectory(path, err, deps.stat) | ||
| if (!info.isDirectory()) throw err | ||
| } | ||
| } | ||
|
|
||
| async function statExistingDirectory( | ||
| path: string, | ||
| originalError: unknown, | ||
| stat: typeof defaultStat = defaultStat, | ||
| ): Promise<Stats> { | ||
| try { | ||
| return await stat(path) | ||
| } catch { | ||
| throw originalError | ||
| } | ||
| } | ||
|
|
||
| function isAlreadyExistsError(err: unknown): boolean { | ||
| return ( | ||
| typeof err === 'object' && | ||
| err !== null && | ||
| 'code' in err && | ||
| err.code === 'EEXIST' | ||
| ) | ||
| } | ||
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
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
66 changes: 66 additions & 0 deletions
66
packages/browseros-agent/apps/server/tests/lib/ensure-directory.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 BrowserOS | ||
| */ | ||
|
|
||
| import { afterEach, describe, expect, it } from 'bun:test' | ||
| import type { Stats } from 'node:fs' | ||
| import { mkdtemp, rm, stat, writeFile } from 'node:fs/promises' | ||
| import { tmpdir } from 'node:os' | ||
| import { join } from 'node:path' | ||
| import { ensureDirectory } from '../../src/lib/ensure-directory' | ||
|
|
||
| describe('ensureDirectory', () => { | ||
| const tempDirs: string[] = [] | ||
|
|
||
| afterEach(async () => { | ||
| await Promise.all( | ||
| tempDirs.map((dir) => rm(dir, { recursive: true, force: true })), | ||
| ) | ||
| tempDirs.length = 0 | ||
| }) | ||
|
|
||
| it('creates missing nested directories', async () => { | ||
| const root = await mkdtemp(join(tmpdir(), 'browseros-ensure-dir-')) | ||
| tempDirs.push(root) | ||
| const target = join(root, 'OneDrive', 'South Hills OS') | ||
|
|
||
| await ensureDirectory(target) | ||
|
|
||
| expect((await stat(target)).isDirectory()).toBe(true) | ||
| }) | ||
|
|
||
| it('treats EEXIST as success when the requested directory exists', async () => { | ||
| const target = 'C:\\Users\\user\\OneDrive\\South Hills OS' | ||
| const eexist = Object.assign( | ||
| new Error( | ||
| "EEXIST: file already exists, mkdir 'C:\\Users\\user\\OneDrive'", | ||
| ), | ||
| { code: 'EEXIST', path: 'C:\\Users\\user\\OneDrive' }, | ||
| ) | ||
| let statPath: string | undefined | ||
|
|
||
| await ensureDirectory(target, { | ||
| mkdir: (async () => { | ||
| throw eexist | ||
| }) as typeof import('node:fs/promises').mkdir, | ||
| stat: (async (path: string) => { | ||
| statPath = path | ||
| return { | ||
| isDirectory: () => true, | ||
| } as Stats | ||
| }) as typeof import('node:fs/promises').stat, | ||
| }) | ||
|
|
||
| expect(statPath).toBe(target) | ||
| }) | ||
|
shadowfax92 marked this conversation as resolved.
|
||
|
|
||
| it('does not hide EEXIST when the requested path is not a directory', async () => { | ||
| const root = await mkdtemp(join(tmpdir(), 'browseros-ensure-dir-')) | ||
| tempDirs.push(root) | ||
| const target = join(root, 'not-a-dir') | ||
| await writeFile(target, 'file') | ||
|
|
||
| await expect(ensureDirectory(target)).rejects.toThrow(/EEXIST|ENOTDIR/) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.