Skip to content
TypeScript

fix(server): list folders named with a backslash on Linux and macOS (#23398)

Victor Vogele92981dcf3f7
diff --git a/apps/server/src/workspace/WorkspacePaths.test.ts b/apps/server/src/workspace/WorkspacePaths.test.ts
index 574904b3435..b178c621453 100644
--- a/apps/server/src/workspace/WorkspacePaths.test.ts
+++ b/apps/server/src/workspace/WorkspacePaths.test.ts
@@ -5,6 +5,8 @@ import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Path from "effect/Path";
+import * as HostProcess from "@t3tools/shared/HostProcess";
+
import * as WorkspacePaths from "./WorkspacePaths.ts";
const layerTest = Layer.empty.pipe(
@@ -58,5 +60,25 @@ it.layer(layerTest)("WorkspacePathsLive", (it) => {
);
}),
);
+
+ // On Windows `\` is a separator, so these names cannot exist there.
+ it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
+ "keeps a backslash in a name as part of the name",
+ () =>
+ Effect.gen(function* () {
+ const workspacePaths = yield* WorkspacePaths.WorkspacePaths;
+ const path = yield* Path.Path;
+ const cwd = yield* makeTempDir();
+
+ for (const relativePath of ["weird\\name.txt", "..\\..\\etc"]) {
+ expect(
+ yield* workspacePaths.resolveRelativePathWithinRoot({
+ workspaceRoot: cwd,
+ relativePath,
+ }),
+ ).toEqual({ absolutePath: path.join(cwd, relativePath), relativePath });
+ }
+ }),
+ );
});
});
diff --git a/apps/server/src/workspace/WorkspacePaths.ts b/apps/server/src/workspace/WorkspacePaths.ts
index 6b748e19396..ea183ed43e1 100644
--- a/apps/server/src/workspace/WorkspacePaths.ts
+++ b/apps/server/src/workspace/WorkspacePaths.ts
@@ -103,10 +103,6 @@ export class WorkspacePaths extends Context.Service<
}
>()("t3/workspace/WorkspacePaths") {}
-function toPosixRelativePath(input: string): string {
- return input.replaceAll("\\", "/");
-}
-
const make = Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
@@ -188,7 +184,10 @@ const make = Effect.gen(function* () {
}
const absolutePath = path.resolve(input.workspaceRoot, normalizedInputPath);
- const relativeToRoot = toPosixRelativePath(path.relative(input.workspaceRoot, absolutePath));
+ // Only the host separator becomes `/`: on POSIX `\` is a file name character.
+ const relativeToRoot = path
+ .relative(input.workspaceRoot, absolutePath)
+ .replaceAll(path.sep, "/");
if (
relativeToRoot.length === 0 ||
relativeToRoot === "." ||