flipped-aurora/gin-vue-admin · error
存在同名文件
Error message
存在同名文件
What it means
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.
Source
Thrown at server/utils/directory.go:25
"reflect"
"strings"
"github.com/flipped-aurora/gin-vue-admin/server/utils/logger"
)
//@author: [piexlmax](https://github.com/piexlmax)
//@function: PathExists
//@description: 文件目录是否存在
//@param: path string
//@return: bool, error
func PathExists(path string) (bool, error) {
fi, err := os.Stat(path)
if err == nil {
if fi.IsDir() {
return true, nil
}
return false, errors.New("存在同名文件")
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateDir
//@description: 批量创建文件夹
//@param: dirs ...string
//@return: err error
func CreateDir(dirs ...string) (err error) {
for _, v := range dirs {
exist, err := PathExists(v)
if err != nil {
return errView on GitHub (pinned to 3136500ef3)
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.
Example fix
// before exists, err := utils.PathExists(dirPath) // err: 存在同名文件 // after (operator fix) // rm ./uploads # remove the file blocking the directory // then re-run: utils.CreateDir(dirPath) succeeds
Defensive patterns
Strategy: validation
Validate before calling
if fi, err := os.Stat(target); err == nil && !fi.IsDir() {
return fmt.Errorf("%s exists and is a file; move it first", target)
} Try / catch
ok, err := utils.PathExists(dir)
if err != nil {
if err.Error() == "存在同名文件" {
return fmt.Errorf("a file named %s blocks directory creation", dir)
}
return err
} Prevention
- 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'
When it happens
Trigger: 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.
Common situations: 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.
Related errors
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/304f6f862c4add92.
Report an issue: GitHub.