Put dev dependencies in tools.go

Jacob Hummer - Apr 24 - - Dev Community

Problem: You have a ./gen.go script that uses github.com/arkady-emelyanov/go-shellparse to do some stuff. When you run go mod tidy it removes github.com/arkady-emelyanov/go-shellparse from your go.mod because ./gen.go has a //go:build ignore in it... which means go mod tidy doesn't include it in its checks. πŸ˜”

gen.go

//go:build ignore

package main

import (
    "github.com/arkady-emelyanov/go-shellparse"
    // etc.
)

func main() {
    shellparse.ParseVarsFile("file.txt")
    exec.Command("go", "run", "github.com/melbahja/got/cmd/got").Run()
}
Enter fullscreen mode Exit fullscreen mode

Solution: Use tools.go with //go:build tools which go mod tidy does scan and add any dependencies to list all the dependencies that ./gen.go needs.

tools.go

//go:build tools

package tools

import (
    _ "github.com/melbahja/got/cmd/got"
    _ "github.com/arkady-emelyanov/go-shellparse"
    // etc.
)
Enter fullscreen mode Exit fullscreen mode
go run ./gen.go
Enter fullscreen mode Exit fullscreen mode

Further reading πŸ“š

. . . . . . . . . .