fix(goctl): resolve pb imports across sibling modules (#5511)#5552
Open
Meng-Xin wants to merge 1 commit intozeromicro:masterfrom
Open
fix(goctl): resolve pb imports across sibling modules (#5511)#5552Meng-Xin wants to merge 1 commit intozeromicro:masterfrom
Meng-Xin wants to merge 1 commit intozeromicro:masterfrom
Conversation
Author
This PR fixes the import-path resolution in `goctl` itself when pb output is generated into a sibling Go module. It is intended to support the multi-module `go.work` layout directly, rather than only relying on changing `go_package` as a workaround. |
Contributor
ReviewThis PR fixes a real pain point in multi-module Go workspaces on Windows (#5511). Problem: When running import "sales-admin/F:/coding/learn/zero-rpc/sales-center/gen/admin" // ❌instead of the correct: import "sales-center/gen/admin" // ✅The fix: When the pb output directory belongs to a different Go module (detected by finding a Code review points to verify:
Overall: The approach is correct. Please ensure the cross-platform path handling is covered by tests before merging. |
…utside current module The previous implementation used strings.TrimPrefix to compute Go import paths, which assumed all target directories reside under the current project directory. This caused incorrect import paths when the protobuf output directory was in a sibling module or an external location. Introduce resolveDirPackage() which determines the correct import path by: 1. Checking if the directory is within the current module 2. Traversing parent directories to find go.mod for sibling modules 3. Falling back to the proto go_package option or directory basename Also unify all Dir.Package assignments (including wd, call, and getChildPackage) to use the new resolver, and harden isImportPath() to reject Unix-style absolute paths on Windows.
aa0b4a8 to
f877110
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix
goctl rpc protocimport generation when--go_out/--go-grpc_outpoint to a sibling Go module instead of the current module.Previously,
goctlderived the generated pb import path by joining the current project module path with the pb output directory on disk. On Windows, when the pb output directory lived outside the current module, this could produce invalid imports such as:"sales-admin/F:/coding/learn/zero-rpc/sales-center/pb/admin"