I have a logging module that works different for prod and dev. I use build tags for this. Everything compiles and works fine, but VSCode doesn’t understand what I’m doing and gives errors.
Here is the minimum working code for prod (logging/logger_prod.go
):
//go:build !dev
// +build !dev
package logging
type ProdLogger struct {
}
func NewLogger() Logger {
return &ProdLogger{}
}
func (l *ProdLogger) Printf(pattern string, args ...interface{}) {
}
And for dev (logging/logger_dev.go
):
//go:build dev
// +build dev
package logging
import "fmt"
type DevLogger struct {
}
func NewLogger() Logger {
return &DevLogger{}
}
func (l *DevLogger) Printf(pattern string, args ...interface{}) {
fmt.Printf(pattern, args...)
}
Main file (main.go):
package main
import (
"github.com/eightlay/azula/internal/modules/logging"
)
func main() {
logger := logging.NewLogger()
logger.Printf("Hello, World!")
}
My launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Dev",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "cmd/gateway/main.go",
"envFile": "${workspaceFolder}/.env",
"env": {
"GOFLAGS": "-tags=dev"
}
},
{
"name": "Launch Prod",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "cmd/gateway/main.go",
"envFile": "${workspaceFolder}/.env",
"env": {
"GOFLAGS": "-tags=prod"
}
}
]
}
The error is: “No packages found for open file ***/logger_prod.go.
This file may be excluded due to its build tags; try adding “-tags=” to your gopls “buildFlags” configuration” in logger_prod.go
I’ve tried to specify buildTags in settings.json
– makes no difference.
I’ve tried to use //go:build prod
and // +build prod
in the prod file. The errors are: “NewLogger redeclared in this block” for both prod and dev files, “undefined: logging.NewLogger” for main.go
.
How should I configure VSCode to solve this issue?