slimtoolkit/slim · error
failed to read file into the tar: %w
Error message
failed to read file into the tar: %w
What it means
After writing the tar header for a regular file, io.Copy(tw, f) failed while streaming the file contents into the archive. This wraps read/write errors — the file could not be read from disk or the tar writer rejected the data mid-copy. The raw os.Open error is returned unwrapped separately, so this specifically covers copy-stage failures.
Source
Thrown at pkg/imagebuilder/internalbuilder/engine.go:316
if info.Mode().IsDir() {
hdr.Typeflag = tar.TypeDir
} else if info.Mode().IsRegular() {
hdr.Typeflag = tar.TypeReg
} else {
return fmt.Errorf("not implemented archiving file type %s (%s)", info.Mode(), rel)
}
if err := tw.WriteHeader(hdr); err != nil {
return fmt.Errorf("failed to write tar header: %w", err)
}
if !info.IsDir() {
f, err := os.Open(fp)
if err != nil {
return err
}
if _, err := io.Copy(tw, f); err != nil {
return fmt.Errorf("failed to read file into the tar: %w", err)
}
f.Close()
}
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to scan files: %w", err)
}
if err := tw.Close(); err != nil {
return nil, fmt.Errorf("failed to finish tar: %w", err)
}
return tarball.LayerFromReader(&b)
}
View on GitHub (pinned to 81940d17fa)
Solutions
- Re-run the build; ensure no concurrent process mutates the layer directory.
- Check disk health and free space (dmesg / filesystem errors).
- If the layer is on a network mount, copy it to local disk before building.
Example fix
// before
// build reads /mnt/nfs/rootfs directly
layerFromDir(LayerDataInfo{Source: "/mnt/nfs/rootfs"})
// after
os.CopyFS("/tmp/rootfs-snapshot", os.DirFS("/mnt/nfs/rootfs"))
layerFromDir(LayerDataInfo{Source: "/tmp/rootfs-snapshot"}) Defensive patterns
Strategy: retry
Validate before calling
f, err := os.Open(fp)
if err != nil { return err }
st, err := f.Stat()
if err != nil || !st.Mode().IsRegular() { f.Close(); return fmt.Errorf("file changed during archive: %s", fp) } Try / catch
if err != nil && strings.Contains(err.Error(), "failed to read file into the tar") {
// retry the build after ensuring no concurrent writes to the layer dir
} Prevention
- Build from a stable snapshot of the layer directory
- Keep layers on reliable local storage, not network mounts
- Check disk health/free space if failures persist
When it happens
Trigger: A file that opened successfully becomes unreadable during copy — deleted/truncated concurrently, disk I/O error, or permission change between open and copy.
Common situations: Another process deleting or truncating files in the layer directory during the build, failing disk/IO errors, or files on flaky network mounts.
Understand the failure class
Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.
Related errors
- failed to calculate relative path: %w
- not implemented archiving file type %s (%s)
- failed to scan files: %w
- cannot write %s file: %w
- bad output tar - %s
AI-assisted analysis of slimtoolkit/slim@81940d17fa (2026-08-31).
Data as JSON: /api/errors/c07a7b3ec1c9da6d.
Report an issue: GitHub.