-
Notifications
You must be signed in to change notification settings - Fork 18k
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: confusing situation with 'go run' and '//go:build ignore' #73152
Comments
Related #54993 |
you could have run |
certainly having that in the error message would have helped |
taking a step back though, would it have been better for your script to be tracked as a tool package (and have its dependencies tracked)? |
i don't think so, no.
it's really, really useful to be able to have little scripts. |
Hi @josharian, improving the error here would likely be helpful. One question though regarding your use of
For your particular case, would it have helped if It sounds like you are well aware of the underlying mechanisms, but at least for my reference, from the go mod tidy doc:
and from the go run doc:
|
Thanks. The use of I'm genuinely unsure about what the right fix is here. |
For gopls, it looks like you could add a
|
Good to know, thanks! Definitely feels a bit like the defaults are fighting with each other here. :P |
Perhaps some new build tag convention could be established for standalone main files, and then the default gopls setting could use that convention in addition to That said, I guess I'm curious what was out of sync in your particular example. In at least some common cases, it does seem to give a better error. For example, if I have a
From a quick look at the cmd/go code, the error you saw can happen when it thinks the
That's probably not exactly what you hit, though, and while I could probably trigger that specific error in other ways, I'm curious what was out of sync for you in particular (if that was apparent based on your debugging). |
It was missing some imports. Not sure why it differed from your experiments. |
I've had this same problem previously, and my solution then was to cheat and stick the import in question into my A dedicated way to say "this is a standalone main file but please include its deps" would be great. (I don't particularly care about mixing deps; that's not unreasonably handled with |
Any other build tag than |
@seankhliao I keep getting the sense from you that you feel that this is user error. I respectfully disagree. It is true that I didn't know some of the mechanics that @thepudds pointed out, but I very much doubt many other folks do either; I am probably as least as informed about the workings of cmd/go as the median gopher. After the conversation above, it seems to me that there are at least two real issues here:
|
I agree it's a poor interplay of defaults, but my stance would be more that:
|
It looks like support for gopls understanding standalone main files was added to gopls via #49657 (where the opening comment there calls them "scripts"). It seems there was a fair amount of enthusiasm expressed there. I don't see any explicit discussion there of the interaction between Using Lines 89 to 93 in d49475e
But today, It is probably too disruptive to change One approach could be to document a new convention for what build tag to use in this scenario, with some name selected that is not A different approach might be to do something like add a ~sentence in the cmd/go or modules documentation somewhere that effectively suggests that any build tag other than Or if all that is flawed, there probably is some other way to establish a better best practice here. |
This seems like a good idea, and adding another default tag is cheap, at least code-wise. Would that need to go through a proposal process, or can someone affiliated with gopls just decide? cc @alandonovan |
I'm not opposed to the idea, but I do think it warrants a proposal, as it is unquestionably a change to an existing interface, and to existing habits. |
CC @matloob, @samthanawalla (via owners). |
Go version
go version go1.24.2 darwin/arm64
Output of
go env
in your module/workspace:What did you do?
Here's my terminal transcript:
After quite a while spend debugging, it turns out the problem is that
x.go
had//go:build ignore
, and sogo mod tidy
ignored it...leaving it in a broken state when actually running it.Even once I had a diagnosis, it was non-trivial to work around, because
go mod tidy
has no place to specify build tags. I had to move the file to a new location, remove the build tag, rungo mod tidy
, and then put it all back together again.What did you see happen?
covered above.
What did you expect to see?
again, covered above.
The text was updated successfully, but these errors were encountered: