From 94d6fce3cc3e0cc2ea44e6ffd86401f5797ca58b Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sun, 19 Apr 2026 08:00:10 +0200 Subject: [PATCH 1/2] Change clipboard tests to demonstrate bug with copying absolute file path Change the tests so that they run in a linked worktree; this uncovers the bug that copying a file's absolute path uses the main repo path rather than the worktree's path. --- pkg/integration/tests/diff/copy_to_clipboard.go | 15 +++++++++++++-- pkg/integration/tests/file/copy_menu.go | 16 +++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/pkg/integration/tests/diff/copy_to_clipboard.go b/pkg/integration/tests/diff/copy_to_clipboard.go index 34f2bb95c..0dd53007e 100644 --- a/pkg/integration/tests/diff/copy_to_clipboard.go +++ b/pkg/integration/tests/diff/copy_to_clipboard.go @@ -2,6 +2,7 @@ package diff import ( "os" + "path/filepath" "github.com/jesseduffield/lazygit/pkg/config" . "github.com/jesseduffield/lazygit/pkg/integration/components" @@ -22,6 +23,12 @@ var CopyToClipboard = NewIntegrationTest(NewIntegrationTestArgs{ config.GetUserConfig().OS.CopyToClipboardCmd = "printf '%s' {{text}} > clipboard" }, SetupRepo: func(shell *Shell) { + // Run the test in a linked worktree so that we catch bugs where we + // use the main repo's path instead of the current worktree's path. + shell.EmptyCommit("initial commit") + shell.AddWorktree("HEAD", "../linked-worktree", "mybranch") + shell.Chdir("../linked-worktree") + shell.CreateDir("dir") shell.CreateFileAndAdd("dir/file1", "1st line\n") shell.Commit("1") @@ -38,6 +45,7 @@ var CopyToClipboard = NewIntegrationTest(NewIntegrationTestArgs{ Contains("3").IsSelected(), Contains("2"), Contains("1"), + Contains("initial commit"), ). SelectNextItem(). PressEnter() @@ -80,9 +88,12 @@ var CopyToClipboard = NewIntegrationTest(NewIntegrationTestArgs{ Confirm(). Tap(func() { t.ExpectToast(Equals("File path copied to clipboard")) - repoDir, _ := os.Getwd() + worktreeDir, _ := os.Getwd() // On windows the following path would have backslashes, but we don't run integration tests on windows yet. - expectClipboard(t, Equals(repoDir+"/dir/file1")) + /* EXPECTED: + expectClipboard(t, Equals(worktreeDir+"/dir/file1")) + ACTUAL: */ + expectClipboard(t, Equals(filepath.Dir(worktreeDir)+"/repo/dir/file1")) }) }). Press(keys.Files.CopyFileInfoToClipboard). diff --git a/pkg/integration/tests/file/copy_menu.go b/pkg/integration/tests/file/copy_menu.go index 8151cfa10..5474614f6 100644 --- a/pkg/integration/tests/file/copy_menu.go +++ b/pkg/integration/tests/file/copy_menu.go @@ -2,6 +2,7 @@ package file import ( "os" + "path/filepath" "github.com/jesseduffield/lazygit/pkg/config" . "github.com/jesseduffield/lazygit/pkg/integration/components" @@ -21,7 +22,13 @@ var CopyMenu = NewIntegrationTest(NewIntegrationTestArgs{ SetupConfig: func(config *config.AppConfig) { config.GetUserConfig().OS.CopyToClipboardCmd = "printf '%s' {{text}} > clipboard" }, - SetupRepo: func(shell *Shell) {}, + SetupRepo: func(shell *Shell) { + // Run the test in a linked worktree so that we catch bugs where we + // use the main repo's path instead of the current worktree's path. + shell.EmptyCommit("initial commit") + shell.AddWorktree("HEAD", "../linked-worktree", "mybranch") + shell.Chdir("../linked-worktree") + }, Run: func(t *TestDriver, keys config.KeybindingConfig) { // Disabled item t.Views().Files(). @@ -130,9 +137,12 @@ var CopyMenu = NewIntegrationTest(NewIntegrationTestArgs{ t.ExpectToast(Equals("File path copied to clipboard")) - repoDir, _ := os.Getwd() + worktreeDir, _ := os.Getwd() // On windows the following path would have backslashes, but we don't run integration tests on windows yet. - expectClipboard(t, Equals(repoDir+"/dir/1-unstaged_file")) + /* EXPECTED: + expectClipboard(t, Equals(worktreeDir+"/dir/1-unstaged_file")) + ACTUAL: */ + expectClipboard(t, Equals(filepath.Dir(worktreeDir)+"/repo/dir/1-unstaged_file")) }) // Selected path diff on a single (unstaged) file From d3095f6fd4080ee8ff0a43f469bcca6b6b750cf7 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sun, 19 Apr 2026 08:00:14 +0200 Subject: [PATCH 2/2] Fix copying a file's absolute path when running in a linked worktree --- pkg/gui/controllers/commits_files_controller.go | 6 +++++- pkg/gui/controllers/files_controller.go | 6 +++++- pkg/integration/tests/diff/copy_to_clipboard.go | 4 ---- pkg/integration/tests/file/copy_menu.go | 4 ---- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/pkg/gui/controllers/commits_files_controller.go b/pkg/gui/controllers/commits_files_controller.go index e4a7cba68..699360b63 100644 --- a/pkg/gui/controllers/commits_files_controller.go +++ b/pkg/gui/controllers/commits_files_controller.go @@ -248,7 +248,11 @@ func (self *CommitFilesController) openCopyMenu() error { copyAbsolutePathItem := &types.MenuItem{ Label: self.c.Tr.CopyAbsoluteFilePath, OnPress: func() error { - if err := self.c.OS().CopyToClipboard(filepath.Join(self.c.Git().RepoPaths.RepoPath(), node.GetPath())); err != nil { + absPath, err := filepath.Abs(node.GetPath()) + if err != nil { + return err + } + if err := self.c.OS().CopyToClipboard(absPath); err != nil { return err } self.c.Toast(self.c.Tr.FilePathCopiedToast) diff --git a/pkg/gui/controllers/files_controller.go b/pkg/gui/controllers/files_controller.go index 6686a7e9d..ee856ddb5 100644 --- a/pkg/gui/controllers/files_controller.go +++ b/pkg/gui/controllers/files_controller.go @@ -1199,7 +1199,11 @@ func (self *FilesController) openCopyMenu() error { copyAbsolutePathItem := &types.MenuItem{ Label: self.c.Tr.CopyAbsoluteFilePath, OnPress: func() error { - if err := self.c.OS().CopyToClipboard(filepath.Join(self.c.Git().RepoPaths.RepoPath(), node.GetPath())); err != nil { + absPath, err := filepath.Abs(node.GetPath()) + if err != nil { + return err + } + if err := self.c.OS().CopyToClipboard(absPath); err != nil { return err } self.c.Toast(self.c.Tr.FilePathCopiedToast) diff --git a/pkg/integration/tests/diff/copy_to_clipboard.go b/pkg/integration/tests/diff/copy_to_clipboard.go index 0dd53007e..e28a7c0bc 100644 --- a/pkg/integration/tests/diff/copy_to_clipboard.go +++ b/pkg/integration/tests/diff/copy_to_clipboard.go @@ -2,7 +2,6 @@ package diff import ( "os" - "path/filepath" "github.com/jesseduffield/lazygit/pkg/config" . "github.com/jesseduffield/lazygit/pkg/integration/components" @@ -90,10 +89,7 @@ var CopyToClipboard = NewIntegrationTest(NewIntegrationTestArgs{ t.ExpectToast(Equals("File path copied to clipboard")) worktreeDir, _ := os.Getwd() // On windows the following path would have backslashes, but we don't run integration tests on windows yet. - /* EXPECTED: expectClipboard(t, Equals(worktreeDir+"/dir/file1")) - ACTUAL: */ - expectClipboard(t, Equals(filepath.Dir(worktreeDir)+"/repo/dir/file1")) }) }). Press(keys.Files.CopyFileInfoToClipboard). diff --git a/pkg/integration/tests/file/copy_menu.go b/pkg/integration/tests/file/copy_menu.go index 5474614f6..16dd6c19b 100644 --- a/pkg/integration/tests/file/copy_menu.go +++ b/pkg/integration/tests/file/copy_menu.go @@ -2,7 +2,6 @@ package file import ( "os" - "path/filepath" "github.com/jesseduffield/lazygit/pkg/config" . "github.com/jesseduffield/lazygit/pkg/integration/components" @@ -139,10 +138,7 @@ var CopyMenu = NewIntegrationTest(NewIntegrationTestArgs{ worktreeDir, _ := os.Getwd() // On windows the following path would have backslashes, but we don't run integration tests on windows yet. - /* EXPECTED: expectClipboard(t, Equals(worktreeDir+"/dir/1-unstaged_file")) - ACTUAL: */ - expectClipboard(t, Equals(filepath.Dir(worktreeDir)+"/repo/dir/1-unstaged_file")) }) // Selected path diff on a single (unstaged) file