{"record":{"id":"d67a7cbe6cbd8ab9","repo":"Tencent/WeKnora","slug":"failed-to-open-source-file-w","errorCode":null,"errorMessage":"failed to open source file: %w","messagePattern":"failed to open source file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/application/service/file/local.go","lineNumber":195,"sourceCode":"\t}\n\n\t// Build destination path with the knowledge-owned layout.\n\tdir := filepath.Join(s.baseDir, fmt.Sprintf(\"%d\", tenantID), knowledgeID)\n\tif _, err := secutils.SafePathUnderBase(s.baseDir, dir); err != nil {\n\t\tlogger.Errorf(ctx, \"Path traversal denied for CopyFile dir: %v\", err)\n\t\treturn \"\", fmt.Errorf(\"invalid path: %w\", err)\n\t}\n\tif err := os.MkdirAll(dir, 0o755); err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to create directory: %w\", err)\n\t}\n\n\text := filepath.Ext(srcPath)\n\tfilename := fmt.Sprintf(\"%d%s\", time.Now().UnixNano(), ext)\n\tdstPath := filepath.Join(dir, filename)\n\n\tsrc, err := os.Open(srcResolved)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to open source file: %w\", err)\n\t}\n\tdefer src.Close()\n\n\tdst, err := os.Create(dstPath)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to create destination file: %w\", err)\n\t}\n\tdefer dst.Close()\n\n\tif _, err := io.Copy(dst, src); err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to copy file content: %w\", err)\n\t}\n\n\trelPath, _ := filepath.Rel(s.baseDir, dstPath)\n\tnewPath := localScheme + filepath.ToSlash(relPath)\n\tlogger.Infof(ctx, \"Copied local file %s to %s\", srcPath, newPath)\n\treturn newPath, nil\n}","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/application/service/file/local.go#L177-L213","documentation":"CopyFile fails to open the source file (os.Open(srcResolved)) and wraps the OS error. This means the source path does not exist, is not readable by the process, or is a directory. The copy never starts in this case; no destination file is created.","triggerScenarios":"Calling CopyFile with srcPath pointing to a deleted/moved file, a file stored with a different naming scheme, a symlink target that vanished, or a file with permissions that deny read access to the service user.","commonSituations":"Uploading/app referencing a file that was garbage-collected or on a temporary volume that was cleaned; wrong path passed by an upstream caller (e.g. DB row stores a stale path); container image changes removed previously present files; NFS/object-store mount not mounted so files 'disappear'.","solutions":["Log/inspect the wrapped error: ENOENT means the file is missing — verify srcPath exists with `stat` before calling CopyFile.","If the source was on a temp/ephemeral volume, persist uploads to the durable baseDir first, or re-upload the original from the client.","Fix file permissions or run the service as a user with read access to the source path.","Guard the caller: only pass paths previously returned by this file service's Save/Upload methods, and check existence beforehand."],"exampleFix":"// before\nsrc, err := os.Open(srcResolved)\nif err != nil {\n    return \"\", fmt.Errorf(\"failed to open source file: %w\", err)\n}\n// after\nif _, err := os.Stat(srcResolved); errors.Is(err, os.ErrNotExist) {\n    return \"\", fmt.Errorf(\"source file does not exist: %s\", srcResolved)\n}\nsrc, err := os.Open(srcResolved)\nif err != nil {\n    return \"\", fmt.Errorf(\"failed to open source file: %w\", err)\n}","handlingStrategy":"validation","validationCode":"func ensureSourceReadable(srcPath string) error {\n    fi, err := os.Stat(srcPath)\n    if err != nil {\n        return fmt.Errorf(\"source missing: %w\", err)\n    }\n    if fi.IsDir() {\n        return fmt.Errorf(\"source is a directory: %s\", srcPath)\n    }\n    f, err := os.Open(srcPath)\n    if err != nil {\n        return fmt.Errorf(\"source not readable: %w\", err)\n    }\n    f.Close()\n    return nil\n}\n// call ensureSourceReadable(srcResolved) before CopyFile","typeGuard":"func fileExistsReadable(path string) bool {\n    fi, err := os.Stat(path)\n    return err == nil && fi.Mode().IsRegular()\n}","tryCatchPattern":"newPath, err := svc.CopyFile(ctx, srcPath, tenantID, knowledgeID)\nvar perr *fs.PathError\nif errors.As(err, &perr) && errors.Is(perr.Err, fs.ErrNotExist) {\n    // stale reference: re-upload or restore source, then retry once\n    if restoreErr := restoreSource(ctx, srcPath); restoreErr == nil {\n        newPath, err = svc.CopyFile(ctx, srcPath, tenantID, knowledgeID)\n    }\n}\nif err != nil {\n    return fmt.Errorf(\"copy failed: %w\", err)\n}","preventionTips":["Only pass paths previously returned by this file service; never hand-built or user-typed paths.","Resolve and pin the absolute source path; detect broken symlinks with os.Stat before opening.","Clean up temp/upload volumes only after confirming no pending references.","Keep source file permissions readable by the service user."],"tags":["filesystem","file-not-found","io","permissions"],"backgroundTag":"file-not-found","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}