[Bug] 预览同步报错:null is not an object (evaluating 'plan.parts.filter')
现象
设置 → WebDAV / S3 / GitHub 同步(S3,Cloudflare R2)里点「预览同步」,弹出报错:
null is not an object (evaluating 'plan.parts.filter')
预览窗口打不开。直接同步本身正常,两台 Mac 都已同步成功。
复现
- 两台 macOS 电脑配置同一个 S3(R2)同步,都已完成一次同步。
- 在其中一台点「预览同步」(此时本机和服务器基本一致)。
原因推测
internal/gui/assets/syncfiles.js 的 askSyncPreview 里:
const by = (d) => plan.parts.filter((p) => p.do === d);
davsync.Plan.Parts 是 []PartPlan,json:"parts" 没有 omitempty。如果 syncOnce 在某条路径(比如没有变化、提前返回)没调用 previewed,Parts 就是 nil,序列化成 "parts": null,前端直接 .filter 就崩了。
建议修法
任选其一(或都做):
- 前端:
(plan.parts || []).filter(...),这样会走到已有的「Nothing to sync: this computer and the server are the same.」分支。 - 后端:
preview()返回前if p.Parts == nil { p.Parts = []PartPlan{} }。
已复现,下面是基于 8634c4ad0890f9159e0367873c357eaded61f714 的候选补丁,按 docs/contributors.md 的社区 issue 贡献方式提供,尚未合入,也未创建外部 PR。
确认到的路径是:preview() 虽然清掉 st.Server 强制读取服务器文件,syncOnce() 仍会在 sum(data) == st.Sum 且本地哈希未变时提前返回。因此正常的“已同步、无变化”场景确实会得到 {"parts":null,...},不必依赖异常响应。
采用报告中提出的后端方向:在两个入口共用的 preview() 初始化空 Parts 数组。非空计划仍按原逻辑 append,错误仍原样返回,不改同步策略、前端代码或凭据处理。空计划的 token 因 null → [] 会改变,但生成和复核使用同一函数;跨版本旧 token 会安全地要求重新预览,而不是错误执行。
验证
Linux amd64 / Go 1.26.8,临时 HOME,固定独立 Go 缓存;测试只使用项目现有的本地 fakeDAV,不连接真实同步账号。
- 新测试在旧生产代码上,两入口都真实失败:
--- FAIL: TestPreviewUnchangedPartsArray --- FAIL: .../saved unchanged preview parts = null, want []; response: {"parts":null,"token":"467155419481a4c7"} --- FAIL: .../form unchanged preview parts = null, want []; response: {"parts":null,"token":"467155419481a4c7"} - 修复后
go test -race -tags nogui ./internal/davsync -run '^TestPreview' -count=10:通过,6.432s。 - 完整同步包
go test -race -tags nogui ./internal/davsync -count=1:通过,193.309s。 go vet -tags nogui ./internal/davsync、改动 Go 文件的gofmt -l、git diff --check:干净。- 回归测试覆盖:先完成一次真实 fakeDAV 同步,两预览入口 JSON 都是
parts:[],等价 token 一致,预览不写远端,SyncPreviewed接受新 token 且无变化时不重复写入。已有TestPreviewThenSync的非空计划和过期 token 测试也通过。 - 独立只读复核未发现阻断问题。
**验证边界:**未跑完整仓库 suite/build、macOS/Windows 或真实 R2。尝试了现有浏览器 suite,但浏览器在测试动作前即因环境问题无法启动:系统 Chromium 报 socket() failed: Operation not permitted,WebKit 缺运行库;因此不声称 UI 自动化通过。补丁只改后端响应初始化,前端布局与交互未修改。
根因与修法来自 @Caho1;采用时请按仓库规则保留报告者和补丁作者 credit,其中报告者为 Co-authored-by: Caho1 <[email protected]>。实现与验证使用了 AI 辅助。
完整补丁
diff --git a/docs/subsystems/sync-backup.md b/docs/subsystems/sync-backup.md
index bc7caf5..4aecd13 100644
--- a/docs/subsystems/sync-backup.md
+++ b/docs/subsystems/sync-backup.md
@@ -229,7 +229,9 @@ it does, and take it back.
before any write, here or on the server. A part's plan says `in`, `up`,
`same` or `off`, whether it replaces a change made here (`replaces`), and
its items added, removed or changed; memory, sign-ins and plugins removed
- by another computer are counted. `Plan.Token` hashes what the sync would
+ by another computer are counted. The JSON `parts` field is always an array,
+ including `[]` when the unchanged-file shortcut has no part decisions
+ (`TestPreviewUnchangedPartsArray`, #7078). `Plan.Token` hashes what the sync would
do; `SyncPreviewed(token)` runs only while a new plan has the same token,
else `ErrPlanChanged` and the GUI shows the new plan. A preview from the
form (`PreviewWith`) is of the unsaved setup, and its Save and sync is
diff --git a/internal/davsync/plan.go b/internal/davsync/plan.go
index 3d702ea..b4984ce 100644
--- a/internal/davsync/plan.go
+++ b/internal/davsync/plan.go
@@ -134,7 +134,8 @@ func preview(ctx context.Context, c Config, st state) (Plan, error) {
st.Server = version{}
st.Local, st.Remote = maps.Clone(st.Local), maps.Clone(st.Remote)
st.Own, st.Brought, st.Replaced = maps.Clone(st.Own), maps.Clone(st.Brought), maps.Clone(st.Replaced)
- var p Plan
+ // Even the unchanged-file shortcut must give clients an array (#7078).
+ p := Plan{Parts: []PartPlan{}}
if err := syncOnce(ctx, c, &st, &p); err != nil {
return Plan{}, err
}
diff --git a/internal/davsync/preview_empty_test.go b/internal/davsync/preview_empty_test.go
new file mode 100644
index 0000000..9147947
--- /dev/null
+++ b/internal/davsync/preview_empty_test.go
@@ -0,0 +1,66 @@
+package davsync
+
+import (
+ "context"
+ "encoding/json"
+ "testing"
+)
+
+// #7078 (Caho1): after a successful sync the unchanged-file shortcut
+// leaves no part decisions. Both preview entry points must still encode
+// parts as an array, which the Settings preview filters.
+func TestPreviewUnchangedPartsArray(t *testing.T) {
+ fake, cfg := scopeServer(t)
+ newComputer(t).use(t)
+ if err := Configure(cfg); err != nil {
+ t.Fatal(err)
+ }
+ syncOK(t)
+ puts := fake.puts
+ token := ""
+ for _, tc := range []struct {
+ name string
+ preview func() (Plan, error)
+ }{
+ {"saved", func() (Plan, error) { return Preview(context.Background()) }},
+ {"form", func() (Plan, error) { return PreviewWith(context.Background(), cfg) }},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ p, err := tc.preview()
+ if err != nil {
+ t.Fatal(err)
+ }
+ b, err := json.Marshal(p)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var wire struct {
+ Parts json.RawMessage `json:"parts"`
+ }
+ if err := json.Unmarshal(b, &wire); err != nil {
+ t.Fatal(err)
+ }
+ if string(wire.Parts) != "[]" {
+ t.Fatalf("unchanged preview parts = %s, want []; response: %s", wire.Parts, b)
+ }
+ if p.Token == "" {
+ t.Fatal("preview has no token")
+ }
+ if token != "" && token != p.Token {
+ t.Fatalf("equivalent preview tokens differ: %s != %s", token, p.Token)
+ }
+ token = p.Token
+ })
+ }
+ if fake.puts != puts {
+ t.Fatalf("preview wrote the server: %d puts", fake.puts-puts)
+ }
+ if !t.Failed() {
+ if err := SyncPreviewed(context.Background(), token); err != nil {
+ t.Fatal(err)
+ }
+ if fake.puts != puts {
+ t.Fatalf("unchanged previewed sync wrote the server: %d puts", fake.puts-puts)
+ }
+ }
+}
VERDICT: PASS on @yhdhaidong's patch — root cause and fix direction from @Caho1's report. Carried as commit 8f3dc2df6 on branch verify/7078-preview-parts (author kept yhdhaidong, Co-authored-by: Caho1 in the commit body as requested), rebased onto current upstream/main (4716a77ff).
What I verified independently (not trusting the write-up):
- Red on main: the new test alone, on a clean tree at
4716a77ff, reproduces the reported face at both preview entries, byte-for-byte as on their Linux run —unchanged preview parts = null, want []; response: {"parts":null,"token":"467155419481a4c7"}(savedandform). - Green with the fix:
TestPreviewUnchangedPartsArraypasses;go test -tags nogui ./internal/davsync -run '^TestPreview' -count=10passes (13.9s); the full packagego test -tags nogui -count=1 -timeout 25m ./internal/davsyncshows one red,TestSyncSkillsBySourceAndLeftOut, which is this Windows machine's symlink-privilege precondition (it fails identically on a cleanupstream/main, the same family as the gateway package'sTestSweepBridgeProjectsred here); everything else green. - Ablation: with only
internal/davsync/plan.goreverted to the oldvar p Planand the test kept (git diff --stat HEAD: 1 file), the same test goes red at both entries with the exact reported text; restored, it is green and the tree is clean. - Applies clean and
git amapplies it as is:git format-patch -1 --zero-commitonto a freshupstream/maincheckout lands with the tree hash identical (3b0f4c69ae92f78be…) and the author preserved. Patch SHA-256 first 16:3c10398ea5aae1c9. go vet -tags nogui ./internal/davsyncand gofmt on the changed files: clean.
Found along the way: their base 8634c4ad is an ancestor of the current tip; one unrelated commit has touched internal/davsync since (3132fa1cd, library rules), and the diff applies untouched. The Preview/PreviewWith wrappers (plan.go:88/109) overwrite their local var p Plan with preview()'s return, so the fix at the shared preview() (plan.go:138) is the single bottleneck both entries route through — no third entry exists. The token of an unchanged plan does change as they note (the hashed JSON gains [] where it had null); a token saved by the old build fails safe as ErrPlanChanged and the GUI shows the new plan.
Siblings: rg over the Plan producers finds no other nil-Parts producer reaching clients. One face left for a later look, no report touches it: View.Applied.Parts (davsync.go:630, copied from the stored applied record) is a different wire shape from the preview plan and could in principle marshal null after an empty apply.
Not verified: a real R2/DAV server; the GUI browser flow (no working browser suite on this Windows machine either — their Linux run hit the same wall); -race (no C toolchain here — their Linux -race full-package run stands as reported).
AI use: independent verification by GLM-5.3-Flash (ZCode) working for ggbdpq; the fix is yhdhaidong's.
补充一份复现环境:我用的同步后端是 GitHub 仓库,不是 WebDAV/S3,所以这个问题与后端类型无关。
- magpie 0.1.4685(CFBundleVersion 0.1.4685,2026-11-23 构建,从 usemagpie.ai 安装包安装)
- macOS 26.4(25E246),arm64
- 同步后端:
github://<user>/<repo>(GitHub 私有仓库),两台 Mac 都各自完成过一次同步,applied.json有记录 - 现象:设置 › 同步与备份 ›「预览…」点下去,底部状态条立刻显示
null is not an object (evaluating 'plan.parts.filter');直接「立即同步」正常,数据不受影响
一个额外的观察,应该能支持「syncOnce 提前返回」这个根因判断:只要本机存在任意一处未同步的改动(哪怕只是把某个同步项关掉再打开),预览就能正常弹窗;两端完全一致时 100% 报错。这也基本排除了网络、凭据、后端类型,因为报错发生在任何远端写动作之前。
在 0.1.4685 上核对到的三处:
internal/davsync/davsync.go:1519—if sum(data) == st.Sum && maps.Equal(L, st.Local) && ... { return nil }这条路径不调用previewed(),Plan.Parts保持 nilinternal/davsync/plan.go:133preview()照常序列化,返回{"parts":null,"token":"..."}internal/gui/assets/syncfiles.js:434—plan.parts.filter(...)没有判空,直接抛 TypeError
倾向 @yhdhaidong 的补丁方向(在共享的 preview() 里初始化空的 Parts):前端加 || [] 只能消掉崩溃,后端补 [] 能让 token 的语义也正常——空计划(不变)也能稳定比较,避免以后同类字段再被写成 null。
另外确认一下现状:v0.1.4732 和当前 main 的 plan.go / syncfiles.js 都还是这样,所以「升级到最新版」目前规避不了,只能绕过预览。
和 #7855 是同一个问题(两边都没变化时预览返回 parts: null,界面在 plan.parts.filter 上报错)。v0.1.4910(b10ae36e3)已修,采用 qq01-hub 的修复,方向和 yhdhaidong 在这里的补丁一样(服务端返回 []),另外界面也把 null 当空列表。麻烦更新后在设置 > 同步里点一下预览确认,好了请回复一声。