fix

package
v0.27.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 18, 2026 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Overview

Package fix provides auto-fix infrastructure for tally. It includes types for fix safety levels, fix resolution, and fix application.

Package fix provides fix application and resolution.

Index

Constants

View Source
const (
	// FixSafe means the fix is always correct and won't change behavior.
	FixSafe = rules.FixSafe

	// FixSuggestion means the fix is likely correct but may need review.
	FixSuggestion = rules.FixSuggestion

	// FixUnsafe means the fix might change behavior significantly.
	FixUnsafe = rules.FixUnsafe
)
View Source
const (
	// FixModeNever disables fixes even with --fix.
	FixModeNever = config.FixModeNever

	// FixModeExplicit requires --fix-rule to apply.
	FixModeExplicit = config.FixModeExplicit

	// FixModeAlways applies with --fix when safety threshold is met (default).
	FixModeAlways = config.FixModeAlways

	// FixModeUnsafeOnly requires --fix-unsafe to apply.
	FixModeUnsafeOnly = config.FixModeUnsafeOnly
)

Variables

This section is empty.

Functions

func ApplyEdit added in v0.20.0

func ApplyEdit(content []byte, edit rules.TextEdit) []byte

ApplyEdit applies a single text edit to content. The edit replaces the range [Start, End) with NewText. Location uses 1-based line numbers (BuildKit convention); we convert to 0-based for array indexing.

func BuildFixModes

func BuildFixModes(cfg *config.Config) map[string]FixMode

BuildFixModes extracts per-rule fix mode settings from a config. Returned keys use the canonical rule code format: "<namespace>/<ruleName>".

Nil is returned when cfg is nil.

func ClearResolvers

func ClearResolvers()

ClearResolvers removes all registered resolvers. This is primarily useful for testing.

func FilterFixedViolations added in v0.21.0

func FilterFixedViolations(
	violations []rules.Violation,
	fixResult *Result,
	fileConfigs map[string]*config.Config,
) []rules.Violation

FilterFixedViolations removes violations that were fixed and suppresses stale violations from rules implementing PostFixRevalidator.

A violation is removed if:

  • It was directly fixed (exact location+rule match in fixResult), or
  • The rule implements PostFixRevalidator and RevalidateAfterFix returns false for the modified file content (the fix from another rule resolved the condition).

func ListResolvers

func ListResolvers() []string

ListResolvers returns the IDs of all registered resolvers.

func RegisterResolver

func RegisterResolver(r FixResolver)

RegisterResolver adds a resolver to the global registry. Panics if a resolver with the same ID is already registered.

Types

type AppliedFix

type AppliedFix struct {
	// RuleCode identifies which rule this fix is for.
	RuleCode string

	// Description explains what the fix did.
	Description string

	// Location is where the fix was applied.
	Location rules.Location

	// Edits are the original (pre-adjustment) text edits of this fix.
	// Positions reference the original document content, making them
	// suitable for direct conversion to LSP TextEdits.
	Edits []rules.TextEdit
}

AppliedFix records a successfully applied fix.

type FileChange

type FileChange struct {
	// Path is the file path.
	Path string

	// FixesApplied lists the fixes that were applied.
	FixesApplied []AppliedFix

	// FixesSkipped lists fixes that couldn't be applied.
	FixesSkipped []SkippedFix

	// OriginalContent is the file content before fixes.
	OriginalContent []byte

	// ModifiedContent is the file content after fixes.
	ModifiedContent []byte
}

FileChange describes changes to a single file.

func (*FileChange) HasChanges

func (fc *FileChange) HasChanges() bool

HasChanges returns true if any fixes were applied to this file.

type FixMode

type FixMode = config.FixMode

Re-export FixMode from config for convenience.

type FixResolver

type FixResolver interface {
	// ID returns the unique identifier for this resolver.
	// This matches the ResolverID field in SuggestedFix.
	ID() string

	// Resolve computes the actual edits for a fix.
	// The fix.ResolverData contains resolver-specific data needed for resolution.
	// The resolveCtx provides the current file content after sync fixes.
	// Returns the edits to apply, or an error if resolution fails.
	//
	// Implementations should respect context cancellation and timeouts.
	Resolve(ctx context.Context, resolveCtx ResolveContext, fix *rules.SuggestedFix) ([]rules.TextEdit, error)
}

FixResolver computes fix edits that require external data or operate on content after other fixes have been applied.

Resolvers are called AFTER sync fixes have been applied to the file. This allows structural transforms to operate on already-modified content, avoiding position drift issues.

Examples:

  • Image digest resolver: fetches digests from container registries
  • Heredoc resolver: transforms RUN instructions after content fixes

func GetResolver

func GetResolver(id string) FixResolver

GetResolver returns the resolver with the given ID, or nil if not found.

type FixSafety

type FixSafety = rules.FixSafety

Re-export FixSafety from rules package for convenience. This allows fix package users to use fix.FixSafe instead of rules.FixSafe.

type Fixer

type Fixer struct {
	// SafetyThreshold determines the minimum safety level for fixes.
	// Only fixes with Safety <= SafetyThreshold will be applied.
	SafetyThreshold FixSafety

	// RuleFilter limits fixes to specific rule codes.
	// If empty, all rules are eligible.
	RuleFilter []string

	// FixModes maps file paths to their per-rule fix modes.
	// Outer key is the normalized file path, inner key is the rule code.
	// Uses config.FixMode constants (FixModeAlways, FixModeNever, etc.).
	// If nil or a file/rule is not present, FixModeAlways is assumed.
	FixModes map[string]map[string]FixMode

	// Concurrency sets the number of parallel async resolutions.
	// Defaults to 4 if not set.
	Concurrency int
}

Fixer applies suggested fixes to source files.

func (*Fixer) Apply

func (f *Fixer) Apply(ctx context.Context, violations []rules.Violation, sources map[string][]byte) (*Result, error)

Apply processes violations and applies their suggested fixes. sources maps file paths to their original content.

Fix application follows a two-phase approach to avoid position drift:

  1. Apply sync fixes first (NeedsResolve=false) - these have pre-computed edits
  2. Resolve and apply async fixes (NeedsResolve=true) - resolvers see the modified content

This ensures structural transforms (like prefer-run-heredoc) operate on content that has already been modified by content fixes (like apt → apt-get).

type ResolveContext

type ResolveContext struct {
	// FilePath is the path to the file being fixed.
	FilePath string

	// Content is the current file content after sync fixes have been applied.
	// Resolvers should parse this to compute correct positions.
	Content []byte
}

ResolveContext provides context for fix resolution. This includes the current file content after sync fixes have been applied, allowing resolvers to compute edits based on the modified state.

type Result

type Result struct {
	// Changes contains modifications for each file.
	Changes map[string]*FileChange
}

Result contains the outcome of applying fixes.

func (*Result) FilesModified

func (r *Result) FilesModified() int

FilesModified returns the number of files with actual changes.

func (*Result) TotalApplied

func (r *Result) TotalApplied() int

TotalApplied returns the total number of fixes applied across all files.

func (*Result) TotalSkipped

func (r *Result) TotalSkipped() int

TotalSkipped returns the total number of fixes skipped across all files.

type SkipReason

type SkipReason int

SkipReason explains why a fix was skipped.

const (
	// SkipConflict means the fix overlaps with another fix.
	SkipConflict SkipReason = iota

	// SkipSafety means the fix is below the safety threshold.
	SkipSafety

	// SkipRuleFilter means the rule is not in the --fix-rule list.
	SkipRuleFilter

	// SkipResolveError means the async resolver failed.
	SkipResolveError

	// SkipNoEdits means the fix has no edits (invalid fix).
	SkipNoEdits

	// SkipFixMode means the rule's fix mode config disallows fixing.
	SkipFixMode
)

func (SkipReason) String

func (r SkipReason) String() string

String returns a human-readable description of the skip reason.

type SkippedFix

type SkippedFix struct {
	// RuleCode identifies which rule this fix is for.
	RuleCode string

	// Reason explains why the fix was skipped.
	Reason SkipReason

	// Location is where the fix would have been applied.
	Location rules.Location

	// Error contains the error message if Reason is SkipResolveError.
	Error string
}

SkippedFix records a fix that couldn't be applied.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL