Skip to content

[Bug] 打开未跟踪文件的工作区 Diff 后,文件在没有任何编辑操作时被自动保存,尾部每轮多一个空行 #2377

Todo

Description

@mos1128

Platform / 平台

Windows

Area / 模块

Editor / 编辑器

Lithe version / Lithe 版本

0.8.15

Priority / 优先级

P1 / 尽快处理:使用体验大幅下降,但通常仍可继续使用

OS and architecture / 操作系统与架构

Windows 11 专业版 build 22631,x86_64

Environment / 环境信息

JDK: 21.0.2 (OpenJDK)
Maven: 3.10.0
涉事文件: 未跟踪的文本文件两个,其中一个 LF 换行、一个 CRLF 换行
JDT LS: 未启用

Steps to reproduce / 复现步骤

  1. 打开一个 Git 工作区,使「更改」列表里出现未跟踪的文本文件。
  2. 双击该文件,打开它的工作区 Diff 页签(日志里的 git.diff.open)。
  3. 不做任何输入,等待 2 秒以上(自动保存延迟约 1.5 s)。
  4. 关闭并重新打开该文件的 Diff 页签(或让工作区重新加载 Diff)。
  5. 回到文件本身:内容末尾多了一个空行。
  6. 重复 3–5:每轮再多一个,没有上限;整个过程零编辑操作。

Expected behavior / 预期行为

只读展示不应该写文件。等待任意多轮后,文件字节、mtime 和 Git 状态都应保持不变,Diff 页签不应触发任何保存。

Actual behavior / 实际行为

Diff 页签在用户零输入的情况下让文件进入“已修改”状态并被自动保存:每一轮在文件末尾追加 1 个行尾符(LF 文件追加 LF,CRLF 文件追加 CRLF),分钟级持续增长。

实测(0.8.15 / Windows,约 8 分钟内,两个文件同时被追加):

  • LF 文本文件:8,691 B -> 8,770 B(+79 个 LF)
  • CRLF 文本文件:2,866 B -> 3,088 B(+111 个 CRLF)
    尺寸是外部工具核对的原始字节;Lithe 自己的 EditorRecovery 草稿里 body 与 baseHash 的差值也正好是这些行尾符的个数,两条路径互相印证。

影响:静默改坏用户工作区文件,脚本和配置尤其致命;污染 Git 变更、让人不敢提交;用户完全无感知。副作用是 editor.log 里连续刷保存失败(见下一节):外部程序占用文件时保存失败,但循环仍在继续。

初步定位(源码阅读 + 单元测试验证,没有做整机运行级验证):

  1. desktop/internal/workbench/difftab.go:621-626 在文档已打开时调用 e.view.ShowContent(content),这一侧的 cfg.OnEdit(同文件 :639-640)接的是 a.diffEdited。
  2. editorview.(*View).ShowContent(desktop/internal/editorview/diffdecor.go:290-299)内部走 replaceFromDisk(desktop/internal/editorview/view.go:496-535),后者在文本变化时 reportEdit(change)(view.go:531-533)。这个 change 是按该侧视图的旧文本算出来的差异。
  3. 宿主的 editDocumentFrom(desktop/internal/workbench/difftab_edit.go:62-72)又把这份差异应用到文档上,于是同一处差异被应用第二次:文档多 1 个行尾符 -> 触发 OnEdit -> documentEdited 安排自动保存 -> 保存成功后 reloadDiffsOfPath 重建 Diff -> 再次 ShowContent -> 下一轮。
  4. 该侧视图的注释称它“read-only、用户从未编辑过文本”(diffdecor.go:290-292),但文档打开时 SetReadOnly(false),它其实可编辑,所以这条回报没有被任何条件挡住。
  5. 同项目的同构代码有保护:source_preview.go:287-289 与 :300-304 在调用 ShowContent 前后加了 p.diffs.mirroring++ / --,而 diffEdited(difftab_edit.go:47-60)本身在 mirroring > 0 时直接 return。difftab.go:622 这里少了这对护栏。
  6. 引入位置:difftab.go 由 0.7.0 的 Viva 迁移提交(PR Epic(0.7.0): 基于 Viva 框架的跨平台方案的迁移(#1794) #1799)新建,这行从那时起就没有护栏,不是后续 PR 引入的回归。

最小修法(供参考,不在本 Issue 内提交):给 difftab.go:622 的 ShowContent 调用加 p.diffs.mirroring++ / --,与 source_preview.go 一致;或者让 ShowContent 不再回报编辑——后者会触及 desktop/internal/editorview/**,按仓库规则还要附编辑器性能基线,成本更高。

Logs or diagnostic output / 日志或诊断输出

# editor.log(同一会话,按时间截取;产品日志未包含文件路径)
2026-11-24T02:26:26.222Z ERROR seq=97 event=editor.document.save_failed msg="Document 1 revision 78 save failed at write" document=1 error="The process cannot access the file because it is being used by another process." error_code=32 error_domain=posix revision=78 stage=write
2026-11-24T02:30:37.914Z ERROR seq=100 event=editor.document.save_failed msg="Document 1 revision 78 save failed at write" document=1 error="document changed on disk" error_code=unknown error_domain=go revision=78 stage=write
2026-11-24T02:31:28.465Z ERROR seq=108 event=editor.document.save_failed msg="Document 1 revision 78 save failed at write" document=1 error="document changed on disk" error_code=unknown error_domain=go revision=78 stage=write
2026-11-24T02:31:40.081Z ERROR seq=122 event=editor.document.save_failed msg="Document 1 revision 78 save failed at write" document=1 error="document changed on disk" error_code=unknown error_domain=go revision=78 stage=write

# app.log(同一次复现里的三次 Diff 打开)
2026-11-24T02:23:56.363Z INFO seq=94 event=app.action.performed op=action-5 msg="User action git.diff.open performed" action=git.diff.open surface=button
2026-11-24T02:24:01.588Z INFO seq=95 event=app.action.performed op=action-6 msg="User action git.diff.open performed" action=git.diff.open surface=button
2026-11-24T02:24:04.300Z INFO seq=96 event=app.action.performed op=action-7 msg="User action git.diff.open performed" action=git.diff.open surface=button

再附一个只依赖 editorview 的单元测试(放在 desktop/internal/editorview/hostmirror_test.go,未提交),证明“ShowContent 会把变更回报给宿主”本身:

package editorview

import (
	"strings"
	"testing"

	"github.com/vivaui/viva/gfx"
)

// TestShowContentMirrorsItsOwnChangeBack reproduces the feedback loop of a second view that shows
// a document's content while its OnEdit mirrors edits into that document, as the right side of a
// working-copy Diff tab does. ShowContent reports the change it applied, described against the
// view's old text; mirroring that report into the document applies it to the document a second
// time, so the document gains one line ending per round.
func TestShowContentMirrorsItsOwnChangeBack(t *testing.T) {
	file := "one\ntwo\n"

	// The document of the file, saved and clean.
	doc := New(Config{HitID: gfx.Item(1), Label: "doc", Content: Prepare([]byte(file))})
	t.Cleanup(doc.Close)

	// The side of a Diff tab starts on the pane's text, which has no break after its last line,
	// and mirrors its edits into the document.
	side := New(Config{
		HitID:   gfx.Item(2),
		Label:   "side",
		Content: Prepare([]byte(strings.TrimSuffix(file, "\n"))),
		OnEdit:  func(edit Edit) { doc.ApplyEdit(edit, true) },
	})
	t.Cleanup(side.Close)

	if got := doc.contents(); got != file {
		t.Fatalf("document starts as %q, want %q", got, file)
	}
	for round := 1; round <= 4; round++ {
		before := doc.contents()
		side.ShowContent(doc.CopyContent())
		after := doc.contents()
		t.Logf("round %d: document %q -> %q", round, before, after)
		if len(after) != len(before)+1 {
			t.Fatalf("round %d grew the document by %d bytes, want 1", round, len(after)-len(before))
		}
	}
	if got, want := doc.contents(), file+strings.Repeat("\n", 4); got != want {
		t.Fatalf("document = %q, want %q", got, want)
	}
}
$ go test ./internal/editorview/ -run TestShowContentMirrorsItsOwnChangeBack -count=1 -v
=== RUN   TestShowContentMirrorsItsOwnChangeBack
    hostmirror_test.go:39: round 1: document "one\ntwo\n" -> "one\ntwo\n\n"
    hostmirror_test.go:39: round 2: document "one\ntwo\n\n" -> "one\ntwo\n\n\n"
    hostmirror_test.go:39: round 3: document "one\ntwo\n\n\n" -> "one\ntwo\n\n\n\n"
    hostmirror_test.go:39: round 4: document "one\ntwo\n\n\n\n" -> "one\ntwo\n\n\n\n\n"
--- PASS: TestShowContentMirrorsItsOwnChangeBack (0.00s)
ok  	github.com/1lck/Lithe-IDEA/desktop/internal/editorview	0.095s

Screenshots or recordings / 截图或录屏

未提供。该缺陷的可见结果是文件字节变化,截图只能拍到“文件被标记为已修改”,无法体现循环过程;上面的日志、字节对账数据和可直接运行的单元测试是替代证据。

Checklist / 提交前检查

  • I searched existing issues for duplicates. / 我已搜索现有 Issue,确认没有重复报告。
  • I removed secrets and personal data from the report. / 我已从报告中移除敏感信息和个人数据。

Activity

  1. added
    bug
    on Nov 24, 2026
  2. added
    review: medium
    on Nov 24, 2026
  3. xiaoyumuxi commented on Nov 24, 2026

    @xiaoyumuxi
    Collaborator

    好的,好的会尽快处理的

  4. self-assigned this
    on Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugissue-form:bugreview: medium

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions