Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(cmd/gf): fix gf gen enums output path error when using relative path#4636

Merged
gqcn merged 1 commit intogogf:masterfrom
lingcoder:bugfix/gen-enums-path-error
Jan 21, 2026
Merged

fix(cmd/gf): fix gf gen enums output path error when using relative path#4636
gqcn merged 1 commit intogogf:masterfrom
lingcoder:bugfix/gen-enums-path-error

Conversation

@lingcoder
Copy link
Contributor

Summary

  • Fix gf gen enums output file created at wrong location when using relative path
  • Output was incorrectly relative to source directory instead of current working directory
  • Add defer gfile.Chdir(originPwd) to restore original working directory

Root Cause

The code calls gfile.Chdir(realPath) to change to source directory before gfile.PutContents(in.Path, ...), causing relative output path to be resolved relative to source directory.

Solution

  • Convert output path to absolute using gfile.Abs() before Chdir
  • Restore original working directory with defer (following genpb.go pattern)

Test Cases

  • Test_Gen_Enums_Issue4387_RelativePath - standard project with relative path
  • Test_Gen_Enums_AbsolutePath - absolute path (should work as before)
  • Test_Gen_Enums_Issue4387_Monorepo - monorepo mode (cd app/xxx && gf gen enums)

Closes #4387

…ath (gogf#4387)

The output file was incorrectly created relative to source directory instead of
current working directory due to Chdir before writing file. Now converts output
path to absolute before Chdir and restores original working directory after.
@gqcn
Copy link
Member

gqcn commented Jan 21, 2026

lgtm

@gqcn gqcn merged commit cee6f49 into gogf:master Jan 21, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gf/gen enums: 枚举维护命令的path参数,因为进程工作目录的变化导致输出路径不符合预期

2 participants