Skip to content

(PE-20308) Pass a literal type and not a string to findresource #761

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

Merged
merged 1 commit into from
Apr 26, 2017

Conversation

hunner
Copy link
Contributor

@hunner hunner commented Apr 25, 2017

Suggested workaround is to pass a literal type instead of a string to
findresource to fix in stdlib, and also to fix loading/requiring of
type in core puppet.

@hunner hunner requested a review from thallgren April 25, 2017 19:12

# Workaround for PE-20308
if reference.is_a?(String)
type_name, title = Puppet::Resource.type_and_title(reference, nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

The Puppet::Resource#type_and_title class method was introduced in Puppet 4.5.0. Perhaps that's what causing appveyor to fail for 4.2.3?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll just fallback to the old behavior pre 4.5.0 as the ticket only reports the behavior on newer puppet versions.

if reference.is_a?(String)
type_name, title = Puppet::Resource.type_and_title(reference, nil)
elsif reference.is_a?(Puppet::Resource)
type_name, title = Puppet::Resource.type_and_title(reference.type, reference.title)
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it not be more efficient to just ask the resource for it's type here?

type = reference.resource_type

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had been looking for something like that originally, but overlooked it. Thanks!

@hunner hunner force-pushed the fix_type_loading branch 2 times, most recently from 6d1662c to 746ddbb Compare April 26, 2017 00:00
- `defined_with_params` calls `findresource(reference.to_s)`
- `findresource` is
  https://github.com/puppetlabs/puppet/blob/4.8.1/lib/puppet/parser/compiler.rb#L407
  and points to
  https://github.com/puppetlabs/puppet/blob/4.8.1/lib/puppet/resource/catalog.rb#L352
- This calls `Puppet::Resource.new` with the type
  https://github.com/puppetlabs/puppet/blob/4.8.1/lib/puppet/resource/catalog.rb#L366
- This ends up calling `resource_type` via
  https://github.com/puppetlabs/puppet/blob/4.8.1/lib/puppet/resource.rb#L317-L319
  and that ends up declaring the type via the autoloader api at
  https://github.com/puppetlabs/puppet/blob/4.8.1/lib/puppet/resource.rb#L390
- But why does the autoloader API fail to find it in the current
  environment?
- Okay, so when the autoloader is trying to find the type, it uses the
  typeloader to look it up in the current environment
  https://github.com/puppetlabs/puppet/blob/4.8.1/lib/puppet/metatype/manager.rb#L171
- this calls `get_file` and `mark_loaded`
  https://github.com/puppetlabs/puppet/blob/4.8.1/lib/puppet/util/autoload.rb#L64-L67

Suggested workaround is to pass a literal type instead of a string to
`findresource` to fix in stdlib, and also to fix loading/requiring of
type in core puppet.

This seems to affect more recent versions of puppet, so fallback to
original behavior on pre-4.5
@hunner hunner force-pushed the fix_type_loading branch from 746ddbb to 4f19c27 Compare April 26, 2017 00:12
@thallgren thallgren merged commit 047fe6d into puppetlabs:master Apr 26, 2017
@hunner hunner deleted the fix_type_loading branch April 26, 2017 16:00
@domcleal
Copy link
Contributor

This change doesn't work with defined types, only native types - I filed https://tickets.puppetlabs.com/browse/MODULES-4790 (tests). Unfortunately this affects ensure_resource, causing duplicate resources and catalog build failures.

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.

5 participants