From cbba677b5fce823e66870057fd99d87f5cefb22f Mon Sep 17 00:00:00 2001 From: Mavdol Date: Wed, 15 Apr 2026 13:37:17 +0200 Subject: [PATCH] update path normalization to handle absolute paths and apply effective path resolution in mkdir functions --- crates/capsule-sdk/javascript/src/polyfills/fs.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/crates/capsule-sdk/javascript/src/polyfills/fs.ts b/crates/capsule-sdk/javascript/src/polyfills/fs.ts index 01f1549..1dfe0fd 100644 --- a/crates/capsule-sdk/javascript/src/polyfills/fs.ts +++ b/crates/capsule-sdk/javascript/src/polyfills/fs.ts @@ -72,9 +72,8 @@ function getPreopenedDirs(): PreopenedDir[] { } function normalizePath(path: string): string { - if (path.startsWith('./')) { - return path.slice(2); - } + if (path.startsWith('./')) return path.slice(2); + if (path.startsWith('/')) return path.slice(1); return path; } @@ -97,9 +96,11 @@ function resolvePath(path: string): { dir: Descriptor; relativePath: string } | const normalizedPath = normalizePath(getEffectivePath(path)); + const sorted = [...preopens].sort((a, b) => b.guestPath.length - a.guestPath.length); + let catchAll: { dir: Descriptor; relativePath: string } | null = null; - for (const { descriptor, guestPath } of preopens) { + for (const { descriptor, guestPath } of sorted) { const normalizedGuest = normalizePath(guestPath); if (normalizedGuest === '.' || normalizedGuest === '') { @@ -468,7 +469,7 @@ export function lstatSync(path: string): StatResult { */ export function mkdirSync(path: string, options?: MkdirOptions): void { if (options?.recursive) { - const normalized = normalizePath(path); + const normalized = normalizePath(getEffectivePath(path)); const parts = normalized.split('/').filter(Boolean); for (let i = 1; i <= parts.length; i++) { const partial = parts.slice(0, i).join('/'); @@ -724,7 +725,7 @@ export interface MkdirOptions { */ export async function mkdir(path: string, options?: MkdirOptions): Promise { if (options?.recursive) { - const normalized = normalizePath(path); + const normalized = normalizePath(getEffectivePath(path)); const parts = normalized.split('/').filter(Boolean); for (let i = 1; i <= parts.length; i++) { const partial = parts.slice(0, i).join('/');