|
| 1 | +package validation |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "context" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "regexp" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/livetemplate/lvt/internal/validator" |
| 14 | +) |
| 15 | + |
| 16 | +// compilerErrorPattern matches Go compiler errors like "file.go:10:5: message". |
| 17 | +// This requires the file:line:col format. Diagnostics that omit the column |
| 18 | +// (e.g. linker messages) fall through to the raw-output fallback intentionally. |
| 19 | +var compilerErrorPattern = regexp.MustCompile(`^(.+?\.go):(\d+):\d+:\s+(.+)$`) |
| 20 | + |
| 21 | +// CompilationCheck runs sqlc generate (optional), go mod tidy (opt-in), and |
| 22 | +// go build on an app directory. By default only go build runs; set |
| 23 | +// RunGoModTidy or RunSqlc to true to enable those steps. |
| 24 | +type CompilationCheck struct { |
| 25 | + // RunGoModTidy runs go mod tidy before building. Off by default because |
| 26 | + // it mutates go.mod/go.sum in the target directory. |
| 27 | + RunGoModTidy bool |
| 28 | + // RunSqlc runs sqlc generate before building (if sqlc.yaml exists and |
| 29 | + // queries.sql has content). Off by default because it requires a locally |
| 30 | + // installed sqlc binary. |
| 31 | + RunSqlc bool |
| 32 | +} |
| 33 | + |
| 34 | +func (c *CompilationCheck) Name() string { return "compilation" } |
| 35 | + |
| 36 | +func (c *CompilationCheck) Run(ctx context.Context, appPath string) *validator.ValidationResult { |
| 37 | + result := validator.NewValidationResult() |
| 38 | + env := envWithGOWORKOff() |
| 39 | + |
| 40 | + // sqlc generate (opt-in) |
| 41 | + if c.RunSqlc { |
| 42 | + c.runSqlc(ctx, appPath, env, result) |
| 43 | + if result.HasErrors() { |
| 44 | + return result // sqlc failure means build results would be unreliable |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // go mod tidy (opt-in — mutates go.mod/go.sum) |
| 49 | + if c.RunGoModTidy { |
| 50 | + tidyCmd := exec.CommandContext(ctx, "go", "mod", "tidy") |
| 51 | + tidyCmd.Dir = appPath |
| 52 | + tidyCmd.Env = env |
| 53 | + if output, err := tidyCmd.CombinedOutput(); err != nil { |
| 54 | + result.AddError("go mod tidy failed: "+strings.TrimSpace(string(output)), "go.mod", 0) |
| 55 | + return result |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // go build ./... |
| 60 | + buildCmd := exec.CommandContext(ctx, "go", "build", "./...") |
| 61 | + buildCmd.Dir = appPath |
| 62 | + buildCmd.Env = env |
| 63 | + if output, err := buildCmd.CombinedOutput(); err != nil { |
| 64 | + parseCompilerErrors(string(output), result) |
| 65 | + if result.ErrorCount() == 0 { |
| 66 | + // Couldn't parse structured errors — add the raw output. |
| 67 | + result.AddError("compilation failed: "+strings.TrimSpace(string(output)), "", 0) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return result |
| 72 | +} |
| 73 | + |
| 74 | +func (c *CompilationCheck) runSqlc(ctx context.Context, appPath string, env []string, result *validator.ValidationResult) { |
| 75 | + sqlcCfg := filepath.Join(appPath, "database/sqlc.yaml") |
| 76 | + if _, err := os.Stat(sqlcCfg); err != nil { |
| 77 | + return // no sqlc config — nothing to do |
| 78 | + } |
| 79 | + if !hasQueries(filepath.Join(appPath, "database/queries.sql")) { |
| 80 | + return // no actual queries |
| 81 | + } |
| 82 | + |
| 83 | + // Require a locally installed sqlc binary; skip if not found. |
| 84 | + sqlcBin, err := exec.LookPath("sqlc") |
| 85 | + if err != nil { |
| 86 | + result.AddWarning("sqlc not found in PATH, skipping sqlc generate", "database/sqlc.yaml", 0) |
| 87 | + return |
| 88 | + } |
| 89 | + |
| 90 | + cmd := exec.CommandContext(ctx, sqlcBin, "generate", "-f", sqlcCfg) |
| 91 | + cmd.Dir = appPath |
| 92 | + cmd.Env = env |
| 93 | + if output, err := cmd.CombinedOutput(); err != nil { |
| 94 | + result.AddError("sqlc generate failed: "+strings.TrimSpace(string(output)), "database/sqlc.yaml", 0) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// envWithGOWORKOff returns the current env with GOWORK=off. |
| 99 | +func envWithGOWORKOff() []string { |
| 100 | + environ := os.Environ() |
| 101 | + env := make([]string, 0, len(environ)+1) |
| 102 | + for _, e := range environ { |
| 103 | + if !strings.HasPrefix(e, "GOWORK=") { |
| 104 | + env = append(env, e) |
| 105 | + } |
| 106 | + } |
| 107 | + return append(env, "GOWORK=off") |
| 108 | +} |
| 109 | + |
| 110 | +// hasQueries returns true if the file contains at least one non-comment line. |
| 111 | +func hasQueries(path string) bool { |
| 112 | + f, err := os.Open(path) |
| 113 | + if err != nil { |
| 114 | + return false |
| 115 | + } |
| 116 | + defer f.Close() |
| 117 | + |
| 118 | + scanner := bufio.NewScanner(f) |
| 119 | + for scanner.Scan() { |
| 120 | + line := strings.TrimSpace(scanner.Text()) |
| 121 | + if line != "" && !strings.HasPrefix(line, "--") { |
| 122 | + return true |
| 123 | + } |
| 124 | + } |
| 125 | + // If scanner hit an I/O error, conservatively return false. |
| 126 | + return false |
| 127 | +} |
| 128 | + |
| 129 | +// parseCompilerErrors extracts file:line errors from go build output. |
| 130 | +func parseCompilerErrors(output string, result *validator.ValidationResult) { |
| 131 | + for _, line := range strings.Split(output, "\n") { |
| 132 | + line = strings.TrimSpace(line) |
| 133 | + if m := compilerErrorPattern.FindStringSubmatch(line); m != nil { |
| 134 | + lineNum, _ := strconv.Atoi(m[2]) |
| 135 | + file := strings.TrimPrefix(m[1], "./") |
| 136 | + result.AddError(m[3], file, lineNum) |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments