{"record":{"id":"5332e7999b6eaf9b","repo":"siyuan-note/siyuan","slug":"create-import-dir-failed-s-5332e7","errorCode":null,"errorMessage":"create import dir failed: %s","messagePattern":"create import dir failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/webfetch.go","lineNumber":81,"sourceCode":"\n\tcontentType := resp.Header.Get(\"Content-Type\")\n\tmaxReadBytes := int64(maxWebFetchBytes)\n\tif !strings.HasPrefix(contentType, \"text/html\") && !strings.HasPrefix(contentType, \"text/plain\") {\n\t\tmaxReadBytes = maxWebFetchFileBytes\n\t}\n\tif resp.ContentLength > maxReadBytes {\n\t\treturn \"\", errors.New(\"response too large\")\n\t}\n\n\tbody, err := io.ReadAll(io.LimitReader(resp.Body, maxReadBytes))\n\tif err != nil {\n\t\treturn \"\", errors.New(\"read body failed: \" + err.Error())\n\t}\n\n\tif !strings.HasPrefix(contentType, \"text/html\") && !strings.HasPrefix(contentType, \"text/plain\") {\n\t\timportDir := filepath.Join(TempDir, \"import\")\n\t\tif merr := os.MkdirAll(importDir, 0755); merr != nil {\n\t\t\treturn \"\", errors.New(\"create import dir failed: \" + merr.Error())\n\t\t}\n\t\tfilename := extractFilename(rawURL, contentType)\n\t\tfilePath := filepath.Join(importDir, filename)\n\t\tif werr := os.WriteFile(filePath, body, 0644); werr != nil {\n\t\t\treturn \"\", errors.New(\"write file failed: \" + werr.Error())\n\t\t}\n\t\treturn fmt.Sprintf(\"Saved to: %s (%d bytes)\", filePath, len(body)), nil\n\t}\n\n\thtmlStr := string(body)\n\n\tisHTML := strings.HasPrefix(contentType, \"text/html\")\n\tif !isHTML {\n\t\treturn truncateRunes(htmlStr, maxWebFetchChars), nil\n\t}\n\n\tif htmlStr == \"\" {\n\t\treturn \"\", nil","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/251596fc0de2f9528c00c224252fd073a99973f4/kernel/util/webfetch.go#L63-L99","documentation":"Returned at webfetch.go:81 when os.MkdirAll(filepath.Join(TempDir, \"import\"), 0755) fails. This happens only on the non-text download branch (content type is not text/html or text/plain), after the body has already been read successfully. It is a local-filesystem error, not a network error.","triggerScenarios":"TempDir resolves to a read-only or non-existent base path, the process lacks create permission on the target, the disk is full, or the composed path is invalid (e.g. embedded NUL, path-too-long on Windows).","commonSituations":"Snap/Flatpak confinement denying write outside the sandbox, iOS sandbox permission change, running off a read-only mount, TempDir pointing at a removed tmpfs, antivirus interfering on Windows.","solutions":["Check util.TempDir value at runtime and confirm the directory can be created with `mkdir -p`.","Free disk space on the volume holding TempDir.","Fix ownership/permissions so the SiYuan process can write under TempDir.","On sandboxed installs, ensure TempDir is inside the writable sandbox area."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Confirm the import target is creatable before triggering a download.\nfunc importDirWritable() error {\n    dir := filepath.Join(util.TempDir, \"import\")\n    return os.MkdirAll(dir, 0755)\n}","typeGuard":"func isCreateImportDirFailed(err error) bool {\n    return err != nil && strings.HasPrefix(err.Error(), \"create import dir failed:\")\n}","tryCatchPattern":"if err := importDirWritable(); err != nil {\n    return fmt.Errorf(\"cannot save downloads: %w\", err)\n}\nout, err := util.WebFetch(raw, \"markdown\")","preventionTips":["Ensure TempDir resolves to a writable location inside the install's sandbox.","On snap/flatpak/iOS, verify TempDir is within the writable sandbox area.","Pre-check disk space and directory writability before fetching non-text content."],"tags":["filesystem","permissions","tempdir"],"backgroundTag":null,"analyzedSha":"251596fc0de2f9528c00c224252fd073a99973f4","analyzedAt":"2026-08-12T21:18:37.123Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}