package pocketbase
import (
"fmt"
"log/slog"
"runtime/debug"
"ganigeorgiev.com/pocketbase/pocketbase/core"
"github.com/fatih/color"
)
const (
expectedDriverVersion = "v1.62.0"
expectedLibcVersion = "v1.80.1"
ModerncDepsCheckHookId = "pbModerncDepsCheck"
)
func checkModerncDeps(app core.App) {
info, ok := debug.ReadBuildInfo()
if !ok {
return
}
var driverVersion, libcVersion string
for _, dep := range info.Deps {
switch dep.Path {
case "modernc.org/libc":
libcVersion = dep.Version
case "modernc.org/sqlite":
driverVersion = dep.Version
}
if driverVersion != "" && libcVersion != "" {
break
}
}
if driverVersion == "" {
return
}
var msg string
if driverVersion != expectedDriverVersion {
msg = fmt.Sprintf(
"You are using modernc.org/sqlite %s which differs from the expected and tested %s.\n"+
"Make sure to either manually update in your go.mod the dependency version to the expected one OR if you want to keep yours "+
"ensure that its indirect modernc.org/libc dependency has the same version as in the https://gitlab.com/cznic/sqlite/-/blob/master/go.mod, "+
"otherwise it could result in unexpected build or runtime errors.",
driverVersion,
expectedDriverVersion,
)
app.Logger().Warn(msg, slog.String("current", driverVersion), slog.String("expected", expectedDriverVersion))
} else if libcVersion != expectedLibcVersion {
msg = fmt.Sprintf(
"You are using modernc.org/libc %s which differs from the expected and tested %s.\n"+
"Please update your go.mod and manually set modernc.org/libc to %s, otherwise it could result in unexpected build or runtime errors "+
"(you may have to also run 'go clean -modcache' to clear the cache if the warning persists).",
libcVersion,
expectedLibcVersion,
expectedLibcVersion,
)
app.Logger().Warn(msg, slog.String("current", libcVersion), slog.String("expected", expectedLibcVersion))
}
if msg != "" && !app.IsDev() {
color.Yellow("\nWARN " + msg + "\n\n")
}
}