## 事象
[[Lazygit]]でコミットメッセージにフォーカスし `y` キーを押す。
![[Pasted image 20250121212647.png]]
いずれのメニューを選んでも以下のエラーになる。
![[Pasted image 20250121212921.png]]
ログは以下のようになりエラーは出ていない。
```
Copy commit message to clipboard
Copying 'message...' to clip board
```
### 環境
| 対象 | バージョン |
| ---------------- | -------------------- |
| [[Lazygit]] | 0.45.2 |
| [[Git]] | 2.48.1 |
| [[Ubuntu]] | 24.04.1 LTS (in WSL) |
| [[WSL]] | 2.2.4.0 |
| [[wl-clipboard]] | 2.2.1 |
| [[Windows 11]] | 10.0.22631 |
- [[Lazygit]]を0.44.1にダウングレードしても問題は解消しない
## 原因
[[Lazygit]]の `Copy to clipboard` コマンドで利用している[[wl-clipboard]]が動いていないから。
```error
$ wl-copy
Failed to connect to a Wayland server: No such file or directory
Note: WAYLAND_DISPLAY is set to wayland-0
Note: XDG_RUNTIME_DIR is set to /run/user/1000/
Please check whether /run/user/1000//wayland-0 socket exists and is accessible.
```
## 解決方法
以下を参照。
<div class="link-card-v2">
<div class="link-card-v2-site">
<img class="link-card-v2-site-icon" src="https://publish-01.obsidian.md/access/35d05cd1bf5cc500e11cc8ba57daaf88/favicon-64.png" />
<span class="link-card-v2-site-name">Minerva</span>
</div>
<div class="link-card-v2-title">
📝wl-clipboadが動かない
</div>
<div class="link-card-v2-content">WSLでwl-clipboardのコマンド (`wl-copy` など) が動かない。WSLのUbuntu 22をUbuntu 24に更新したあとから発生している。Failed to connect to a Wayland server: No such file or directory</div>
<img class="link-card-v2-image" src="https://publish-01.obsidian.md/access/35d05cd1bf5cc500e11cc8ba57daaf88/Notes/attachments/troubleshooting.webp" />
<a data-href="📝wl-clipboadが動かない" class="internal-link"></a>
</div>
%%[[📝wl-clipboadが動かない]]%%
## 参考
### 詳細調査メモ
[`pkg/commands/oscommands/os.go` 287行目](https://github.com/jesseduffield/lazygit/blob/40d6800fd35ad2d5c6d96d6f08ffa42d4c764ad3/pkg/commands/oscommands/os.go?plain=1#L287)
```go
func (c *OSCommand) CopyToClipboard(str string) error {
escaped := strings.Replace(str, "\n", "\\n", -1)
truncated := utils.TruncateWithEllipsis(escaped, 40)
msg := utils.ResolvePlaceholderString(
c.Tr.Log.CopyToClipboard,
map[string]string{
"str": truncated,
},
)
c.LogCommand(msg, false)
if c.UserConfig().OS.CopyToClipboardCmd != "" {
// ここにはきてない
cmdStr := utils.ResolvePlaceholderString(c.UserConfig().OS.CopyToClipboardCmd, map[string]string{
"text": c.Cmd.Quote(str),
})
return c.Cmd.NewShell(cmdStr).Run()
}
// ここにきてる
return clipboard.WriteAll(str)
}
```
[`vendor.github.com/atotto/clipboard/clipboard_unix.go`](https://github.com/jesseduffield/lazygit/blob/40d6800fd35ad2d5c6d96d6f08ffa42d4c764ad3/vendor/github.com/atotto/clipboard/clipboard_unix.go?plain=1#L129) でコマンドに `wl-copy` を使おうとしていることは分かる。
```go
func writeAll(text string) error {
if Unsupported {
return missingCommands
}
// wl-copy
copyCmd := getCopyCommand()
in, err := copyCmd.StdinPipe()
if err != nil {
return err
}
if err := copyCmd.Start(); err != nil {
return err
}
if _, err := in.Write([]byte(text)); err != nil {
return err
}
if err := in.Close(); err != nil {
return err
}
return copyCmd.Wait()
}
```
`clipboard.WriteAll(str)` が原因。`copyCmd` が `wl-copy` であることは確認。更に `go/1.23.1/src/os/exec/exec.go` の `Wait()` で問題が発生していそう。
```go
func (c *Cmd) Wait() error {
if c.Process == nil {
return errors.New("exec: not started")
}
if c.ProcessState != nil {
return errors.New("exec: Wait was already called")
}
state, err := c.Process.Wait()
if err == nil && !state.Success() {
err = &ExitError{ProcessState: state}
}
c.ProcessState = state
var timer *time.Timer
if c.ctxResult != nil {
watch := <-c.ctxResult
timer = watch.timer
// If c.Process.Wait returned an error, prefer that.
// Otherwise, report any error from the watchCtx goroutine,
// such as a Context cancellation or a WaitDelay overrun.
if err == nil && watch.err != nil {
err = watch.err
}
}
if goroutineErr := c.awaitGoroutines(timer); err == nil {
// Report an error from the copying goroutines only if the program otherwise
// exited normally on its own. Otherwise, the copying error may be due to the
// abnormal termination.
err = goroutineErr
}
closeDescriptors(c.parentIOPipes)
c.parentIOPipes = nil
return err
}
```
[[wl-clipboard]]コマンドがそもそも動いていない。
```error
$ wl-copy
Failed to connect to a Wayland server: No such file or directory
Note: WAYLAND_DISPLAY is set to wayland-0
Note: XDG_RUNTIME_DIR is set to /run/user/1000/
Please check whether /run/user/1000//wayland-0 socket exists and is accessible.
```
[[Wayland]]サーバが起動してない。
```console
$ ll /run/user/1000/wayland-0
"/run/user/1000/wayland-0": No such file or directory (os error 2)
```
```console
$ echo $WAYLAND_DISPLAY
wayland-0
$ echo $XDG_RUNTIME_DIR
/run/user/1000
```