Skip to content

[dmt] add module-package-consistency rule with --fix support#381

Open
diyliv wants to merge 6 commits into
mainfrom
feature/module-package-consistency-fix
Open

[dmt] add module-package-consistency rule with --fix support#381
diyliv wants to merge 6 commits into
mainfrom
feature/module-package-consistency-fix

Conversation

@diyliv

@diyliv diyliv commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Cross-validation rule (module_package_consistency.go): compares overlapping fields (name, requirements.deckhouse, requirements.kubernetes, requirements.modules) between module.yaml and package.yaml when both files exist, reporting any divergence. package.yaml is treated as source of truth.
  • --fix mode (module_package_fix.go): AST-based YAML patching rewrites module.yaml fields to match package.yaml values. Preserves comments, field ordering, and untouched fields. Atomic write via temp file + rename. Maps package.yaml mandatory/conditional module groups to old-spec flat map format (!optional suffix).
  • Unmapped fields warning (lint_errors.go:ReportUnmappedFields): emits warnings for anyOf and subscribe sections in package.yaml that have no representation in module.yaml.
  • Flag wiring: adds --fix boolean flag to dmt lint command (flags.go).
  • Config wiring: consistency rule already had ModulePackageConsistencyRule config field; module.go now passes flags.Fix to the rule check.
  • Tests: 34 test cases covering name/deckhouse/kubernetes mismatch, module mandatory↔conditional cross-check, constraint mismatch, partial presence (only one file, empty fields), anyOf handling, fix mode for all field types, comment/ordering preservation in fix, and integration test matching user-provided example with non-standard required key and prefix !optional format.

Context

When both module.yaml and package.yaml exist in a module directory, their overlapping fields (name, deckhouse/kubernetes constraints, module dependencies) can diverge over time. This rule detects inconsistencies and --fix automatically patches module.yaml to match package.yaml (old spec format), treating package.yaml as the authoritative source.

Example

Module with diverging module.yaml and package.yaml:

module.yaml (before fix):

name: wrong-name
requirements:
  deckhouse: ">=0.0.0"
  kubernetes: ">=1.19.0"
  modules:
    required:
      prompp: "!optional >=0.16.0"

package.yaml (source of truth):

name: correct-name
requirements:
  deckhouse:
    constraint: ">=0.1.0"
  kubernetes:
    constraint: ">=1.20.0"
  modules:
    mandatory:
      - name: stronghold
        constraint: ">=0.1.0"
    conditional:
      - name: prompp
        constraint: ">=0.1.0"
    anyOf:
      - description: "cloud provider"
        modules:
          - name: cloud-provider-aws
            constraint: ">=1.0.0"

Before: dmt lint reports 6 errors (name, deckhouse, kubernetes, missing stronghold, wrong prompp constraint, extra missing-mod).

After dmt lint --fix:

  • All 6 errors resolved, module.yaml rewritten to:
name: correct-name
requirements:
  deckhouse: ">=0.1.0"
  kubernetes: ">=1.20.0"
  modules:
    stronghold: '>=0.1.0'
    prompp: '>=0.1.0 !optional'
  • Warning emitted: package.yaml has anyOf modules which cannot be represented in module.yaml

@diyliv diyliv self-assigned this Jun 8, 2026
@diyliv diyliv added enhancement New feature or request go Pull requests that update go code labels Jun 8, 2026
@diyliv diyliv marked this pull request as draft June 8, 2026 13:37
@diyliv diyliv requested a review from ldmonster June 10, 2026 15:31
@ldmonster ldmonster requested a review from Copilot June 11, 2026 10:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new module-package-consistency cross-validation capability with an optional --fix mode that rewrites module.yaml fields to match package.yaml (as the source of truth), plus CLI wiring and expanded test coverage.

Changes:

  • Introduce AST-based YAML patcher to auto-fix module.yaml (name, requirements.deckhouse/kubernetes, requirements.modules) from package.yaml.
  • Extend the consistency rule to optionally run the fixer and emit warnings for package.yaml sections that can’t be represented in module.yaml (anyOf, subscribe).
  • Wire --fix flag into dmt lint and add fix-mode tests.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
pkg/linters/module/rules/module_package_fix.go New YAML AST patching implementation for module.yaml rewrite.
pkg/linters/module/rules/module_package_consistency.go Adds --fix integration via FixFn and fix/warn flow before comparisons.
pkg/linters/module/rules/module_package_consistency_test.go Adds fix-mode tests validating rewrite behavior and warnings.
pkg/linters/module/module.go Wires CLI flags.Fix into the consistency rule invocation.
internal/flags/flags.go Adds dmt lint --fix boolean flag.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/linters/module/rules/module_package_consistency.go Outdated
Comment thread pkg/linters/module/rules/module_package_fix.go Outdated
Comment thread pkg/linters/module/rules/module_package_fix.go Outdated
Comment thread pkg/linters/module/rules/module_package_fix.go Outdated
Comment thread pkg/linters/module/rules/module_package_fix.go Outdated
Signed-off-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com>
@diyliv diyliv force-pushed the feature/module-package-consistency-fix branch from 6a5c8c3 to 52d6935 Compare June 13, 2026 17:01
@diyliv diyliv marked this pull request as ready for review June 13, 2026 17:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.

Comment on lines +66 to +70
if err := os.WriteFile(path, out, fixFilePerm); err != nil {
return fmt.Errorf("write %s: %w", ModuleConfigFilename, err)
}

return nil

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment thread cmd/dmt/main.go
diyliv and others added 2 commits June 15, 2026 15:41
…n patchModuleYAML and re-run lint after --fix applies changes

Signed-off-by: diyliv <onlogn081@gmail.com>
Signed-off-by: Stepan Paksashvili <stepan.paksashvili@flant.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request go Pull requests that update go code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants