From 6c7f04b041ae2fdce8356479298ad3fc26aa95f4 Mon Sep 17 00:00:00 2001 From: Md Mushfiqur Rahim <20mahin2020@gmail.com> Date: Mon, 25 May 2026 00:39:04 +0600 Subject: [PATCH] fix(editor): check temp close (#1339) ## What? Checks `tmpFile.Close()` when preparing the temporary file for the external editor. If closing the temp file fails, the temp file is removed and an `EditorFinishedMsg` with a contextual error is returned. ## Why? Closes #728. Ignoring `tmpFile.Close()` could hide failures that happen while flushing the written email body before opening the external editor. Returning the close error makes editor startup failures explicit and prevents continuing with an unreliable temp file. --------- Co-authored-by: FromSi --- main.go | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 0182756651fdba92dd63c4e8dd119f9be5f90fc3..1acbcafb5421a0cbcb1339c0647c7cdb4e96bb31 100644 --- a/main.go +++ b/main.go @@ -2680,19 +2680,32 @@ func openExternalEditor(body string) tea.Cmd { tmpPath := tmpFile.Name() if _, err := tmpFile.WriteString(body); err != nil { - tmpFile.Close() //nolint:errcheck,gosec - os.Remove(tmpPath) //nolint:errcheck,gosec + writeErr := err + if err := tmpFile.Close(); err != nil { + _ = os.Remove(tmpPath) + return func() tea.Msg { + return tui.EditorFinishedMsg{Err: fmt.Errorf("closing temp file after write failure: %w", err)} + } + } + _ = os.Remove(tmpPath) + return func() tea.Msg { + return tui.EditorFinishedMsg{Err: fmt.Errorf("writing temp file: %w", writeErr)} + } + } + if err := tmpFile.Close(); err != nil { + _ = os.Remove(tmpPath) return func() tea.Msg { - return tui.EditorFinishedMsg{Err: fmt.Errorf("writing temp file: %w", err)} + return tui.EditorFinishedMsg{Err: fmt.Errorf("closing temp file: %w", err)} } } - tmpFile.Close() //nolint:errcheck,gosec parts := strings.Fields(editor) args := append(parts[1:], tmpPath) //nolint:gocritic c := exec.Command(parts[0], args...) //nolint:gosec,noctx return tea.ExecProcess(c, func(err error) tea.Msg { - defer os.Remove(tmpPath) //nolint:errcheck + defer func() { + _ = os.Remove(tmpPath) + }() if err != nil { return tui.EditorFinishedMsg{Err: err} }