Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ lint: ## Check formatting and run the CLI lint smoke test.
cd "$$tmpdir" && $(GO) run ./cmd/vaar lint

bench: ## Run the lint smoke benchmark.
$(GO) test ./internal/lint -run '^$$' -bench '^BenchmarkRunnerSmoke$$' -benchmem
$(GO) test ./internal/application/lint -run '^$$' -bench '^BenchmarkApplicationSmoke$$' -benchmem

build: ## Build the CLI binary into bin/vaar.
@mkdir -p $(BIN_DIR)
Expand Down
160 changes: 128 additions & 32 deletions internal/application/lint/application.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright © 2026 envaar
// SPDX-License-Identifier: Apache-2.0

// Package lint coordinates one read-only lint use case. It composes scope
// resolution, dotenv loading, analysis conversion and the analysis-backed lint
// engine, but does not render output, map exit codes or mutate files.
// Package lint coordinates the lint application use case. It composes scope
// resolution, dotenv loading, analysis conversion, rule execution and the
// optional mutation lifecycle, but does not render output or map exit codes.
package lint

import (
Expand All @@ -13,50 +13,68 @@ import (
"github.com/envaar/vaar/internal/analysis"
analysisdotenv "github.com/envaar/vaar/internal/analysis/dotenv"
lintengine "github.com/envaar/vaar/internal/lint"
"github.com/envaar/vaar/internal/mutations"
"github.com/envaar/vaar/internal/scope"
sourcedotenv "github.com/envaar/vaar/internal/source/dotenv"
)

// Options selects the repository inputs and rules for one read-only lint run.
// Output, exit-code and mutation options deliberately do not belong here.
// Options selects the repository inputs, rules and optional safe-fix lifecycle
// for one lint run. Output and exit-code options deliberately do not belong
// here.
type Options struct {
Root string
Target string
TargetDir string
OnlyRules []string
SkipRules []string
Fix bool
}

// Dependencies provides narrow seams for the application boundary. Production
// callers should use New, which supplies the repository scope resolver and
// dotenv loader. The seams let tests prove that orchestration resolves and
// loads exactly once without introducing source interfaces into analysis.
// callers should use New, which supplies the repository scope resolver, dotenv
// loader and mutation planner. The seams let tests prove lifecycle behavior
// without introducing source interfaces into analysis.
type Dependencies struct {
ResolveScope func(scope.Options) (scope.Selection, error)
LoadDotenv func(paths, displayPaths []string) ([]sourcedotenv.Document, error)
BuildPlan func([]sourcedotenv.Document, []lintengine.Rule) (mutations.Plan, error)
ApplyPlan func(mutations.Plan) error
}

// Service is the read-only lint application service.
// Service is the lint application service.
type Service struct {
engine *lintengine.Engine
resolveScope func(scope.Options) (scope.Selection, error)
loadDotenv func(paths, displayPaths []string) ([]sourcedotenv.Document, error)
buildPlan func([]sourcedotenv.Document, []lintengine.Rule) (mutations.Plan, error)
applyPlan func(mutations.Plan) error
}

// Result contains the findings and the loaded state needed by the later
// mutation-planning flow. Reporters should consume Findings only;
// LoadedDocuments retain source-owned values for internal fix planning and are
// not output data.
// Result contains findings and the loaded state produced by one application
// run. Reporters should consume Findings only; LoadedDocuments retain
// source-owned values for internal fix planning and are not output data.
type Result struct {
Findings []lintengine.Finding
Selection scope.Selection
LoadedDocuments []sourcedotenv.Document
Snapshot analysis.Snapshot
SelectedRules []lintengine.Rule
Changed bool
}

// New constructs a read-only lint application service with the standard scope
// resolver and dotenv source loader.
// HasUnfixedFindings reports whether the final lint snapshot still contains a
// finding that was not repaired by the optional fix lifecycle.
func (r Result) HasUnfixedFindings() bool {
for _, finding := range r.Findings {
if !finding.Fixed {
return true
}
}
return false
}

// New constructs a lint application service with the standard scope resolver,
// dotenv source loader and mutation planner.
func New(rules ...lintengine.Rule) *Service {
return NewWithDependencies(Dependencies{}, rules...)
}
Expand All @@ -70,17 +88,27 @@ func NewWithDependencies(dependencies Dependencies, rules ...lintengine.Rule) *S
if dependencies.LoadDotenv == nil {
dependencies.LoadDotenv = sourcedotenv.LoadMany
}
if dependencies.BuildPlan == nil {
dependencies.BuildPlan = mutations.BuildPlan
}
if dependencies.ApplyPlan == nil {
dependencies.ApplyPlan = func(plan mutations.Plan) error {
return plan.Apply()
}
}

return &Service{
engine: lintengine.NewEngine(rules...),
resolveScope: dependencies.ResolveScope,
loadDotenv: dependencies.LoadDotenv,
buildPlan: dependencies.BuildPlan,
applyPlan: dependencies.ApplyPlan,
}
}

// Run resolves the requested scope once, loads each selected dotenv source
// once, converts the loaded documents into one analysis snapshot, and runs the
// selected lint rules. It performs no writes, rendering or exit-code mapping.
// Run resolves the requested scope once and executes the selected lint rules.
// With Fix enabled, it plans and applies safe changes, reloads the same scope,
// and reruns the same selected rule plan before returning the final result.
func (s *Service) Run(ctx context.Context, opts Options) (Result, error) {
plan, err := s.engine.SelectRulePlan(lintengine.EngineOptions{
OnlyRules: opts.OnlyRules,
Expand All @@ -89,7 +117,6 @@ func (s *Service) Run(ctx context.Context, opts Options) (Result, error) {
if err != nil {
return Result{}, err
}
selected := plan.Rules()
if err := ctx.Err(); err != nil {
return Result{}, err
}
Expand All @@ -106,37 +133,112 @@ func (s *Service) Run(ctx context.Context, opts Options) (Result, error) {
return Result{}, err
}

return s.runWithSelection(ctx, opts, selection, plan)
}

// RunWithSelection executes the configured rules against a pre-resolved scope
// selection. Callers that already resolved scope, such as the CLI preflight for
// output-path validation, can reuse the same selection without a second scope
// walk.
func (s *Service) RunWithSelection(ctx context.Context, opts Options, selection scope.Selection) (Result, error) {
plan, err := s.engine.SelectRulePlan(lintengine.EngineOptions{
OnlyRules: opts.OnlyRules,
SkipRules: opts.SkipRules,
})
if err != nil {
return Result{}, err
}
if err := ctx.Err(); err != nil {
return Result{}, err
}

return s.runWithSelection(ctx, opts, selection, plan)
}

func (s *Service) runWithSelection(ctx context.Context, opts Options, selection scope.Selection, plan lintengine.RulePlan) (Result, error) {
selected := plan.Rules()
documents, snapshot, findings, err := s.loadAndRun(ctx, selection, plan)
if err != nil {
return Result{}, err
}

result := Result{
Findings: findings,
Selection: selection,
LoadedDocuments: documents,
Snapshot: snapshot,
SelectedRules: selected,
}
if !opts.Fix {
return result, nil
}
if err := ctx.Err(); err != nil {
return Result{}, err
}

mutationPlan, err := s.buildPlan(documents, selected)
if err != nil {
return Result{}, fmt.Errorf("plan lint fixes: %w", err)
}
if changes := mutationPlan.Changes(); len(changes) == 0 {
return result, nil
}
if err := ctx.Err(); err != nil {
return Result{}, err
}
if err := s.applyPlan(mutationPlan); err != nil {
return Result{}, fmt.Errorf("apply lint fixes: %w", err)
}
if err := ctx.Err(); err != nil {
return Result{}, err
}

updatedDocuments, updatedSnapshot, remaining, err := s.loadAndRun(ctx, selection, plan)
if err != nil {
return Result{}, err
}

findings = markFixedFindings(findings, remaining)
lintengine.SortFindings(findings)
result.Findings = findings
result.LoadedDocuments = updatedDocuments
result.Snapshot = updatedSnapshot
result.Changed = true
return result, nil
}

func (s *Service) loadAndRun(ctx context.Context, selection scope.Selection, plan lintengine.RulePlan) ([]sourcedotenv.Document, analysis.Snapshot, []lintengine.Finding, error) {
displayPaths := make([]string, len(selection.Paths))
for i, path := range selection.Paths {
displayPaths[i] = selection.DisplayPath(path)
}

documents, err := s.loadDotenv(selection.Paths, displayPaths)
if err != nil {
return Result{}, fmt.Errorf("load lint sources: %w", err)
return nil, analysis.Snapshot{}, nil, fmt.Errorf("load lint sources: %w", err)
}
if len(documents) != len(selection.Paths) {
return Result{}, fmt.Errorf(
return nil, analysis.Snapshot{}, nil, fmt.Errorf(
"load lint sources: got %d documents for %d selected paths",
len(documents), len(selection.Paths),
)
}
for i, document := range documents {
if document.SourcePath != selection.Paths[i] {
return Result{}, fmt.Errorf(
return nil, analysis.Snapshot{}, nil, fmt.Errorf(
"load lint sources: document %d has source path %q, want %q",
i, document.SourcePath, selection.Paths[i],
)
}
if document.Path != displayPaths[i] {
return Result{}, fmt.Errorf(
return nil, analysis.Snapshot{}, nil, fmt.Errorf(
"load lint sources: document %d has display path %q, want %q",
i, document.Path, displayPaths[i],
)
}
}
if err := ctx.Err(); err != nil {
return Result{}, err
return nil, analysis.Snapshot{}, nil, err
}

inputs := make([]analysisdotenv.DocumentInput, len(documents))
Expand All @@ -150,14 +252,8 @@ func (s *Service) Run(ctx context.Context, opts Options) (Result, error) {
snapshot := analysis.NewSnapshot(analysisdotenv.FromDocuments(inputs))
findings, err := s.engine.RunPlan(ctx, snapshot, plan)
if err != nil {
return Result{}, fmt.Errorf("run lint engine: %w", err)
return nil, analysis.Snapshot{}, nil, fmt.Errorf("run lint engine: %w", err)
}

return Result{
Findings: findings,
Selection: selection,
LoadedDocuments: documents,
Snapshot: snapshot,
SelectedRules: selected,
}, nil
return documents, snapshot, findings, nil
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
/*
Copyright © 2026 envaar
SPDX-License-Identifier: Apache-2.0
*/
// Copyright © 2026 envaar
// SPDX-License-Identifier: Apache-2.0

// Package lint_test measures the runner against a representative dotenv
// fixture.
// Package lint_test measures the application service against a representative
// dotenv fixture.
package lint_test

import (
Expand All @@ -14,11 +12,11 @@ import (
"strings"
"testing"

"github.com/envaar/vaar/internal/lint"
applicationlint "github.com/envaar/vaar/internal/application/lint"
"github.com/envaar/vaar/internal/lint/rules"
)

func BenchmarkRunnerSmoke(b *testing.B) {
func BenchmarkApplicationSmoke(b *testing.B) {
root := b.TempDir()
fixture := []byte(strings.Join([]string{
"KEY=value",
Expand All @@ -32,12 +30,12 @@ func BenchmarkRunnerSmoke(b *testing.B) {
b.Fatalf("write benchmark fixture: %v", err)
}

runner := lint.NewRunner(rules.All()...)
service := applicationlint.New(rules.All()...)
b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
if _, err := runner.Run(context.Background(), lint.Options{Root: root}); err != nil {
if _, err := service.Run(context.Background(), applicationlint.Options{Root: root}); err != nil {
b.Fatalf("benchmark run failed: %v", err)
}
}
Expand Down
Loading
Loading