From e7de0bd93a616478d47ac4fbc8e715367e7a2588 Mon Sep 17 00:00:00 2001 From: alwaystrue Date: Wed, 4 Nov 2026 17:40:04 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=97=AA=E9=80=80=E7=9C=9F=E5=87=B6?= =?UTF-8?q?=E2=80=94=E2=80=94=E8=A3=85=E8=BD=BD=E5=8E=86=E5=8F=B2=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E5=90=8E=20transInflight=20=E4=B8=BA=20nil?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit crash.log 实锤:panic: assignment to entry in nil map, docTranslation(hover 渲染帧内)。 合并判空是错的:loadTranslations 只装 a.trans,缓存有数据后 a.trans 永远非 nil,跳过了 transInflight 初始化——hover 一个 未缓存符号就往 nil map 写,主线程渲染帧直接炸。两次崩溃同因。 修复:两个 map 分别判空;loadTranslations 同时初始化两个。 回归测试:TestDocTranslationNilInflightWithLoadedCache。 --- internal/app/completion_test.go | 22 ++++++++++++++++++++++ internal/app/translate.go | 6 ++++++ 2 files changed, 28 insertions(+) diff --git a/internal/app/completion_test.go b/internal/app/completion_test.go index e63db33..0488d33 100644 --- a/internal/app/completion_test.go +++ b/internal/app/completion_test.go @@ -72,3 +72,25 @@ func TestHoverTranslationInProgress(t *testing.T) { t.Fatalf("翻译中应显示原文, got %q", got) } } + +// TestDocTranslationNilInflightWithLoadedCache 复现闪退:启动时装载了历史 +// 缓存(a.trans 非 nil)但 transInflight 仍为 nil,此时 hover 一个未缓存的 +// 文档——不得往 nil map 写(crash.log: assignment to entry in nil map)。 +func TestDocTranslationNilInflightWithLoadedCache(t *testing.T) { + a := &App{ + trans: map[string]string{"cached": "已缓存译文"}, // 只有 trans 被装载 + set: settings{TranslateDocs: true, AIBaseURL: "x", AIAPIKey: "y", AIModel: "z"}, + } + defer func() { + if r := recover(); r != nil { + t.Fatalf("不得 panic: %v", r) + } + }() + got := a.docTranslation("some uncached doc text") + if got != "some uncached doc text" { + t.Fatalf("未缓存应返回原文, got %q", got) + } + if a.transInflight == nil { + t.Fatal("transInflight 应被初始化") + } +} diff --git a/internal/app/translate.go b/internal/app/translate.go index f61c21b..bba318d 100644 --- a/internal/app/translate.go +++ b/internal/app/translate.go @@ -36,8 +36,13 @@ func (a *App) docTranslation(text string) string { if a.transInflight[key] { return text } + // 两个 map 分别判空:loadTranslations 只装 a.trans(有历史缓存时它 + // 非 nil),transInflight 仍是 nil——之前合并判空导致往 nil map 写, + // 渲染帧里直接 panic 闪退(crash.log 实锤)。 if a.trans == nil { a.trans = map[string]string{} + } + if a.transInflight == nil { a.transInflight = map[string]bool{} } a.transInflight[key] = true @@ -98,6 +103,7 @@ func (a *App) loadTranslations() { return } a.trans = map[string]string{} + a.transInflight = map[string]bool{} for k, v := range m { if k != "" && v != "" { // 旧版缓存 key 是原文,统一规范化,能被预热与懒翻译共同命中。