Skip to content

Fix boolean parameters#469

Merged
amitkarsale merged 1 commit intopuppetlabs:mainfrom
alexjfisher:fix_booleans
Mar 17, 2025
Merged

Fix boolean parameters#469
amitkarsale merged 1 commit intopuppetlabs:mainfrom
alexjfisher:fix_booleans

Conversation

@alexjfisher
Copy link
Copy Markdown
Contributor

The unsafe rubocop fixes done in
189ac29 broke the boolean parameters.

Before this commit, if you explicitly tried to set either of them to false, they would actually be set to the symbol :false with is not falsey so would act as if you'd set them to true.

@alexjfisher alexjfisher requested a review from a team as a code owner February 19, 2025 13:38
@alexjfisher
Copy link
Copy Markdown
Contributor Author

Also see comments in #430 (review)

bastelfreak
bastelfreak previously approved these changes Feb 19, 2025

newvalues(true, false)

defaultto false
Copy link
Copy Markdown

@joshcooper joshcooper Mar 6, 2025

Choose a reason for hiding this comment

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

The defaultto needs to change to symbol :false too. See 189ac29#diff-44ad177ebcd7857beabf27a6995960e777ebfeac942fb4ec34b1d6720a934e6b

Also the cause of this pain is https://puppet.atlassian.net/browse/PUP-8442

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.

Good spot. It appeared to work as false. Not sure that's because the munging from Puppet::Parameter::Boolean is being applied to the defaultto or if the default (ie as if defaultto isn't specified) is just falsey anyway. Either way, I don't want it to be working by accident, so will update it as suggested.

Meanwhile... do you think there are other custom types where the same rubocop 'fix' might have been repeated?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Seems likely this could be an issue in other modules. Hopefully that cop isn't autocorrect-able?

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.

It is if you use --autocorrect-all...

Doing a quick github search, I'm suspicious of puppetlabs/puppetlabs-registry@3f8ae6e (but don't currently work on Windows so can't really do any testing)

Also is https://github.com/puppetlabs/puppetlabs-apt/blob/d172e8588c88c3e2ad85ad4be8ade1c603d23e6d/lib/puppet/type/apt_key.rb#L75 a bug? Does that need to be a symbol?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Sorry I've conflated a few different things here.

For parameters

If you do defaultto false, then the value (as seen by the provider) will be nil due to https://puppet.atlassian.net/browse/PUP-1967. This may or may not be a problem, depending on whether the provider treats nil and false the same. For example, the file :backup parameter defaults to false, but the provider only cares if the value is truthy, so it's not an issue
https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/type/file.rb#L127

However, It would be a problem if the provider did:

if resource[:backup] == false

Second, the recommended way to create a boolean parameter is to do

newparam(:force, :boolean => true, :parent => Puppet::Parameter::Boolean) do

The boolean option is recommended for consistency and adds a predicate method to Puppet::Type::File#force?. See https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/type/file.rb#L1022

The parent option is recommended so that all parameters handle the same values. By default, this is:

irb(main):014:0> Puppet::Type.type(:file).paramclass(:force).value_collection.values
=> [:true, :false, :yes, :no]

If a parameter does newvalues(:true, :false) then it restricts the values to just those two:

irb(main):002:0> Puppet::Type.type(:user).paramclass(:purge_ssh_keys).value_collection.values
=> [:true, :false]

For Properties

There's a separate but related issue that if you define a boolean property with newvalues(true, false) then puppet will ignore the property if its desired value is false. See https://puppet.atlassian.net/browse/PUP-2368

Conclusion

To preserve the original behavior in java_ks, I would add back newvalues(:true, :false) and defaultto :false

The registry change may be ok, but we'd need to audit the entire provider to see if it handles nil and false the same. I'd recommend reverting the change.

The apt_key default may be ok, as long as the provider handles nil and false the same.

We really need to fix boolean parameter and property handling because this is insane.

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.

I might be inclined to leave newvalues(:true, :false) out and let Puppet::Parameter::Boolean handle what the allowed values are even if that does mean the type technically allows a bit more than it did before (ie :yes and :no). The fewer custom lines of code in each type, the better.

Then maybe as part of the language steering committee consider deprecating yes and no for all types using Puppet::Parameter::Boolean??

and Puppet::Property::Boolean should either be deprecated or fixed (if possible). https://puppet.atlassian.net/browse/PUP-2368 makes it unusable IMO.

The unsafe rubocop fixes done in
189ac29 broke the boolean parameters.

Before this commit, if you explicitly tried to set either of them to
`false`, they would actually be set to the symbol `:false` with is not
falsey so would act as if you'd set them to `true`.
@alexjfisher alexjfisher requested a review from joshcooper March 12, 2025 09:58
@amitkarsale amitkarsale merged commit ff69e23 into puppetlabs:main Mar 17, 2025
52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants