Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Ensure check methods can't modify tokens array #233

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

alexjfisher
Copy link

@alexjfisher alexjfisher commented Feb 8, 2025

The functionality originally released in rodjek#296 was probably broken at the time, possibly made worse by later rubocop 'fixes' and also incompatible with ruby 3.4.

We now look at the stack trace only from the tokens wrapper method in checkplugin.

Fixes #225

@alexjfisher alexjfisher requested review from bastelfreak and a team as code owners February 8, 2025 18:15
@alexjfisher alexjfisher force-pushed the fix_duplicated_tokens branch from 2257895 to ec82d69 Compare February 8, 2025 18:17
@alexjfisher alexjfisher marked this pull request as draft February 8, 2025 22:13
@alexjfisher
Copy link
Author

After a bit more testing, it looks like label behaves differently across ruby versions...

@alexjfisher
Copy link
Author

@bastelfreak
Copy link
Collaborator

lol

The functionality originally released in rodjek#296
was probably broken at the time, possibly made worse by later rubocop
'fixes' and also incompatible with ruby 3.4.

We now look at the stack trace only from the `tokens` wrapper method in
`checkplugin`.

Fixes puppetlabs#225
Copy link

@ekohl ekohl left a comment

Choose a reason for hiding this comment

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

I think in the short term this is correct, but it may be worth looking ahead to what the API should be.

@tokens
end
def tokens(duplicate: false)
duplicate ? @tokens.dup : @tokens
Copy link

Choose a reason for hiding this comment

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

I had a crazy thought: rather than duplicating, would it make sense to freeze it? https://rubyapi.org/o/array#method-i-freeze says it will not do anything if already frozen, so should be cheap and you can't modify it.

Copy link
Author

Choose a reason for hiding this comment

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

There wasn't much information I could find about to why this was originally introduced, but other methods (such as fix) definitely do need to be able to modify the array.

end

def fix
tokens << :fix_token
Copy link

Choose a reason for hiding this comment

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

I don't think this is supposed to be a valid API because there's also the add_token to actually modify it.

def check
# Since we're calling `tokens` from a `check` method, we should get our own Array object.
# If we add an extra token to it, PuppetLint::Data.tokens should remain unchanged.
tokens << :extra_token
Copy link

Choose a reason for hiding this comment

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

When returning a frozen array it will raise a FrozenError. Perhaps we should catch that and raise a deprecation warning?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NoMethodError: undefined method '[]' for nil with ruby3.4.0dev
3 participants