{"record":{"id":"304f6f862c4add92","repo":"flipped-aurora/gin-vue-admin","slug":"error-304f6f","errorCode":null,"errorMessage":"存在同名文件","messagePattern":"存在同名文件","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/utils/directory.go","lineNumber":25,"sourceCode":"\t\"reflect\"\n\t\"strings\"\n\n\t\"github.com/flipped-aurora/gin-vue-admin/server/utils/logger\"\n)\n\n//@author: [piexlmax](https://github.com/piexlmax)\n//@function: PathExists\n//@description: 文件目录是否存在\n//@param: path string\n//@return: bool, error\n\nfunc PathExists(path string) (bool, error) {\n\tfi, err := os.Stat(path)\n\tif err == nil {\n\t\tif fi.IsDir() {\n\t\t\treturn true, nil\n\t\t}\n\t\treturn false, errors.New(\"存在同名文件\")\n\t}\n\tif os.IsNotExist(err) {\n\t\treturn false, nil\n\t}\n\treturn false, err\n}\n\n//@author: [piexlmax](https://github.com/piexlmax)\n//@function: CreateDir\n//@description: 批量创建文件夹\n//@param: dirs ...string\n//@return: err error\n\nfunc CreateDir(dirs ...string) (err error) {\n\tfor _, v := range dirs {\n\t\texist, err := PathExists(v)\n\t\tif err != nil {\n\t\t\treturn err","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/utils/directory.go#L7-L43","documentation":"PathExists distinguishes three states via os.Stat: path exists as a directory (true, nil), path exists but is a regular FILE (false, '存在同名文件'), or path does not exist (false, nil). The error exists so CreateDir can abort instead of failing later when os.MkdirAll hits an existing file at the target path.","triggerScenarios":"Calling CreateDir(dirPath) when a FILE already occupies exactly that path — os.Stat succeeds, fi.IsDir() is false, so the 'same-name file exists' error is returned instead of creating a directory.","commonSituations":"A previous build/tool wrote a file where a directory is now required (e.g. 'uploads' created as a file); packaging artifacts extracted flat; case-insensitive filesystems colliding with a file named like the target dir.","solutions":["Delete or rename the file that occupies the target path, then retry CreateDir.","Check with `ls -la <path>` or `file <path>` to confirm a regular file exists there and remove it (`rm <path>`).","In code, handle the error distinctly from os errors and prompt the user to move the conflicting file rather than blindly MkdirAll."],"exampleFix":"// before\nexists, err := utils.PathExists(dirPath) // err: 存在同名文件\n// after (operator fix)\n// rm ./uploads        # remove the file blocking the directory\n// then re-run: utils.CreateDir(dirPath) succeeds","handlingStrategy":"validation","validationCode":"if fi, err := os.Stat(target); err == nil && !fi.IsDir() {\n    return fmt.Errorf(\"%s exists and is a file; move it first\", target)\n}","typeGuard":null,"tryCatchPattern":"ok, err := utils.PathExists(dir)\nif err != nil {\n    if err.Error() == \"存在同名文件\" {\n        return fmt.Errorf(\"a file named %s blocks directory creation\", dir)\n    }\n    return err\n}","preventionTips":["Before creating directories, check the target with os.Stat and fail fast when it is a regular file","Keep build outputs and runtime dirs (uploads/, logs/) out of flat file layouts that could occupy those names","Surface PathExists errors distinctly to operators instead of collapsing them with 'already exists'"],"tags":["filesystem","go","directory"],"backgroundTag":"path-conflicts-with-existing-file","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}