slimtoolkit/slim · error

failed to calculate relative path: %w

Error message

failed to calculate relative path: %w

What it means

While walking the layer directory to build a tar archive, filepath.Rel(input.Source, fp) failed to compute the path of a file relative to the layer root. This should be practically impossible (fp is produced by Walk of Source), so it usually indicates a non-standard or concurrently mutated filesystem state. The error is wrapped as "failed to calculate relative path".

Source

Thrown at pkg/imagebuilder/internalbuilder/engine.go:287

		!fsutil.IsDir(input.Source) {
		return nil, fmt.Errorf("bad input data")
	}

	var b bytes.Buffer
	tw := tar.NewWriter(&b)

	layerBasePath := "/"
	if input.Params != nil && input.Params.TargetPath != "" {
		layerBasePath = input.Params.TargetPath
	}

	err := filepath.Walk(input.Source, func(fp string, info os.FileInfo, err error) error {
		if err != nil {
			return nil
		}
		rel, err := filepath.Rel(input.Source, fp)
		if err != nil {
			return fmt.Errorf("failed to calculate relative path: %w", err)
		}

		hdr := &tar.Header{
			Name: path.Join(layerBasePath, filepath.ToSlash(rel)),
			Mode: int64(info.Mode()),
		}

		if !info.IsDir() {
			hdr.Size = info.Size()
		}

		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)
		}

View on GitHub (pinned to 81940d17fa)

Solutions

  1. Re-run the build — if caused by concurrent mutation, ensure the layer directory is stable during build.
  2. Pass a cleaned, absolute layer root (filepath.Clean / filepath.Abs) as Source.
  3. Check for other processes (cleaners, IDEs, sync tools) modifying the layer directory during the build.

Example fix

// before
src := "/build/rootfs/../rootfs"
// after
src, _ := filepath.Abs(filepath.Clean("/build/rootfs/../rootfs"))
layerFromDir(LayerDataInfo{Source: src})
Defensive patterns

Strategy: validation

Validate before calling

src, err := filepath.Abs(filepath.Clean(rawSrc))
if err != nil {
    return fmt.Errorf("invalid layer source path: %w", err)
}

Try / catch

if err != nil && strings.Contains(err.Error(), "failed to calculate relative path") {
    // re-run with a cleaned absolute source path and no concurrent writers
}

Prevention

When it happens

Trigger: filepath.Walk callback receives an fp whose relationship to input.Source cannot be relativized — e.g. the source path form is inconsistent (non-clean paths, symlinks resolving outside, Windows drive mismatch) or the directory is mutated concurrently during the walk.

Common situations: Layer directory being modified/deleted by another process while the image build is walking it; passing a path with unusual formatting (double slashes, trailing separators) as the layer root.

Related errors


AI-assisted analysis of slimtoolkit/slim@81940d17fa (2026-08-31). Data as JSON: /api/errors/09ab552f86d74575. Report an issue: GitHub.