Description
What version of Go are you using (go version
)?
$ go version go version go 1.17.1 linux/amd64
Does this issue reproduce with the latest release?
Yes? Will try later
What operating system and processor architecture are you using (go env
)?
go env
Output
$ go env
What did you do?
Context
I'm using git. I'm working on a long running new-version branch. I receive a request to patch something on main
branch. So I quickly create a new branch from main
. Think of it as something like this:
│
│\
│ \
│ \
│ │
│ │
main│ │
│ │
│ │newver
│ │
│ │
│ │
│ │
│ │
│ │
▼ ▼
\
\
\
│
│patch
│
│
▼
Now in the newver
branch there are a number of new subdirectories/subpackages etc. Because it's quite experimental, not all of the subdirectories will be staged and committed into the branch. Thus when I switch to the new branch, there are some subdirectories that are not removed (let's use for example a subpackage called machine
). Some are removed because they're already staged and committed to newver
(and let's use for example, a package called statistics
) .
What I did
So, now I'm in the patch
branch, with some new subdirectories. I add a 2 line patch. Then I run go mod tidy
.
go mod tidy
recursively goes into each subdirectory and does its module thing. Fantastic. The issue is the code in machine
refers to the subpackage statistics
. Ah, now we run into a problem - it's going to be 404 not found (unrecognized import path).
What I did to overcome the issue
There were two options I had:
- stash the changes in
patch
branch, go back tonewver
, make a new branch offnewver
, call itnewverexp
and add all the experimental stuff that is not ready fornewver
, then go back to the patch branch, unstash the changes, rungo mod tidy
and push changes. Outcome: I now have more branches to manage! - backup those subdirectories and delete them, run
go mod tidy
and then restore them from backup.
Either way, both options took more time than expected. Now, you might say "this is just your poor git hygiene". And I'd be inclined to agree with you.
What would be nicer
It would make things a lot more expedient if I could just tell go mod tidy
to not recursively go into certain directories that has .go
files in them because:
- they may actually be data files with the same extension
- they may be temporary
Activity
mvdan commentedon Oct 8, 2021
Going the route of #30058, have you thought of dropping a temporary
go.mod
file in that subdirectory? Then any commands likego mod tidy
orgo build ./...
would entirely skip it. It doesn't matter what thego.mod
file contains, it could be entirely empty - the point is that with such a file being present, then that directory sub-tree is entirely ignored for the parent module, as it's assumed to be a nested module.bcmills commentedon Oct 8, 2021
I think there are already enough options for this sort of use case.
git stash
in this sort of situation.go.mod
file to prune out the new code, as Daniel suggests, would also work.machine
directory to_machine
or similar. (Thego
command ignores directories that begin with_
or.
or are namedtestdata
.)go mod tidy -e
to suppress the error for the missingstatistics
package. (That may still add dependencies for other packages imported bymachine
, though.)git codereview
tool even recognizes sentences likeDO NOT MAIL
andDO NOT MERGE
in commit messages for exactly that sort of situation.)Honestly, I think that list of options is already too long — it's hard to know which to use when! I don't want to add to it further. 😅
[-]go mod tidy: add ability to ignore certain subdirectories[/-][+]cmd/go: add ability to allow mod tidy to ignore certain subdirectories[/+]mpx commentedon Oct 11, 2021
It might be useful to document these options somewhere easily found when using
mod tidy
. I suspect many people will find options beyondgit stash
non-obvious (eg, skipping_
is not that well known, same with usinggo.mod
to suppress dependencies).FWIW, I regularly do the
git stash; go mod tidy; git stash pop
dance, along with moving/renaming files. I'd be interested to improve this workflow, but I'm not sure what that would look like. For Git, it would be convenient ifgo mod tidy
had an option to only consider files in the index -- but this level of integration probably isn't appropriate.gopherbot commentedon Nov 8, 2021
Timed out in state WaitingForInfo. Closing.
(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)