From 0fc4e7cfc84765c4c83ede560cefe06fc00e897f Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 02:16:49 -0600 Subject: [PATCH] fix(mcpx): source abs() expands home only for bare ~ and ~/ (#2988) Mirror the #1702 tilde rule already used by searchpath and routes_dashboard: a ~name path resolves literally under Dir instead of under the caller's home. Adds the abs() case matrix (~, ~/x, ~alice/x, ~nosuchuser/x, ~scratch) and an end-to-end Resolve(@file:~alice/...) test proving the literal directory under Dir is read. --- .../mcpx/internal/source/abs_internal_test.go | 26 ++++++++++++++++ pkgs/mcpx/internal/source/source.go | 2 +- pkgs/mcpx/internal/source/source_test.go | 31 +++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 pkgs/mcpx/internal/source/abs_internal_test.go diff --git a/pkgs/mcpx/internal/source/abs_internal_test.go b/pkgs/mcpx/internal/source/abs_internal_test.go new file mode 100644 index 0000000000..83b1126542 --- /dev/null +++ b/pkgs/mcpx/internal/source/abs_internal_test.go @@ -0,0 +1,26 @@ +package source + +import "testing" + +func TestAbsExpandsOnlyBareTildeAndTildeSlash(t *testing.T) { + // #1702 / #2988, rule pinned by triage task 1921 and mirrored from + // searchpath's TestOnlyBareTildeAndTildeSlashExpandHome: only a bare + // ~ and ~/... name the caller's home. A ~name path is not expanded -- + // it resolves literally under Dir -- because resolving it under the + // caller's home silently reads a lookalike directory (and invents + // one for users that do not exist), while a passwd lookup would + // answer differently for the CLI user and the daemon user. + t.Setenv("HOME", "/home/testhome") + cases := []struct{ in, want string }{ + {"~", "/home/testhome"}, + {"~/x", "/home/testhome/x"}, + {"~alice/x", "/w/~alice/x"}, + {"~nosuchuser/x", "/w/~nosuchuser/x"}, + {"~scratch", "/w/~scratch"}, + } + for _, c := range cases { + if got := abs(c.in, "/w"); got != c.want { + t.Errorf("abs(%q) = %q, want %q", c.in, got, c.want) + } + } +} diff --git a/pkgs/mcpx/internal/source/source.go b/pkgs/mcpx/internal/source/source.go index cb78245395..1cf7b818f5 100644 --- a/pkgs/mcpx/internal/source/source.go +++ b/pkgs/mcpx/internal/source/source.go @@ -117,7 +117,7 @@ func abs(path, dir string) string { if filepath.IsAbs(path) { return path } - if strings.HasPrefix(path, "~") { + if path == "~" || strings.HasPrefix(path, "~/") { if home, err := os.UserHomeDir(); err == nil { return filepath.Join(home, strings.TrimPrefix(path, "~")) } diff --git a/pkgs/mcpx/internal/source/source_test.go b/pkgs/mcpx/internal/source/source_test.go index d9cbc8aed3..3516475d0f 100644 --- a/pkgs/mcpx/internal/source/source_test.go +++ b/pkgs/mcpx/internal/source/source_test.go @@ -193,3 +193,34 @@ func TestTextAndFileAtTheSameLevelIsAConflict(t *testing.T) { t.Errorf("one form alone is fine: %v", err) } } + +func TestResolveFileWithTildeNameReadsLiteralDirUnderDir(t *testing.T) { + // End to end for #2988: @file:~alice/... must read the literal + // ~alice directory under Dir, never $HOME/alice. + home := t.TempDir() + t.Setenv("HOME", home) + if err := os.MkdirAll(filepath.Join(home, "alice"), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(home, "alice", "note.txt"), []byte("from home"), 0o644); err != nil { + t.Fatal(err) + } + dir := t.TempDir() + literal := filepath.Join(dir, "~alice") + if err := os.MkdirAll(literal, 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(literal, "note.txt"), []byte("from literal dir"), 0o644); err != nil { + t.Fatal(err) + } + got, err := source.Resolve("@file:~alice/note.txt", source.Options{Dir: dir}) + if err != nil { + t.Fatal(err) + } + if got.Text != "from literal dir" { + t.Errorf("Resolve(@file:~alice/note.txt) text = %q, want %q", got.Text, "from literal dir") + } + if len(got.Files) != 1 || got.Files[0] != filepath.Join(literal, "note.txt") { + t.Errorf("Resolve(@file:~alice/note.txt) files = %v, want [%s]", got.Files, filepath.Join(literal, "note.txt")) + } +}