{"record":{"id":"512d9b8d20df5e00","repo":"lima-vm/lima","slug":"failed-to-unmarshal-xml-w","errorCode":null,"errorMessage":"failed to unmarshal xml: %w","messagePattern":"failed to unmarshal xml: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/imgutil/nativeimgutil/asifutil/asif_darwin.go","lineNumber":86,"sourceCode":"\tresizeArgs := []string{\"image\", \"resize\", \"--size\", fmt.Sprintf(\"%d\", size), path}\n\tif output, err := exec.CommandContext(context.Background(), \"diskutil\", resizeArgs...).CombinedOutput(); err != nil {\n\t\treturn fmt.Errorf(\"failed to resize ASIF image %#q: %w: %s\", path, err, output)\n\t}\n\treturn nil\n}\n\ntype AttachedDisk struct {\n\tDisk      string // whole disk device, e.g. \"disk4\" (GUID_partition_scheme)\n\tContainer string // APFS container partition, e.g. \"disk4s2\" (Apple_APFS)\n\tData      string // data volume device, e.g. \"disk7s5\"\n}\n\n// parseDiskutilImageAttachOutput parses the output of `diskutil image attach -plist -nomount <disk>`\n// and returns the attached disk information.\nfunc parseDiskutilImageAttachOutput(xmlStr string) (*AttachedDisk, error) {\n\tvar p plist.Plist\n\tif err := xml.Unmarshal([]byte(xmlStr), &p); err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to unmarshal xml: %w\", err)\n\t}\n\n\tif p.Value.Dict == nil {\n\t\treturn nil, errors.New(\"unexpected plist format: missing root dict\")\n\t}\n\n\tseVal, ok := p.Value.Dict[\"system-entities\"]\n\tif !ok || len(seVal.Array) == 0 {\n\t\treturn nil, errors.New(\"unexpected plist format: missing system-entities array\")\n\t}\n\n\tresult := &AttachedDisk{}\n\tfor _, devEnt := range seVal.Array {\n\t\tdevDict := devEnt.Dict\n\t\tif devDict == nil {\n\t\t\tcontinue\n\t\t}\n\t\tdevEntry, hasDevEntry := devDict[\"dev-entry\"]","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/lima-vm/lima/blob/dd909d0973cd84fa35f9e1693181b4585ea616c1/pkg/imgutil/nativeimgutil/asifutil/asif_darwin.go#L68-L104","documentation":"parseDiskutilImageAttachOutput parses the plist XML emitted by `diskutil image attach -plist -nomount`. This error wraps a failure from encoding/xml Unmarshal, meaning the output was not well-formed XML plist. diskutil's output is treated as a contract; malformed output breaks parsing.","triggerScenarios":"DiskutilImageAttachNoMount receives output that xml.Unmarshal rejects: truncated output, stderr mixed into stdout, a localized/error message instead of plist, or plist format variations encoding/xml cannot map into plist.Plist.","commonSituations":"Older/newer macOS emitting a different plist structure; attaching fails and diskutil prints a human-readable error instead of plist; output truncation from process signal or pipe buffering issues.","solutions":["Log the raw xmlStr alongside the wrapped error to inspect what diskutil returned","Check the wrapped Unmarshal error for the malformed offset/element","Verify the attach actually succeeded; handle non-plist error output before parsing","Account for macOS-version differences in the plist schema used by the plist package"],"exampleFix":"// before\nout, err := exec.Command(\"diskutil\", \"image\", \"attach\", \"--plist\", \"-nomount\", path).Output()\n// after\nvar stderr bytes.Buffer\ncmd := exec.Command(\"diskutil\", \"image\", \"attach\", \"--plist\", \"-nomount\", path)\ncmd.Stderr = &stderr\nout, err := cmd.Output()\nif err != nil {\n    return fmt.Errorf(\"attach failed: %w: %s\", err, stderr.String()) // avoid parsing non-plist output\n}","handlingStrategy":"try-catch","validationCode":"// preflight: check output looks like a plist before parsing\nfunc looksLikePlist(s string) bool { return strings.HasPrefix(strings.TrimSpace(s), \"<?xml\") }","typeGuard":null,"tryCatchPattern":"disk, err := asifutil.DiskutilImageAttachNoMount(path)\nif err != nil {\n    if strings.Contains(err.Error(), \"failed to unmarshal xml\") {\n        log.Printf(\"diskutil returned non-plist output; attach likely failed\")\n    }\n    return err\n}","preventionTips":["Capture stderr separately so error text never mixes into plist stdout","Log raw diskutil output on parse failure for diagnosis","Pin/test against the macOS versions you support"],"tags":["macos","plist","xml","parsing"],"backgroundTag":"plist-parse-failed","analyzedSha":"dd909d0973cd84fa35f9e1693181b4585ea616c1","analyzedAt":"2026-09-01T14:24:59.842Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}