{"record":{"id":"09ab552f86d74575","repo":"slimtoolkit/slim","slug":"failed-to-calculate-relative-path-w","errorCode":null,"errorMessage":"failed to calculate relative path: %w","messagePattern":"failed to calculate relative path: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/imagebuilder/internalbuilder/engine.go","lineNumber":287,"sourceCode":"\t\t!fsutil.IsDir(input.Source) {\n\t\treturn nil, fmt.Errorf(\"bad input data\")\n\t}\n\n\tvar b bytes.Buffer\n\ttw := tar.NewWriter(&b)\n\n\tlayerBasePath := \"/\"\n\tif input.Params != nil && input.Params.TargetPath != \"\" {\n\t\tlayerBasePath = input.Params.TargetPath\n\t}\n\n\terr := filepath.Walk(input.Source, func(fp string, info os.FileInfo, err error) error {\n\t\tif err != nil {\n\t\t\treturn nil\n\t\t}\n\t\trel, err := filepath.Rel(input.Source, fp)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"failed to calculate relative path: %w\", err)\n\t\t}\n\n\t\thdr := &tar.Header{\n\t\t\tName: path.Join(layerBasePath, filepath.ToSlash(rel)),\n\t\t\tMode: int64(info.Mode()),\n\t\t}\n\n\t\tif !info.IsDir() {\n\t\t\thdr.Size = info.Size()\n\t\t}\n\n\t\tif info.Mode().IsDir() {\n\t\t\thdr.Typeflag = tar.TypeDir\n\t\t} else if info.Mode().IsRegular() {\n\t\t\thdr.Typeflag = tar.TypeReg\n\t\t} else {\n\t\t\treturn fmt.Errorf(\"not implemented archiving file type %s (%s)\", info.Mode(), rel)\n\t\t}","sourceCodeStart":269,"sourceCodeEnd":305,"githubUrl":"https://github.com/slimtoolkit/slim/blob/81940d17fa112cc678e32209214bcb2355cb3004/pkg/imagebuilder/internalbuilder/engine.go#L269-L305","documentation":"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\".","triggerScenarios":"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.","commonSituations":"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.","solutions":["Re-run the build — if caused by concurrent mutation, ensure the layer directory is stable during build.","Pass a cleaned, absolute layer root (filepath.Clean / filepath.Abs) as Source.","Check for other processes (cleaners, IDEs, sync tools) modifying the layer directory during the build."],"exampleFix":"// before\nsrc := \"/build/rootfs/../rootfs\"\n// after\nsrc, _ := filepath.Abs(filepath.Clean(\"/build/rootfs/../rootfs\"))\nlayerFromDir(LayerDataInfo{Source: src})","handlingStrategy":"validation","validationCode":"src, err := filepath.Abs(filepath.Clean(rawSrc))\nif err != nil {\n    return fmt.Errorf(\"invalid layer source path: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"failed to calculate relative path\") {\n    // re-run with a cleaned absolute source path and no concurrent writers\n}","preventionTips":["Pass filepath.Clean/Abs-processed paths as layer roots","Freeze the layer directory (no concurrent writes) during build","Avoid .. and duplicate separators in configured paths"],"tags":["filesystem","tar","image-build"],"backgroundTag":"relative-path-computation-failed","analyzedSha":"81940d17fa112cc678e32209214bcb2355cb3004","analyzedAt":"2026-08-31T23:06:12.682Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}