detached d007024

fix(agent): preflight LSP workspace edits

commit
d007024a7bbcd93e32591ea9ce46ae3b270c58ff
author
brice · · 2026-08-04 12:10 UTC
committer
brice · · 2026-08-04 12:56 UTC
parent
9051051
signature
GPG VerifiedCD88F9E564E0102646FB6077A1A07C7C7C3E349D · arch-gpg Verified for the committer email
2 files · +49 -8
M packages/agent/lsp/edits.go +26 -8
@@ -43,26 +43,44 @@ func ApplyWorkspaceEdit(root string, edit WorkspaceEdit) error {
4343 }
4444 all[change.TextDocument.URI] = append(all[change.TextDocument.URI], change.Edits...)
4545 }
46+ targets, err := prepareEditTargets(rootReal, all)
47+ if err != nil {
48+ return err
49+ }
50+ for _, location := range targets {
51+ if err := applyTextEdits(location.target, location.changes); err != nil {
52+ return fmt.Errorf("apply edit to %s: %w", location.path, err)
53+ }
54+ }
55+ return nil
56+}
57+
58+type editTarget struct {
59+ path string
60+ target string
61+ changes []TextEdit
62+}
63+
64+func prepareEditTargets(rootReal string, all map[string][]TextEdit) ([]editTarget, error) {
65+ targets := make([]editTarget, 0, len(all))
4666 for uri, changes := range all {
4767 path, err := uriToPath(uri)
4868 if err != nil {
49- return err
69+ return nil, err
5070 }
5171 if err := safeWorkspacePath(rootReal, path); err != nil {
52- return err
72+ return nil, err
5373 }
5474 target, err := filepath.EvalSymlinks(path)
5575 if err != nil {
56- return fmt.Errorf("workspace edit target %q: %w", path, err)
76+ return nil, fmt.Errorf("workspace edit target %q: %w", path, err)
5777 }
5878 if err := safeWorkspacePath(rootReal, target); err != nil {
59- return err
60- }
61- if err := applyTextEdits(target, changes); err != nil {
62- return fmt.Errorf("apply edit to %s: %w", path, err)
79+ return nil, err
6380 }
81+ targets = append(targets, editTarget{path: path, target: target, changes: changes})
6482 }
65- return nil
83+ return targets, nil
6684 }
6785
6886 func safeWorkspacePath(root, path string) error {
@@ -43,26 +43,44 @@ func ApplyWorkspaceEdit(root string, edit WorkspaceEdit) error {
43 }43 }
44 all[change.TextDocument.URI] = append(all[change.TextDocument.URI], change.Edits...)44 all[change.TextDocument.URI] = append(all[change.TextDocument.URI], change.Edits...)
45 }45 }
46+ targets, err := prepareEditTargets(rootReal, all)
47+ if err != nil {
48+ return err
49+ }
50+ for _, location := range targets {
51+ if err := applyTextEdits(location.target, location.changes); err != nil {
52+ return fmt.Errorf("apply edit to %s: %w", location.path, err)
53+ }
54+ }
55+ return nil
56+}
57+
58+type editTarget struct {
59+ path string
60+ target string
61+ changes []TextEdit
62+}
63+
64+func prepareEditTargets(rootReal string, all map[string][]TextEdit) ([]editTarget, error) {
65+ targets := make([]editTarget, 0, len(all))
46 for uri, changes := range all {66 for uri, changes := range all {
47 path, err := uriToPath(uri)67 path, err := uriToPath(uri)
48 if err != nil {68 if err != nil {
49- return err69+ return nil, err
50 }70 }
51 if err := safeWorkspacePath(rootReal, path); err != nil {71 if err := safeWorkspacePath(rootReal, path); err != nil {
52- return err72+ return nil, err
53 }73 }
54 target, err := filepath.EvalSymlinks(path)74 target, err := filepath.EvalSymlinks(path)
55 if err != nil {75 if err != nil {
56- return fmt.Errorf("workspace edit target %q: %w", path, err)76+ return nil, fmt.Errorf("workspace edit target %q: %w", path, err)
57 }77 }
58 if err := safeWorkspacePath(rootReal, target); err != nil {78 if err := safeWorkspacePath(rootReal, target); err != nil {
59- return err79+ return nil, err
60- }
61- if err := applyTextEdits(target, changes); err != nil {
62- return fmt.Errorf("apply edit to %s: %w", path, err)
63 }80 }
81+ targets = append(targets, editTarget{path: path, target: target, changes: changes})
64 }82 }
65- return nil83+ return targets, nil
66 }84 }
67 85
68 func safeWorkspacePath(root, path string) error {86 func safeWorkspacePath(root, path string) error {
M packages/agent/lsp/lsp_test.go +23 -0
@@ -357,6 +357,29 @@ func TestApplyWorkspaceEditUsesUTF16AndRejectsOutside(t *testing.T) {
357357 }
358358 }
359359
360+func TestWorkspaceEditPreflightsAllTargets(t *testing.T) {
361+ root := t.TempDir()
362+ inside := filepath.Join(root, "inside.txt")
363+ outside := filepath.Join(t.TempDir(), "outside.txt")
364+ if err := os.WriteFile(inside, []byte("old\n"), 0o644); err != nil {
365+ t.Fatal(err)
366+ }
367+ if err := os.WriteFile(outside, []byte("old\n"), 0o644); err != nil {
368+ t.Fatal(err)
369+ }
370+ rootReal, err := filepath.EvalSymlinks(root)
371+ if err != nil {
372+ t.Fatal(err)
373+ }
374+ _, err = prepareEditTargets(rootReal, map[string][]TextEdit{
375+ URIForPath(inside): {{Range: Range{}, NewText: "inside"}},
376+ URIForPath(outside): {{Range: Range{}, NewText: "outside"}},
377+ })
378+ if err == nil {
379+ t.Fatal("workspace edit accepted an unsafe target after a safe target")
380+ }
381+}
382+
360383 func TestApplyWorkspaceEditFollowsInWorkspaceSymlink(t *testing.T) {
361384 if runtime.GOOS == "windows" {
362385 t.Skip("symlink permissions vary on Windows")
@@ -357,6 +357,29 @@ func TestApplyWorkspaceEditUsesUTF16AndRejectsOutside(t *testing.T) {
357 }357 }
358 }358 }
359 359
360+func TestWorkspaceEditPreflightsAllTargets(t *testing.T) {
361+ root := t.TempDir()
362+ inside := filepath.Join(root, "inside.txt")
363+ outside := filepath.Join(t.TempDir(), "outside.txt")
364+ if err := os.WriteFile(inside, []byte("old\n"), 0o644); err != nil {
365+ t.Fatal(err)
366+ }
367+ if err := os.WriteFile(outside, []byte("old\n"), 0o644); err != nil {
368+ t.Fatal(err)
369+ }
370+ rootReal, err := filepath.EvalSymlinks(root)
371+ if err != nil {
372+ t.Fatal(err)
373+ }
374+ _, err = prepareEditTargets(rootReal, map[string][]TextEdit{
375+ URIForPath(inside): {{Range: Range{}, NewText: "inside"}},
376+ URIForPath(outside): {{Range: Range{}, NewText: "outside"}},
377+ })
378+ if err == nil {
379+ t.Fatal("workspace edit accepted an unsafe target after a safe target")
380+ }
381+}
382+
360 func TestApplyWorkspaceEditFollowsInWorkspaceSymlink(t *testing.T) {383 func TestApplyWorkspaceEditFollowsInWorkspaceSymlink(t *testing.T) {
361 if runtime.GOOS == "windows" {384 if runtime.GOOS == "windows" {
362 t.Skip("symlink permissions vary on Windows")385 t.Skip("symlink permissions vary on Windows")

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 477

↑↓ moveenter openesc close