Skip to content

Commit f7474f1

Browse files
committed
normalize virtual CWD paths
1 parent 7db31fd commit f7474f1

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

crates/capsule-sdk/javascript/src/polyfills/fs.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,11 @@ function resolvePath(path: string): { dir: Descriptor; relativePath: string } |
107107
continue;
108108
}
109109

110-
if (normalizedPath.startsWith(normalizedGuest + '/')) {
111-
const relativePath = normalizedPath.slice(normalizedGuest.length + 1);
112-
return { dir: descriptor, relativePath };
110+
const guestPrefix = normalizedGuest.endsWith('/') ? normalizedGuest : normalizedGuest + '/';
111+
112+
if (normalizedPath.startsWith(guestPrefix)) {
113+
const relativePath = normalizedPath.slice(guestPrefix.length);
114+
return { dir: descriptor, relativePath: relativePath || '.' };
113115
}
114116

115117
if (normalizedPath === normalizedGuest) {

crates/capsule-sdk/javascript/src/polyfills/process.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,23 @@ function getArgv(): string[] {
7070
}
7171

7272
/**
73-
* Initialized from wasi:cli/environment initialCwd(), falls back to '.'.
73+
* Initialized from wasi:cli/environment initialCwd(), falls back to '/'.
7474
*/
7575
let _virtualCwd: string = (() => {
7676
try {
7777
const env = (globalThis as any)['wasi:cli/environment'];
7878
if (env && typeof env.initialCwd === 'function') {
79-
return env.initialCwd() ?? '.';
79+
const initial = env.initialCwd();
80+
if (initial) {
81+
return initial.startsWith('/') ? initial : '/' + initial;
82+
}
8083
}
8184
} catch {}
82-
return '.';
85+
return '/';
8386
})();
8487

8588
/**
86-
* Internal setter for virtual CWD — handles relative and absolute paths.
89+
* Internal setter for virtual CWD — handles relative and absolute paths safely via absolute resolution.
8790
*/
8891
function setCwd(directory: string): void {
8992
if (!directory) return;
@@ -92,11 +95,10 @@ function setCwd(directory: string): void {
9295
} else if (directory === '..') {
9396
const parts = _virtualCwd.split('/').filter(Boolean);
9497
parts.pop();
95-
_virtualCwd = parts.join('/') || '.';
98+
_virtualCwd = '/' + parts.join('/');
9699
} else {
97-
_virtualCwd = (_virtualCwd === '.' || _virtualCwd === '')
98-
? directory.replace(/\/+$/, '')
99-
: _virtualCwd.replace(/\/+$/, '') + '/' + directory.replace(/\/+$/, '');
100+
const base = _virtualCwd.endsWith('/') ? _virtualCwd : _virtualCwd + '/';
101+
_virtualCwd = (base + directory).replace(/\/+$/, '');
100102
}
101103
}
102104

0 commit comments

Comments
 (0)