{"record":{"id":"61b57467bc73bc4e","repo":"siyuan-note/siyuan","slug":"failed-to-write-file-paths-to-pasteboard","errorCode":null,"errorMessage":"failed to write file paths to pasteboard","messagePattern":"failed to write file paths to pasteboard","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/clipboard_darwin.go","lineNumber":102,"sourceCode":"\t// 分配 C 的 char* 数组，便于传入 Objective-C\n\tcPaths := make([]*C.char, len(paths))\n\tfor i, p := range paths {\n\t\tcPaths[i] = C.CString(p)\n\t}\n\tdefer func() {\n\t\tfor _, c := range cPaths {\n\t\t\tC.free(unsafe.Pointer(c))\n\t\t}\n\t}()\n\t// 取首元素地址作为 const char** 传入\n\tret := C.writeFilePathsToPasteboard((**C.char)(unsafe.Pointer(&cPaths[0])), C.int(len(paths)))\n\tswitch ret {\n\tcase 0:\n\t\treturn nil\n\tcase -2:\n\t\treturn errors.New(\"no valid file paths to write (invalid UTF-8 or path)\")\n\tdefault:\n\t\treturn errors.New(\"failed to write file paths to pasteboard\")\n\t}\n}\n","sourceCodeStart":84,"sourceCodeEnd":105,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/util/clipboard_darwin.go#L84-L105","documentation":"The macOS cgo helper writeFilePathsToPasteboard returns a generic failure code (anything other than 0 or -2) when writing file paths to NSPasteboard fails. Go maps that to this opaque error; the underlying Cocoa error details are not propagated.","triggerScenarios":"Calling util.WriteFilePaths on macOS when the NSPasteboard write fails — e.g. pasteboard unavailable, out of memory, or an unexpected return code from the Objective-C helper.","commonSituations":"Pasteboard contention with another app holding the pasteboard, sandboxed/oddly-permissioned environments, or a bug in the helper returning an unmapped code.","solutions":["Retry the write once after a short delay (pasteboard may be transiently busy)","Verify paths exist and are accessible, then retry","If persistent, file/diagnose in the helper — the return code is unmapped so add logging around the C call"],"exampleFix":"// before\nif err := util.WriteFilePaths(paths); err != nil { return err }\n// after\nif err := util.WriteFilePaths(paths); err != nil {\n    time.Sleep(50 * time.Millisecond)\n    if err2 := util.WriteFilePaths(paths); err2 != nil { return err2 }\n}","handlingStrategy":"retry","validationCode":"for _, p := range paths { if _, err := os.Stat(p); err != nil { return err } }","typeGuard":null,"tryCatchPattern":"if err := util.WriteFilePaths(paths); err != nil {\n    time.Sleep(100 * time.Millisecond)\n    if retryErr := util.WriteFilePaths(paths); retryErr != nil {\n        // report persistent pasteboard failure\n    }\n}","preventionTips":["Retry transient pasteboard writes once before surfacing the error","Close apps that aggressively lock NSPasteboard","Log the unmapped return code from the C helper to diagnose recurring failures"],"tags":["clipboard","macos","pasteboard","cgo"],"backgroundTag":"api-error-response","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}