lint

package
v0.39.30 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 21 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	SeverityErrorLevel = iota
	SeverityWarningLevel
	SeverityInfoLevel
)

Variables

View Source
var (
	SeverityError   = Severity{"ERROR", SeverityErrorLevel}
	SeverityWarning = Severity{"WARNING", SeverityWarningLevel}
	SeverityInfo    = Severity{"INFO", SeverityInfoLevel}
)
View Source
var AllRules = func(l *Linter) Rules {
	return Rules{
		{
			Name:        "forbidden-repository-used",
			Description: "do not specify a forbidden repository",
			Severity:    SeverityError,
			LintFunc: func(config config.Configuration) error {
				for _, repo := range config.Environment.Contents.BuildRepositories {
					if slices.Contains(forbiddenRepositories, repo) {
						return fmt.Errorf("forbidden repository %s is used", repo)
					}
				}
				for _, repo := range config.Environment.Contents.Repositories {
					if slices.Contains(forbiddenRepositories, repo) {
						return fmt.Errorf("forbidden repository %s is used", repo)
					}
				}
				return nil
			},
		},
		{
			Name:        "forbidden-keyring-used",
			Description: "do not specify a forbidden keyring",
			Severity:    SeverityError,
			LintFunc: func(config config.Configuration) error {
				for _, keyring := range config.Environment.Contents.Keyring {
					if slices.Contains(forbiddenKeyrings, keyring) {
						return fmt.Errorf("forbidden keyring %s is used", keyring)
					}
				}
				return nil
			},
		},
		{
			Name:        "valid-copyright-header",
			Description: "every package should have a valid copyright header",
			Severity:    SeverityInfo,
			LintFunc: func(config config.Configuration) error {
				if len(config.Package.Copyright) == 0 {
					return fmt.Errorf("copyright header is missing")
				}
				for _, c := range config.Package.Copyright {
					if c.License == "" {
						return fmt.Errorf("license is missing")
					}
				}
				return nil
			},
		},
		{
			Name:        "contains-epoch",
			Description: "every package should have an epoch",
			Severity:    SeverityError,
			LintFunc: func(_ config.Configuration) error {
				var node yaml.Node
				fileInfo, err := os.Stat(l.options.Path)
				if err != nil {
					return err
				}

				if fileInfo.IsDir() {
					return nil
				}

				yamlData, err := os.ReadFile(l.options.Path)
				if err != nil {
					return err
				}

				err = yaml.Unmarshal(yamlData, &node)
				if err != nil {
					return err
				}

				if node.Content == nil {
					return fmt.Errorf("config %s has no yaml content", l.options.Path)
				}

				pkg, err := renovate.NodeFromMapping(node.Content[0], "package")
				if err != nil {
					return err
				}

				if pkg == nil {
					return fmt.Errorf("config %s has no package content", l.options.Path)
				}

				err = containsKey(pkg, "epoch")
				if err != nil {
					return fmt.Errorf("config %s has no package.epoch", l.options.Path)
				}

				return nil
			},
		},
		{
			Name:        "valid-pipeline-fetch-uri",
			Description: "every fetch pipeline should have a valid uri",
			Severity:    SeverityError,
			LintFunc: func(config config.Configuration) error {
				for _, p := range config.Pipeline {
					uri, err := extractURI(p)
					if err != nil {
						return err
					}
					if uri == "" {
						continue
					}
					u, err := url.ParseRequestURI(uri)
					if err != nil {
						return fmt.Errorf("uri is invalid URL structure")
					}
					if !reValidHostname.MatchString(u.Host) {
						return fmt.Errorf("uri hostname %q is invalid", u.Host)
					}
				}
				return nil
			},
		},
		{
			Name:        "uri-mimic",
			Description: "every config should use a consistent hostname",
			Severity:    SeverityError,
			LintFunc: func(config config.Configuration) error {
				for _, p := range config.Pipeline {
					uri := p.With["uri"]
					if uri == "" {
						continue
					}
					u, err := url.ParseRequestURI(uri)
					if err != nil {

						return nil
					}
					host := u.Host
					if seenHosts[host] {
						continue
					}
					for k := range seenHosts {

						dist := levenshtein.DistanceForStrings([]rune(host), []rune(k), levenshtein.DefaultOptions)
						if hostEditDistanceExceptions[host] == k || hostEditDistanceExceptions[k] == host {
							continue
						}
						if dist <= minhostEditDistance {
							return fmt.Errorf("%q too similar to %q", host, k)
						}

						hostParts := strings.Split(host, ".")
						kParts := strings.Split(k, ".")
						if strings.Join(hostParts[:len(hostParts)-1], ".") == strings.Join(kParts[:len(kParts)-1], ".") {
							return fmt.Errorf("%q shares components with %q", host, k)
						}
					}
					seenHosts[host] = true
				}
				return nil
			},
		},

		{
			Name:        "valid-pipeline-fetch-digest",
			Description: "every fetch pipeline should have a valid digest",
			Severity:    SeverityError,
			LintFunc: func(config config.Configuration) error {
				for _, p := range config.Pipeline {
					if p.Uses == "fetch" {
						hashGiven := false
						if sha256, ok := p.With["expected-sha256"]; ok {
							if !reValidSHA256.MatchString(sha256) {
								return fmt.Errorf("expected-sha256 is not valid SHA256")
							}
							hashGiven = true
						}
						if sha512, ok := p.With["expected-sha512"]; ok {
							if !reValidSHA512.MatchString(sha512) {
								return fmt.Errorf("expected-sha512 is not valid SHA512")
							}
							hashGiven = true
						}
						if !hashGiven {
							return fmt.Errorf("expected-sha256 or expected-sha512 is missing")
						}
					}
				}
				return nil
			},
		},
		{
			Name:        "no-repeated-deps",
			Description: "no repeated dependencies",
			Severity:    SeverityError,
			LintFunc: func(config config.Configuration) error {
				seen := map[string]struct{}{}
				for _, p := range config.Environment.Contents.Packages {
					if _, ok := seen[p]; ok {
						return fmt.Errorf("package %s is duplicated in environment", p)
					}
					seen[p] = struct{}{}
				}
				return nil
			},
		},
		{
			Name:        "bad-template-var",
			Description: "bad template variable",
			Severity:    SeverityError,
			LintFunc: func(config config.Configuration) error {
				badTemplateVars := []string{
					"$pkgdir",
					"$pkgver",
					"$pkgname",
					"$srcdir",
				}

				hasBadVar := func(runs string) error {
					for _, badVar := range badTemplateVars {
						if strings.Contains(runs, badVar) {
							return fmt.Errorf("package contains likely incorrect template var %s", badVar)
						}
					}
					return nil
				}

				for _, s := range config.Pipeline {
					if err := hasBadVar(s.Runs); err != nil {
						return err
					}
				}

				for _, subPkg := range config.Subpackages {
					for _, subPipeline := range subPkg.Pipeline {
						if err := hasBadVar(subPipeline.Runs); err != nil {
							return err
						}
					}
				}
				return nil
			},
		},
		{
			Name:        "bad-version",
			Description: "version is malformed",
			Severity:    SeverityError,
			LintFunc: func(config config.Configuration) error {
				version := config.Package.Version
				if err := versions.ValidateWithoutEpoch(version); err != nil {
					return fmt.Errorf("invalid version %s, could not parse", version)
				}
				return nil
			},
		},
		{
			Name:        "prefer-git-checkout",
			Description: "prefer git-checkout over fetch to get sources from the upstream repository",
			Severity:    SeverityWarning,
			LintFunc: func(config config.Configuration) error {
				for _, p := range config.Pipeline {
					if p.Uses == "fetch" {
						return fmt.Errorf("fetch pipeline for %q should use git-checkout instead", p.With["uri"])
					}
				}
				return nil
			},
		},
		{
			Name:        "valid-pipeline-git-checkout-commit",
			Description: "every git-checkout pipeline should have a valid expected-commit",
			Severity:    SeverityError,
			LintFunc: func(config config.Configuration) error {
				for _, p := range config.Pipeline {
					if p.Uses == gitCheckout {
						if commit, ok := p.With["expected-commit"]; ok {
							if !reValidSHA1.MatchString(commit) {
								return fmt.Errorf("expected-commit is not valid SHA1")
							}
						} else {
							return fmt.Errorf("expected-commit is missing")
						}
					}
				}
				return nil
			},
		},
		{
			Name:        "valid-pipeline-git-checkout-tag-or-branch",
			Description: "every git-checkout pipeline should have a tag or branch",
			Severity:    SeverityError,
			LintFunc: func(config config.Configuration) error {
				for _, p := range config.Pipeline {
					if p.Uses == gitCheckout {
						_, hasTag := p.With["tag"]
						_, hasBranch := p.With["branch"]

						if !hasTag && !hasBranch {
							return fmt.Errorf("tag or branch is missing")
						}
					}
				}
				return nil
			},
		},
		{
			Name:        "check-when-version-changes",
			Description: "check comments to make sure they are updated when version changes",
			Severity:    SeverityError,
			LintFunc: func(config config.Configuration) error {
				re := regexp.MustCompile(`# CHECK-WHEN-VERSION-CHANGES: (.+)`)
				checkString := func(s string) error {
					match := re.FindStringSubmatch(s)
					if len(match) == 0 {
						return nil
					}
					for _, m := range match[1:] {
						if m != config.Package.Version {
							return fmt.Errorf("version in comment: %s does not match version in package: %s, check that it can be updated and update the comment", m, config.Package.Version)
						}
					}
					return nil
				}
				for _, p := range config.Pipeline {
					if err := checkString(p.Runs); err != nil {
						return err
					}
				}
				for _, subPkg := range config.Subpackages {
					for _, subPipeline := range subPkg.Pipeline {
						if err := checkString(subPipeline.Runs); err != nil {
							return err
						}
					}
				}
				return nil
			},
		},
		{
			Name:        "tagged-repository-in-environment-repos",
			Description: "remove tagged repositories like @local from the repositories block",
			Severity:    SeverityError,
			LintFunc: func(config config.Configuration) error {
				for _, repo := range config.Environment.Contents.BuildRepositories {
					if strings.HasPrefix(repo, "@") {
						return fmt.Errorf("repository %q is tagged", repo)
					}
				}
				for _, repo := range config.Environment.Contents.Repositories {
					if strings.HasPrefix(repo, "@") {
						return fmt.Errorf("repository %q is tagged", repo)
					}
				}
				return nil
			},
		},
		{
			Name:        "git-checkout-must-use-github-updates",
			Description: "when using git-checkout, must use github/git updates so we can get the expected-commit",
			Severity:    SeverityError,
			LintFunc: func(config config.Configuration) error {
				for _, p := range config.Pipeline {
					if p.Uses == gitCheckout && strings.HasPrefix(p.With["repository"], "https://github.com/") {
						if config.Update.Enabled && config.Update.GitHubMonitor == nil && config.Update.GitMonitor == nil {
							return fmt.Errorf("configure update.github/update.git when using git-checkout")
						}
					}
				}
				return nil
			},
		},
		{
			Name:        "valid-spdx-license",
			Description: "every package should have a valid SPDX license",
			Severity:    SeverityError,
			LintFunc: func(config config.Configuration) error {
				for _, c := range config.Package.Copyright {
					switch c.License {

					case "custom", "PROPRIETARY":
						continue
					}
					if valid, _ := spdxexp.ValidateLicenses([]string{c.License}); !valid {
						return fmt.Errorf("license %q is not valid SPDX license", c.License)
					}
				}
				return nil
			},
		},
		{
			Name:        "valid-package-or-subpackage-test",
			Description: "every package should have a valid main or subpackage test",
			Severity:    SeverityInfo,
			LintFunc: func(c config.Configuration) error {
				if c.Test != nil && len(c.Test.Pipeline) > 0 {

					return nil
				}

				for _, sp := range c.Subpackages {
					if sp.Test != nil && len(sp.Test.Pipeline) > 0 {

						return nil
					}
				}
				return fmt.Errorf("no main package or subpackage test found")
			},
		},
		{
			Name:        "update-disabled-reason",
			Description: "packages with auto-update disabled should have a reason",

			Severity: SeverityWarning,
			LintFunc: func(c config.Configuration) error {
				cfg := c.Update
				if cfg.Enabled {
					return nil
				}

				if !cfg.Enabled && cfg.ExcludeReason != "" {
					return nil
				}
				return fmt.Errorf("auto-update is disabled but no reason is provided")
			},
		},
		{
			Name:        "background-process-without-redirect",
			Description: "test steps should redirect output when running background processes",
			Severity:    SeverityWarning,
			LintFunc: func(c config.Configuration) error {
				checkSteps := func(steps []config.Pipeline) error {
					for _, s := range steps {
						if s.Runs == "" {
							continue
						}
						lines := strings.Split(s.Runs, "\n")
						for i, line := range lines {
							checkLine := line
							if strings.Contains(line, "&") && i+1 < len(lines) {
								checkLine += "\n" + lines[i+1]
							}

							needsRedirect := reBackgroundProcess.MatchString(checkLine) || reDaemonProcess.MatchString(line)
							if needsRedirect && !reOutputRedirect.MatchString(line) {
								return fmt.Errorf("background process missing output redirect: %s", strings.TrimSpace(line))
							}
						}
					}
					return nil
				}

				if c.Test != nil {
					if err := checkSteps(c.Test.Pipeline); err != nil {
						return err
					}
				}
				for _, sp := range c.Subpackages {
					if sp.Test != nil {
						if err := checkSteps(sp.Test.Pipeline); err != nil {
							return err
						}
					}
				}
				return nil
			},
		},
		{
			Name:        "sccache-enabled",
			Description: "sccache/enable is for local devel-use only",
			Severity:    SeverityError,
			LintFunc: func(c config.Configuration) error {
				if anyPipelineUses(c, sccacheEnable) {
					return fmt.Errorf("scacche/enable pipeline exists")
				}
				return nil
			},
		},
		{
			Name:        "valid-update-schedule",
			Description: "update schedule config should contain a valid period",
			Severity:    SeverityError,
			LintFunc: func(config config.Configuration) error {
				if config.Update.Schedule == nil {
					return nil
				}
				_, err := config.Update.Schedule.GetScheduleMessage()
				return err
			},
		},
		{
			Name:        "unused-var-transform",
			Description: "var-transforms that define variables which are never referenced",
			Severity:    SeverityWarning,
			LintFunc: func(cfg config.Configuration) error {
				// We need to check the raw YAML because ParseConfiguration already applies
				// variable substitutions, so the substituted values would be in the config.
				var unusedVars []string
				for _, vt := range cfg.VarTransforms {
					varRef := fmt.Sprintf("${{vars.%s}}", vt.To)

					usedInVarTransform := false
					for _, other := range cfg.VarTransforms {
						if other.From == varRef {
							usedInVarTransform = true
							break
						}
					}

					if usedInVarTransform || isVariableReferencedInRawYAML(cfg.Root(), varRef) {
						continue
					}

					unusedVars = append(unusedVars, vt.To)
				}

				if len(unusedVars) > 0 {
					if len(unusedVars) == 1 {
						return fmt.Errorf("var-transform creates unused variable %q", unusedVars[0])
					}
					return fmt.Errorf("var-transform creates unused variables %q", unusedVars)
				}
				return nil
			},
		},

		{
			Name:        "update-identifier-must-match-git-repository",
			Description: "update identifier must match the git repository",
			Severity:    SeverityError,
			LintFunc: func(config config.Configuration) error {

				if config.Update.GitHubMonitor == nil {
					return nil
				}

				pipelines := pickPipelinesUsing(gitCheckout, config.Pipeline)
				if len(pipelines) == 0 {
					return nil
				}

				for _, pipeline := range pipelines {

					repositoryURI, err := extractURI(*pipeline)
					if err != nil {
						return err
					}

					ghIdentifier := identifierFromRepoURI(repositoryURI)

					if strings.EqualFold(ghIdentifier, config.Update.GitHubMonitor.Identifier) {
						return nil
					}
				}

				return fmt.Errorf("update identifier does not match the repository URI")
			},
		},
		{
			Name:        "depends-on-unversioned-py-provides",
			Description: "packages should not depend on unversioned py3- provides for python multi-version packages",
			Severity:    SeverityWarning,
			LintFunc: func(cfg config.Configuration) error {
				var violations []string

				for _, dep := range cfg.Package.Dependencies.Runtime {
					if isUnversionedPy3Dependency(dep) {
						violations = append(violations, dep)
					}
				}

				for _, pkg := range cfg.Environment.Contents.Packages {
					if isUnversionedPy3Dependency(pkg) {
						violations = append(violations, pkg)
					}
				}

				for _, subPkg := range cfg.Subpackages {
					for _, dep := range subPkg.Dependencies.Runtime {
						if isUnversionedPy3Dependency(dep) {
							violations = append(violations, dep)
						}
					}
				}

				if len(violations) > 0 {
					return fmt.Errorf("found unversioned py3-* dependencies: %s (use py3.X-* or py3-supported-* instead)", strings.Join(violations, ", "))
				}

				return nil
			},
		},
		{
			Name:        "valid-version-stream-provides",
			Description: "a version-streamed package should provide its base name at its own version or a version in its stream",
			Severity:    SeverityError,
			LintFunc: func(cfg config.Configuration) error {
				m := reVersionStreamName.FindStringSubmatch(cfg.Package.Name)
				if m == nil {
					return nil
				}
				base, stream := m[1], m[2]

				for _, p := range cfg.Package.Dependencies.Provides {
					name, version, ok := strings.Cut(p, "=")
					if !ok || name != base {
						continue
					}

					if version == cfg.Package.Version || version == cfg.Package.FullVersion() {
						continue
					}
					if version == stream || strings.HasPrefix(version, stream+".") || strings.HasPrefix(version, stream+"-") {
						continue
					}
					return fmt.Errorf("provides %q does not match the package version or the %s stream, use %s=${{package.full-version}}", p, stream, base)
				}
				return nil
			},
		},
		{
			Name:        "fetch-templating",
			Description: "fetch URLs and git sources should use version templates to avoid drift",
			Severity:    SeverityWarning,
			LintFunc: func(config config.Configuration) error {
				return fetch.ValidateFetchTemplating(&config)
			},
		},
	}
}

AllRules is a list of all available rules to evaluate.

Functions

This section is empty.

Types

type ConditionFunc

type ConditionFunc func() bool

ConditionFunc is a function that checks if a rule should be executed.

type EvalResult

type EvalResult struct {
	// File is the name of the file that was evaluated against.
	File string

	// Errors is a list of validation errors for each rule.
	Errors EvalRuleErrors
}

EvalResult represents the result of an evaluation for a single configuration.

type EvalRuleError

type EvalRuleError struct {
	// Rule is the rule that caused the error.
	Rule Rule

	// Error is the error that occurred.
	Error error
}

EvalRuleError represents an error that occurred during single rule evaluation.

type EvalRuleErrors

type EvalRuleErrors []EvalRuleError

EvalRuleErrors returns a list of EvalError.

func (EvalRuleErrors) WrapErrors

func (e EvalRuleErrors) WrapErrors() error

WrapErrors wraps multiple errors into a single error.

type Function

type Function func(config.Configuration) error

Function is a function that lints a single configuration.

type Linter

type Linter struct {
	// contains filtered or unexported fields
}

Linter represents a linter instance.

func New

func New(opts ...Option) *Linter

New initializes a new instance of Linter.

func (*Linter) Lint

func (l *Linter) Lint(ctx context.Context, minSeverity Severity) (Result, error)

Lint evaluates all rules and returns the result.

func (*Linter) Print

func (l *Linter) Print(ctx context.Context, result Result)

func (*Linter) PrintRules

func (l *Linter) PrintRules(ctx context.Context)

PrintRules prints the rules to stdout.

type Option

type Option func(*Options)

Option represents a linter option.

func WithPath

func WithPath(path string) Option

WithPath sets the path to the file or directory to lint.

func WithSkipRules

func WithSkipRules(skipRules []string) Option

WithSkipRules sets the skip rules option.

type Options

type Options struct {
	// Path is the path to the file or directory to lint
	Path string

	// Skip rules removes the given slice of rules to be checked
	SkipRules []string
}

Options represents the options to configure the linter.

type Result

type Result []EvalResult

Result is a list of RuleResult.

func (Result) HasErrors

func (r Result) HasErrors() bool

HasErrors returns true if any of the EvalResult has an error.

type Rule

type Rule struct {
	// Name is the name of the rule.
	Name string

	// Description is the description of the rule.
	Description string

	// Severity is the severity of the rule.
	Severity Severity

	// LintFunc is the function that lints a single configuration.
	LintFunc Function

	// ConditionFuncs is a list of and-conditioned functions that check if the rule should be executed.
	ConditionFuncs []ConditionFunc
}

Rule represents a linter rule.

type Rules

type Rules []Rule

Rules is a list of Rule.

type Severity

type Severity struct {
	Name  string
	Value int
}

Severity is the severity level of a rule.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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