Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

[Windows] Fix `Copy Path` and `Copy Relative Path` returning identical results. (#5399)

Srihari Thyagarajan0a9ee16a67dc
diff --git a/frontend/src/components/editor/file-tree/__tests__/requesting-tree.test.ts b/frontend/src/components/editor/file-tree/__tests__/requesting-tree.test.ts
index 38173b2dc4..c2d6597624 100644
--- a/frontend/src/components/editor/file-tree/__tests__/requesting-tree.test.ts
+++ b/frontend/src/components/editor/file-tree/__tests__/requesting-tree.test.ts
@@ -1,6 +1,7 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { toast } from "@/components/ui/use-toast";
+import type { FilePath } from "@/utils/paths";
import { RequestingTree } from "../requesting-tree";
const sendListFiles = vi.fn();
@@ -261,4 +262,60 @@ describe("RequestingTree", () => {
expect(mockOnChange).toHaveBeenCalled();
});
});
+
+ describe("relativeFromRoot", () => {
+ test("should return relative path for Unix paths", async () => {
+ const tree = new RequestingTree({
+ listFiles: sendListFiles,
+ createFileOrFolder: sendCreateFileOrFolder,
+ deleteFileOrFolder: sendDeleteFileOrFolder,
+ renameFileOrFolder: sendRenameFileOrFolder,
+ });
+
+ sendListFiles.mockResolvedValue({
+ files: [],
+ root: "/home/user/project",
+ });
+
+ await tree.initialize(vi.fn());
+
+ const relativePath = tree.relativeFromRoot(
+ "/home/user/project/src/file.py" as FilePath,
+ );
+ expect(relativePath).toBe("src/file.py");
+ });
+
+ test("should return relative path for Windows paths", async () => {
+ const tree = new RequestingTree({
+ listFiles: sendListFiles,
+ createFileOrFolder: sendCreateFileOrFolder,
+ deleteFileOrFolder: sendDeleteFileOrFolder,
+ renameFileOrFolder: sendRenameFileOrFolder,
+ });
+
+ sendListFiles.mockResolvedValue({
+ files: [],
+ root: "C:\\Users\\test\\project",
+ });
+
+ await tree.initialize(vi.fn());
+
+ const relativePath = tree.relativeFromRoot(
+ "C:\\Users\\test\\project\\src\\file.py" as FilePath,
+ );
+ expect(relativePath).toBe("src\\file.py");
+ });
+
+ test("should return original path when not under root", async () => {
+ const relativePath = requestingTree.relativeFromRoot(
+ "/other/path/file.py" as FilePath,
+ );
+ expect(relativePath).toBe("/other/path/file.py");
+ });
+
+ test("should handle root path exactly", async () => {
+ const relativePath = requestingTree.relativeFromRoot("/root" as FilePath);
+ expect(relativePath).toBe("/root");
+ });
+ });
});
diff --git a/frontend/src/components/editor/file-tree/requesting-tree.tsx b/frontend/src/components/editor/file-tree/requesting-tree.tsx
index 36267255b4..76812ed07d 100644
--- a/frontend/src/components/editor/file-tree/requesting-tree.tsx
+++ b/frontend/src/components/editor/file-tree/requesting-tree.tsx
@@ -203,7 +203,11 @@ export class RequestingTree {
};
public relativeFromRoot = (path: FilePath): FilePath => {
- const root = withTrailingSlash(this.rootPath);
+ // Add a trailing delimiter to the root path if it doesn't have one
+ const root = this.rootPath.endsWith(this.path.deliminator)
+ ? this.rootPath
+ : `${this.rootPath}${this.path.deliminator}`;
+
if (path.startsWith(root)) {
return path.slice(root.length) as FilePath;
}
@@ -224,7 +228,3 @@ export class RequestingTree {
return response;
};
}
-
-function withTrailingSlash(path: string): string {
- return path.endsWith("/") ? path : `${path}/`;
-}