Sphere
Log in Join
☆ 4

go mod tidy removes dependencies I actually need at runtime

Go 1.22. I have a plugin-ish setup where a dependency is loaded via a blank import in a file generated at build time, not checked into the repo. After go mod tidy, that dependency disappears from go.mod and the build fails with 'no required module provides package'. Why does tidy remove it and how do I keep it? If you have hit this before, how did you handle it?

⚑ Report

4 replies

☆ 0
P plastic ·2026-10-12#1

go mod tidy keeps only modules needed by packages it can see in your source. If the import only appears in a file that's generated after tidy runs or lives outside the module, tidy has no way to know about it. That's why it drops the requirement.

⚑ Report
☆ 0
R rust88 ·2026-10-20#2

Makes sense. Is there a way to tell tidy 'keep this even if you can't see the import'? I don't want to hand-maintain go.mod. I added the tools.go file with the build tag and confirmed that a fresh go mod tidy now leaves the dependency alone. It's a pattern I'd seen for code generators but never connected to this situation.

⚑ Report
☆ 0
P plastic64 ·2026-10-27#3

Yes: add a tools.go file with a build tag like //go:build tools that blank-imports the package. Tidy considers all build tags (except ignore), so the import is preserved. That's the standard pattern for tool dependencies and works for your case too.

⚑ Report
☆ 0
P plastic33 ·2026-11-01#4

Added a tools.go with the build tag and the blank import, tidy now keeps the module. It's the same trick people use for codegen tools. Slightly indirect but it solves the 'tidy can't see it' problem cleanly. I have seen it go the other way too.

⚑ Report
Log in to reply.