From 6cc70eaeeab9899ecfa9924b566bf3437f355c6f Mon Sep 17 00:00:00 2001 From: Aly Raffauf Date: Sun, 1 Nov 2026 16:10:59 -0500 Subject: [PATCH] refactor(workspace): share lexical path normalization --- src/albedo/daemon/folders.gleam | 39 +++++++++++++------------------ src/albedo/harness/location.gleam | 5 ++-- test/e2e/locations_test.py | 9 +++++++ 3 files changed, 28 insertions(+), 25 deletions(-) diff --git a/src/albedo/daemon/folders.gleam b/src/albedo/daemon/folders.gleam index 0ab8f903c..c336fb46c 100644 --- a/src/albedo/daemon/folders.gleam +++ b/src/albedo/daemon/folders.gleam @@ -215,10 +215,14 @@ fn open(path: String, route: Route) -> Result(#(Machine, String), Failure) { case path { "~" | "~/" <> _ | "/" <> _ -> { let machine = - Machine(vcs.local(), entries, file_size, normalise(home()), fn(path) { - path - }) - let dir = normalise(expand(path, machine.home)) + Machine( + vcs.local(), + entries, + file_size, + location.normalise_absolute_path(home()), + fn(path) { path }, + ) + let dir = location.normalise_absolute_path(expand(path, machine.home)) case is_directory(dir) { True -> Ok(#(machine, dir)) False -> Error(missing(dir)) @@ -272,7 +276,7 @@ fn remote(path: String, route: Route) -> Result(#(Machine, String), Failure) { ) let dir = case rest { "" -> absolute - _ -> normalise(expand(rest, host.home)) + _ -> location.normalise_absolute_path(expand(rest, host.home)) } let show = fn(path) { location.to_string(location.Remote(user, name, path)) } let request = @@ -389,24 +393,26 @@ fn resolve( ) -> Result(String, FileFailure) { let #(dir, home) = case workspace { location.Local(dir) -> { - let home = normalise(home()) - #(normalise(expand(dir, home)), Ok(home)) + let home = location.normalise_absolute_path(home()) + #(location.normalise_absolute_path(expand(dir, home)), Ok(home)) } location.Remote(path: dir, ..) -> #(dir, Error(Nil)) } case path, location.parse(path) { "", _ -> Error(BadPath("path is empty")) - "/" <> _, _ -> Ok(normalise(path)) + "/" <> _, _ -> Ok(location.normalise_absolute_path(path)) "~", _ | "~/" <> _, _ -> home - |> result.map(fn(home) { normalise(expand(path, home)) }) + |> result.map(fn(home) { + location.normalise_absolute_path(expand(path, home)) + }) |> result.replace_error(BadPath("~ is only resolved in a local workspace")) _, Ok(location.Remote(path: absolute, ..) as at) -> case location.ssh_target(at) == location.ssh_target(workspace) { True -> Ok(absolute) False -> Error(BadPath("path is not on this session's host")) } - _, _ -> Ok(normalise(child(dir, path))) + _, _ -> Ok(location.normalise_absolute_path(child(dir, path))) } } @@ -616,19 +622,6 @@ fn gathered( ) } -fn normalise(path: String) -> String { - let parts = - string.split(path, "/") - |> list.fold([], fn(parts, part) { - case part { - "" | "." -> parts - ".." -> list.drop(parts, 1) - _ -> [part, ..parts] - } - }) - "/" <> string.join(list.reverse(parts), "/") -} - fn vcs_name(checkout: vcs.Checkout) -> String { case checkout { vcs.GitCheckout(_) -> "git" diff --git a/src/albedo/harness/location.gleam b/src/albedo/harness/location.gleam index b2c97e5d7..25b737263 100644 --- a/src/albedo/harness/location.gleam +++ b/src/albedo/harness/location.gleam @@ -67,7 +67,7 @@ fn remote(text: String) -> Result(Location, String) { None -> Ok(None) }) case path { - "/" <> _ -> Ok(Remote(user, host, normalise(path))) + "/" <> _ -> Ok(Remote(user, host, normalise_absolute_path(path))) "~" <> _ -> Error( "a path on " @@ -109,7 +109,8 @@ fn address(value: String) -> Result(String, String) { const alphanumeric = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" -fn normalise(path: String) -> String { +/// Normalize an absolute POSIX path lexically without resolving filesystem links. +pub fn normalise_absolute_path(path: String) -> String { let parts = string.split(path, "/") |> list.fold([], fn(parts, part) { diff --git a/test/e2e/locations_test.py b/test/e2e/locations_test.py index da56c93bb..4cf1b6266 100644 --- a/test/e2e/locations_test.py +++ b/test/e2e/locations_test.py @@ -83,6 +83,15 @@ class LocationsTest(unittest.TestCase): }, ) + def test_remote_workspace_normalization_clamps_parent_segments_at_root(self): + created = self.create(f"{HOST}:/../../one//two/.././") + self.assertEqual(created["workspace"], f"{HOST}:/one") + self.assertEqual(created["location"]["path"], "/one") + + root = self.create(f"{HOST}:/one/../../") + self.assertEqual(root["workspace"], f"{HOST}:/") + self.assertEqual(root["location"]["path"], "/") + # `albedo new` takes the active profile, which other tests on the shared # daemon rewrite; a daemon of its own keeps that profile this fixture's. # exclusive: CLI new depends on the active provider profile other fixtures rewrite -- knotmirror 0.0.1