puppetlabs / puppet-lint

Check that your Puppet manifests conform to the style guide

Home Page:https://puppetlabs.github.io/puppet-lint/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pdk validate fails if code has specific style guide incompatibility

tuxmea opened this issue · comments

Describe the Bug

In some cases lint is unable to autofix the string containing only a variable lint check.

Expected Behavior

Lint will autofix

Steps to Reproduce

See code below

Environment

  • Version PDK 3.0.0
  • Platform Mac OS 13.6 and RHEL 8 and RHEL 7

Additional Context


puppet-lint version: 4.0.0
ruby version: 3.2.2-p53
platform: x86_64-darwin21
file path: manifests/init.pp
file contents:

# @summary A short summary of the purpose of this class
#
# A description of what this class does
#
# @example
#   include test
class test {
  $var = '/tmp/test'
  if $::operatingsystemmajrelease == 'RedHat' {
    file {"${test}":
      ensure => file,
    }
  }
}

error:

NoMethodError: undefined method `-' for nil:NilClass
/opt/puppetlabs/pdk/share/cache/ruby/3.2.0/gems/puppet-lint-4.0.0/lib/puppet-lint/data.rb:67:in `insert'
/opt/puppetlabs/pdk/share/cache/ruby/3.2.0/gems/puppet-lint-4.0.0/lib/puppet-lint/checkplugin.rb:62:in `add_token'
/opt/puppetlabs/pdk/share/cache/ruby/3.2.0/gems/puppet-lint-manifest_whitespace-check-0.3.0/lib/puppet-lint/plugins/check_manifest_whitespace_opening_brace.rb:87:in `fix'
/opt/puppetlabs/pdk/share/cache/ruby/3.2.0/gems/puppet-lint-4.0.0/lib/puppet-lint/checkplugin.rb:42:in `block in fix_problems'
/opt/puppetlabs/pdk/share/cache/ruby/3.2.0/gems/puppet-lint-4.0.0/lib/puppet-lint/checkplugin.rb:38:in `each'
/opt/puppetlabs/pdk/share/cache/ruby/3.2.0/gems/puppet-lint-4.0.0/lib/puppet-lint/checkplugin.rb:38:in `fix_problems'
/opt/puppetlabs/pdk/share/cache/ruby/3.2.0/gems/puppet-lint-4.0.0/lib/puppet-lint/checks.rb:67:in `block in run'
/opt/puppetlabs/pdk/share/cache/ruby/3.2.0/gems/puppet-lint-4.0.0/lib/puppet-lint/checks.rb:65:in `each'
/opt/puppetlabs/pdk/share/cache/ruby/3.2.0/gems/puppet-lint-4.0.0/lib/puppet-lint/checks.rb:65:in `run'
/opt/puppetlabs/pdk/share/cache/ruby/3.2.0/gems/puppet-lint-4.0.0/lib/puppet-lint.rb:226:in `run'
/opt/puppetlabs/pdk/share/cache/ruby/3.2.0/gems/puppet-lint-4.0.0/lib/puppet-lint/bin.rb:85:in `block in run'
/opt/puppetlabs/pdk/share/cache/ruby/3.2.0/gems/puppet-lint-4.0.0/lib/puppet-lint/bin.rb:80:in `each'
/opt/puppetlabs/pdk/share/cache/ruby/3.2.0/gems/puppet-lint-4.0.0/lib/puppet-lint/bin.rb:80:in `run'
/opt/puppetlabs/pdk/share/cache/ruby/3.2.0/gems/puppet-lint-4.0.0/bin/puppet-lint:7:in `<top (required)>'

I get the impression that it's a bug in the plugin. If we check the stack trace:

# Public: Add new token.
def insert(index, token)
current_token = tokens[index - 1]

That ends up being nil - 1. In #172 I tried to formalize the API, but implicitly it says index must be an integer.

Looking at the next frame:

def add_token(index, token)
PuppetLint::Data.insert(index, token)

That just passes it along, so the same API.

The next frame is the plugin:

https://github.com/voxpupuli/puppet-lint-manifest_whitespace-check/blob/ff2a72941ef32686f36ff41d98b9f0bbac965b76/lib/puppet-lint/plugins/check_manifest_whitespace_opening_brace.rb#L87

So tokens.index(token) ends up being nil. Note it is in the fix(problem) method and you have multiple problems there, so I think 2 plugins both fix the token stream. What probably happens is that one rewrites it to

    file {$test:
      ensure => file,
    }

And then the whitespace check wants to rewrite it to:

    file { "${test}":
      ensure => file,
    }

But the " token no longer exists, so it is nil.