diff --git a/tools/goctl/api/gogen/util.go b/tools/goctl/api/gogen/util.go index 106688b1..aa9ba8fc 100644 --- a/tools/goctl/api/gogen/util.go +++ b/tools/goctl/api/gogen/util.go @@ -70,7 +70,20 @@ func getParentPackage(dir string) (string, error) { return "", err } - return filepath.ToSlash(filepath.Join(projectCtx.Path, strings.TrimPrefix(projectCtx.WorkDir, projectCtx.Dir))), nil + // fix https://github.com/zeromicro/go-zero/issues/1058 + wd := projectCtx.WorkDir + d := projectCtx.Dir + same, err := ctlutil.SameFile(wd, d) + if err != nil { + return "", err + } + + trim := strings.TrimPrefix(projectCtx.WorkDir, projectCtx.Dir) + if same { + trim = strings.TrimPrefix(strings.ToLower(projectCtx.WorkDir), strings.ToLower(projectCtx.Dir)) + } + + return filepath.ToSlash(filepath.Join(projectCtx.Path, trim)), nil } func writeProperty(writer io.Writer, name, tag, comment string, tp spec.Type, indent int) error { diff --git a/tools/goctl/util/file.go b/tools/goctl/util/file.go index b8ef6bf2..149d2a83 100644 --- a/tools/goctl/util/file.go +++ b/tools/goctl/util/file.go @@ -148,6 +148,24 @@ func LoadTemplate(category, file, builtin string) (string, error) { return string(content), nil } +// SameFile compares the between path if the same path, +// it maybe the same path in case case-ignore, such as: +// /Users/go_zero and /Users/Go_zero, as far as we know, +// this case maybe appear on macOS and Windows. +func SameFile(path1, path2 string) (bool, error) { + stat1, err := os.Stat(path1) + if err != nil { + return false, err + } + + stat2, err := os.Stat(path2) + if err != nil { + return false, err + } + + return os.SameFile(stat1, stat2), nil +} + func createTemplate(file, content string, force bool) error { if FileExists(file) && !force { return nil