{"record":{"id":"15bf407938559177","repo":"can1357/oh-my-pi","slug":"tar-directory-formatarchivepathforerror-normali","errorCode":null,"errorMessage":"Tar directory '${formatArchivePathForError(normalized)}' cannot contain file data","messagePattern":"Tar directory '(.+?)' cannot contain file data","errorType":"exception","errorClass":"ArchiveError","httpStatus":null,"severity":"error","filePath":"packages/utils/src/ar/tar.ts","lineNumber":749,"sourceCode":"\t\t\tdirectory ? 0x35 : 0x30,\n\t\t),\n\t);\n\tif (!directory) appendPayload(parts, payload);\n}\n\n/** Encode files as a deterministic ustar archive, using PAX records for overflow paths. */\nexport async function encodeTar(members: Iterable<readonly [string, Uint8Array]>): Promise<Uint8Array> {\n\tconst parts: Uint8Array[] = [];\n\tconst kinds = new Map<string, \"directory\" | \"file\">();\n\tlet sequence = 0;\n\tfor (const [rawPath, bytes] of members) {\n\t\tconst directory = rawPath.endsWith(\"/\") || rawPath.endsWith(\"\\\\\");\n\t\tconst normalized = normalizeArchiveEntryPath(rawPath);\n\t\tif (!normalized) throw new ArchiveError(`Invalid tar member path '${formatArchivePathForError(rawPath)}'`);\n\t\tconst pathBytes = TEXT_ENCODER.encode(normalized);\n\t\tif (pathBytes.byteLength === 0) throw new ArchiveError(\"Invalid empty tar member path\");\n\t\tif (directory && bytes.byteLength !== 0) {\n\t\t\tthrow new ArchiveError(`Tar directory '${formatArchivePathForError(normalized)}' cannot contain file data`);\n\t\t}\n\t\tconst segments = normalized.split(\"/\");\n\t\tfor (let index = 1; index < segments.length; index++) {\n\t\t\tconst parent = segments.slice(0, index).join(\"/\");\n\t\t\tconst kind = kinds.get(parent);\n\t\t\tif (kind === \"file\")\n\t\t\t\tthrow new ArchiveError(`Tar member '${formatArchivePathForError(parent)}' is not a directory`);\n\t\t\tif (kind === \"directory\") continue;\n\t\t\tkinds.set(parent, \"directory\");\n\t\t\tappendTarEntry(parts, parent, new Uint8Array(0), true, sequence++);\n\t\t}\n\t\tconst existing = kinds.get(normalized);\n\t\tif (existing) {\n\t\t\tif (directory && existing === \"directory\") continue;\n\t\t\tthrow new ArchiveError(`Duplicate tar member path '${formatArchivePathForError(normalized)}'`);\n\t\t}\n\t\tkinds.set(normalized, directory ? \"directory\" : \"file\");\n\t\tappendTarEntry(parts, normalized, bytes, directory, sequence++);","sourceCodeStart":731,"sourceCodeEnd":767,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/utils/src/ar/tar.ts#L731-L767","documentation":"A member whose raw path ends with '/' or '\\' is treated as a directory entry, but tar directory entries carry no payload. The encoder throws when such an entry was given non-empty bytes, since there is no valid way to store file data under a directory-typed header.","triggerScenarios":"createTar called with a key like 'dir/' mapped to non-empty bytes — e.g. a data structure that stores a folder's concatenated contents under the folder's name with a trailing slash.","commonSituations":"Converting from zip or a virtual FS where directories are implicit and someone appended '/' to a file key; hand-built members maps mixing file/dir conventions.","solutions":["Remove the trailing slash for entries that carry data","Move the bytes under a non-directory key like 'dir/index.txt'","Drop the bytes if the entry is genuinely a directory"],"exampleFix":"// before\nmembers.set(\"docs/\", fileBytes);\n// after\nmembers.set(\"docs/readme.txt\", fileBytes);","handlingStrategy":"validation","validationCode":"for (const [key, bytes] of members) {\n\tif ((key.endsWith(\"/\") || key.endsWith(\"\\\\\")) && bytes.byteLength > 0) {\n\t\tthrow new Error(`'${key}' is a directory but has ${bytes.byteLength} bytes`);\n\t}\n}","typeGuard":null,"tryCatchPattern":"try {\n\treturn await createTar(members);\n} catch (err) {\n\tif (err instanceof ArchiveError && err.message.includes(\"cannot contain file data\")) {\n\t\t// move data under a non-directory key\n\t}\n\tthrow err;\n}","preventionTips":["Reserve trailing-slash keys for empty directory entries only","Keep directory and file entries in separate conventions in your data model"],"tags":["archive","tar","path-validation"],"backgroundTag":"invalid-archive-entry-path","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}