Skip to content

Conversation

@gavindidrichsen
Copy link

@gavindidrichsen gavindidrichsen commented Nov 6, 2025

Context

GitHub Issue #254 revealed that puppet-lint's --fix flag was incorrectly overwriting YAML files with Puppet manifest content.

The root cause was in the file processing logic in bin.rb, which was applying fix logic to all discovered files (*.pp, *.yaml, *.yml) regardless of file type. This resulted in YAML configuration files being corrupted with Puppet manifest content, creating potential data loss scenarios in production environments.

Changes

Since each file has an associated PuppetLint instance, then I refactored the file-write logic from bin.rb into the PuppetLint. This refactor makes each PuppetLint file instance aware of its file type and fix eligibility. Each PuppetLint instance is now responsible for its own behavior based on its file type (and other constraints)

Now bin.rb simply calls fix() on each PuppetLint instance if the user passes the --fix flag. Only if the instance is elegible for a fix will this occur.

Testing

  • manually tested the fix with bundle exec puppet-lint --fix spec/fixtures/test/manifests/issue_254_overwriting_yaml/
  • added rspec automated tests as well.

@gavindidrichsen gavindidrichsen force-pushed the cat_2484_fix_yaml_overwrite branch 2 times, most recently from e097856 to 185f57a Compare November 6, 2025 15:21
…tion

Verifies that --fix flag does not corrupt YAML files when processing
directories containing both .pp and .yaml files.

Signed-off-by: Gavin Didrichsen <[email protected]>
This change is simple in that it moves some of the logic to the PuppetLint.  Every file gets its own PuppetLint object with its own list of problems, checks, etc.  Therefore, each PuppetLint should know itself (*.pp?  *.yml?  *.yaml?) and whether it is a valid candidate for fixing.  Keep all validation and fix logic on the PuppetLint instead of in the bin.rb executable.

Signed-off-by: Gavin Didrichsen <[email protected]>
…puppet-lint

Tests supports_fixes? and should_write_fixes? methods that implement
selective fixing based on file extension.

Signed-off-by: Gavin Didrichsen <[email protected]>
@gavindidrichsen gavindidrichsen force-pushed the cat_2484_fix_yaml_overwrite branch from 185f57a to 348fe92 Compare November 6, 2025 15:43
This commit fixes the following warning that appeared at the top of the rspec output:

```
WARNING: `around(:context)` hooks are not supported and behave like `around(:example)`. Called from ...
```

Signed-off-by: Gavin Didrichsen <[email protected]>
@gavindidrichsen gavindidrichsen force-pushed the cat_2484_fix_yaml_overwrite branch from cc7a85c to 04c7065 Compare November 6, 2025 15:53
@gavindidrichsen
Copy link
Author

My first commit is a test that verifies the issue, e.g.,

  puppet-lint git:(b25f35d)  bundle exec rspec
...
...
  when fixing a directory containing both .pp and .yaml files
    does not overwrite YAML files (FAILED - 1)
    exitstatus

Failures:

  1) PuppetLint::Bin when fixing a directory containing both .pp and .yaml files does not overwrite YAML files
     Failure/Error: expect(yaml_after).to eq(original_yaml)
       
       expected: "---\nclasses:\n  foo-bar:\n    parameters: []\n    resources:\n      notify:\n        - title: hello\n"
            got: "class foo-bar {\n  notify { 'hello': }\n}\n"
       
       (compared using ==)
       
       Diff:
       @@ -1,7 +1,3 @@
       ----
       -classes:
       -  foo-bar:
       -    parameters: []
       -    resources:
       -      notify:
       -        - title: hello
       +class foo-bar {
       +  notify { 'hello': }
       +}
       
       
     # ./spec/unit/puppet-lint/bin_spec.rb:630:in `block (3 levels) in <top (required)>'

Finished in 2.89 seconds (files took 3.62 seconds to load)
125 examples, 1 failure

After applying the 2nd commit fix, then the tests pass green.

@gavindidrichsen gavindidrichsen marked this pull request as ready for review November 6, 2025 15:55
@gavindidrichsen gavindidrichsen requested review from a team and bastelfreak as code owners November 6, 2025 15:55
@gavindidrichsen gavindidrichsen added the bug Something isn't working label Nov 6, 2025
@david22swan david22swan merged commit f566bf1 into main Nov 6, 2025
8 checks passed
@david22swan david22swan deleted the cat_2484_fix_yaml_overwrite branch November 6, 2025 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants