Skip to content

Commit f4ff169

Browse files
only21milclaude
andcommitted
fix(server): preserve literal backslashes in POSIX workspace paths
resolveRelativePathWithinRoot turned every backslash into `/` before the containment check. On Linux and macOS `\` is a file name character, so a name like `weird\name.txt` came back as `weird/name.txt`, and a name like `..\..\etc` was refused as outside the project root. Only the host separator is translated now; Windows is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent bd6940b commit f4ff169

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

‎apps/server/src/workspace/WorkspacePaths.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import * as FileSystem from "effect/FileSystem";
55
import * as Layer from "effect/Layer";
66
import * as Path from "effect/Path";
77

8+
import * as HostProcess from "@t3tools/shared/HostProcess";
9+
810
import * as WorkspacePaths from "./WorkspacePaths.ts";
911

1012
const layerTest = Layer.empty.pipe(
@@ -58,5 +60,25 @@ it.layer(layerTest)("WorkspacePathsLive", (it) => {
5860
);
5961
}),
6062
);
63+
64+
// On Windows `\` is a separator, so these names cannot exist there.
65+
it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
66+
"keeps a backslash in a name as part of the name",
67+
() =>
68+
Effect.gen(function* () {
69+
const workspacePaths = yield* WorkspacePaths.WorkspacePaths;
70+
const path = yield* Path.Path;
71+
const cwd = yield* makeTempDir();
72+
73+
for (const relativePath of ["weird\\name.txt", "..\\..\\etc"]) {
74+
expect(
75+
yield* workspacePaths.resolveRelativePathWithinRoot({
76+
workspaceRoot: cwd,
77+
relativePath,
78+
}),
79+
).toEqual({ absolutePath: path.join(cwd, relativePath), relativePath });
80+
}
81+
}),
82+
);
6183
});
6284
});

‎apps/server/src/workspace/WorkspacePaths.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,6 @@ export class WorkspacePaths extends Context.Service<
103103
}
104104
>()("t3/workspace/WorkspacePaths") {}
105105

106-
function toPosixRelativePath(input: string): string {
107-
return input.replaceAll("\\", "/");
108-
}
109-
110106
const make = Effect.gen(function* () {
111107
const fileSystem = yield* FileSystem.FileSystem;
112108
const path = yield* Path.Path;
@@ -188,7 +184,10 @@ const make = Effect.gen(function* () {
188184
}
189185

190186
const absolutePath = path.resolve(input.workspaceRoot, normalizedInputPath);
191-
const relativeToRoot = toPosixRelativePath(path.relative(input.workspaceRoot, absolutePath));
187+
// Only the host separator becomes `/`: on POSIX `\` is a file name character.
188+
const relativeToRoot = path
189+
.relative(input.workspaceRoot, absolutePath)
190+
.replaceAll(path.sep, "/");
192191
if (
193192
relativeToRoot.length === 0 ||
194193
relativeToRoot === "." ||

0 commit comments

Comments
 (0)