{"record":{"id":"0006055b6a0ed0a5","repo":"golang/go","slug":"tar-cannot-add-non-regular-file","errorCode":null,"errorMessage":"tar: cannot add non-regular file","messagePattern":"tar: cannot add non-regular file","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/archive/tar/writer.go","lineNumber":426,"sourceCode":"\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif name == \".\" {\n\t\t\treturn nil\n\t\t}\n\t\tinfo, err := d.Info()\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tlinkTarget := \"\"\n\t\tif typ := d.Type(); typ == fs.ModeSymlink {\n\t\t\tvar err error\n\t\t\tlinkTarget, err = fs.ReadLink(fsys, name)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t} else if !typ.IsRegular() && typ != fs.ModeDir {\n\t\t\treturn errors.New(\"tar: cannot add non-regular file\")\n\t\t}\n\t\th, err := FileInfoHeader(info, linkTarget)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\th.Name = name\n\t\tif d.IsDir() {\n\t\t\th.Name += \"/\"\n\t\t}\n\t\tif err := tw.WriteHeader(h); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif !d.Type().IsRegular() {\n\t\t\treturn nil\n\t\t}\n\t\tf, err := fsys.Open(name)\n\t\tif err != nil {\n\t\t\treturn err","sourceCodeStart":408,"sourceCodeEnd":444,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/archive/tar/writer.go#L408-L444","documentation":"Returned by Writer.AddFS when walking an fs.FS and encountering a directory entry whose type is neither regular, directory, nor symlink. AddFS intentionally supports only those three kinds (the portable subset); device files, sockets, named pipes, and other special files are refused because they cannot be represented portably in a tar that will be extracted on arbitrary platforms.","triggerScenarios":"Calling tw.AddFS(fsys) where fsys contains a Unix socket, named pipe (FIFO), character/block device, or any other non-regular non-directory non-symlink entry.","commonSituations":"Archiving a project directory that contains a .git/objects/pack/*.idx socket-like artifact, a docker socket bind-mounted into the tree, a node_modules with FIFOs, or /tmp-like filesystems with sockets; archiving a real OS filesystem (os.DirFS) rather than a clean file tree.","solutions":["Pre-filter the fs.FS so AddFS only sees regular files, directories, and symlinks — wrap with an fs.FS that skips other types.","Use fs.WalkDir yourself and skip entries where !d.Type().IsRegular() && d.Type() != fs.ModeDir && d.Type() != fs.ModeSymlink.","Stage the content into a clean directory (cp -r without special files) before archiving.","If you must archive special files, write those headers manually with FileInfoHeader (TypeChar/TypeBlock/TypeFifo) — sockets are still unsupported."],"exampleFix":"// before\ntw := tar.NewWriter(w)\nerr := tw.AddFS(os.DirFS(\"/var/run\")) // contains sockets -> throws\n\n// after\ntw := tar.NewWriter(w)\nerr := fs.WalkDir(os.DirFS(\"src\"), \".\", func(name string, d fs.DirEntry, err error) error {\n  if err != nil { return err }\n  t := d.Type()\n  if !t.IsRegular() && t != fs.ModeDir && t != fs.ModeSymlink {\n    return nil // skip sockets, pipes, devices\n  }\n  // ... FileInfoHeader + WriteHeader + Copy as in AddFS\n  return nil\n})","handlingStrategy":"validation","validationCode":"err := fs.WalkDir(fsys, \".\", func(name string, d fs.DirEntry, err error) error {\n  if err != nil { return err }\n  t := d.Type()\n  if !t.IsRegular() && t != fs.ModeDir && t != fs.ModeSymlink {\n    return nil // skip sockets, pipes, devices\n  }\n  return nil\n})","typeGuard":"func isArchivable(t fs.FileMode) bool {\n  return t.IsRegular() || t == fs.ModeDir || t == fs.ModeSymlink\n}","tryCatchPattern":"if err := tw.AddFS(fsys); err != nil && strings.Contains(err.Error(), \"cannot add non-regular file\") {\n  log.Printf(\"skipping non-regular files in %v\", fsys)\n  err = nil\n}","preventionTips":["Pre-filter the fs.FS so AddFS only sees regular files, dirs, and symlinks.","Avoid passing os.DirFS of system directories (/var/run, /tmp) that contain sockets.","Stage content into a clean directory before archiving."],"tags":["archive-tar","addfs","file-type","fs","writer","unsupported"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}