{"record":{"id":"8efba04adea16db0","repo":"dagger/dagger","slug":"failed-to-lstat-file-target","errorCode":null,"errorMessage":"failed to lstat file target","messagePattern":"failed to lstat file target","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/fsutil/copy/copy.go","lineNumber":696,"sourceCode":"\t\t}\n\t\treturn true, nil\n\t} else if !st.IsDir() {\n\t\treturn false, errors.Errorf(\"cannot copy to non-directory: %s\", dst)\n\t} else if overwriteTargetMetadata {\n\t\tif err := os.Chmod(dst, stat.Mode()); err != nil {\n\t\t\treturn false, errors.Wrapf(err, \"failed to chmod on %s\", dst)\n\t\t}\n\t}\n\treturn false, nil\n}\n\nfunc ensureEmptyFileTarget(dst string) error {\n\tfi, err := os.Lstat(dst)\n\tif err != nil {\n\t\tif os.IsNotExist(err) {\n\t\t\treturn nil\n\t\t}\n\t\treturn errors.Wrap(err, \"failed to lstat file target\")\n\t}\n\tif fi.IsDir() {\n\t\treturn errors.Errorf(\"cannot replace to directory %s with file\", dst)\n\t}\n\treturn os.Remove(dst)\n}\n\nfunc containsWildcards(name string) bool {\n\tisWindows := runtime.GOOS == \"windows\"\n\tfor i := 0; i < len(name); i++ {\n\t\tch := name[i]\n\t\tif ch == '\\\\' && !isWindows {\n\t\t\ti++\n\t\t} else if ch == '*' || ch == '?' || ch == '[' {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false","sourceCodeStart":678,"sourceCodeEnd":714,"githubUrl":"https://github.com/dagger/dagger/blob/82ba2681dbe30d3547a1dc50ea495900ab5b6047/internal/fsutil/copy/copy.go#L678-L714","documentation":"ensureEmptyFileTarget lstats the destination before copying a file over it; if Lstat fails with an error other than NotExist, this wrapped error is returned. It indicates the destination path could not even be inspected — permissions on the parent directory, a bad path, or I/O errors — not that the file is absent.","triggerScenarios":"copy() of a file to dst where os.Lstat(dst) returns a non-IsNotExist error (EACCES on parent dir, ENOTDIR via a non-dir path component, EIO, ENAMETOOLONG).","commonSituations":"Destination path traverses a directory without execute permission; dst contains a symlink loop or corrupted mount; path length exceeds filesystem limits.","solutions":["Inspect the wrapped os error and fix parent-directory permissions so the path can be stat'ed","Validate the destination path components exist and are traversable directories","Check for symlink loops or broken mounts on the destination path","Shorten the destination path if hitting ENAMETOOLONG"],"exampleFix":"// before\ncopy.Copy(ctx, srcFS, \"/src/file\", \"/root-only-dir/file\") // lstat EACCES\n// after\nif _, err := os.Lstat(\"/root-only-dir\"); err != nil {\n    return fmt.Errorf(\"dest parent inaccessible: %w\", err)\n}\ncopy.Copy(ctx, srcFS, \"/src/file\", \"/root-only-dir/file\")","handlingStrategy":"validation","validationCode":"for dir := filepath.Dir(dst); dir != \"/\"; dir = filepath.Dir(dir) {\n    if _, err := os.Lstat(dir); err != nil {\n        return fmt.Errorf(\"cannot stat path component %s: %w\", dir, err)\n    }\n}","typeGuard":"func pathStatable(p string) error {\n    _, err := os.Lstat(p)\n    if err == nil || os.IsNotExist(err) {\n        return nil\n    }\n    return err\n}","tryCatchPattern":"if err := copy.Copy(ctx, srcFS, src, dst); err != nil {\n    if strings.Contains(err.Error(), \"failed to lstat file target\") {\n        return fmt.Errorf(\"destination %s is not inspectable: check parent permissions/mounts: %w\", dst, err)\n    }\n    return err\n}","preventionTips":["Ensure every component of the destination path is traversable (x bit on dirs)","Avoid symlink loops and long paths in destinations","Stat the destination path early in setup to surface access problems","Fix parent-directory ownership/permissions before file copies"],"tags":["filesystem","lstat","copy","permissions"],"backgroundTag":"path-not-accessible","analyzedSha":"82ba2681dbe30d3547a1dc50ea495900ab5b6047","analyzedAt":"2026-09-05T07:21:37.930Z","contentChangedAt":"2026-09-05T07:21:37.930Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}