-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/go: generate shouldn't require a complete valid package #36422
Comments
I agree that there are many valid use cases for Other cases like |
Is this actually the case?
I think Which I think makes sense because |
Also related, I think: #36068. |
Yes, we certainly need at least one of the source files to contain a valid |
What about if you have conflicting package names? Which one is correct? I ask because no package clause could be considered to be a special case of that could it not? |
@myitcv I guess you could go with heuristics in that case; choose one or more of:
If the above heuristics fail (e.g. you've got two files with package names unrelated to the directory, But I suspect those heuristics would catch almost all cases. |
The only reason I point that out is that |
Would it work to add a new flag for generate to specify the package and override parsing it from files? |
@zephyrtronium, we already have a way to specify the package name: add a Moreover, that seems like the clear tiebreaker, too: when |
Output files with negated build tag will be ignored by go generate with the same tag:
Therefore invalid codes will not break the next go generate. |
I believe skipping invalid files would work. The The patch below adds error checking to the loop responsible for spawning --- a/src/cmd/go/internal/generate/generate.go
+++ b/src/cmd/go/internal/generate/generate.go
@@ -168,7 +168,12 @@ func runGenerate(cmd *base.Command, args []string) {
// Even if the arguments are .go files, this loop suffices.
printed := false
- for _, pkg := range load.Packages(args) {
+
+ for _, pkg := range load.PackagesAndErrors(args) {
+ if pkg.Error != nil {
+ continue
+ }
+
if modload.Enabled() && pkg.Module != nil && !pkg.Module.Main {
if !printed {
fmt.Fprintf(os.Stderr, "go: not generating in packages in dependency modules\n") |
What should happen to the value of $ cat p.go
//go:generate echo "generate output for package $GOPACKAGE"
package +bad For reference, the current behavior is the following error:
Edit: Since |
@dmitshur The Go spec should be updated in relation to directives to avoid undefined behavior like this. This problem arises from |
@dmitshur, I would argue that an invalid If a |
Change https://golang.org/cl/229097 mentions this issue: |
This change allows go generate to process packages where files may disappear during code generation. See also golang#36422. Fixes golang#36068
Change https://golang.org/cl/311531 mentions this issue: |
This change allows go generate to process packages where files may disappear during code generation. See also golang#36422. Updates golang#36068
When developing a "go generate" script, it's easy to create an invalid program as the output. When that happens, one can no longer run "go generate" because the package must typecheck before "go generate" is allowed to run. The result can be confusing, and I see no vital reason why this property must be true; moreover, sometimes it could be impossible to satisfy without hand-editing some files to get the package to the state where it could be run.
Let's lift this requirement.
The text was updated successfully, but these errors were encountered: