Skip to content

Render the creating resource when a path has several resources - #49

Open
tas50 wants to merge 1 commit into
chef:mainfrom
tas50:fix/render-file-resource-selection
Open

Render the creating resource when a path has several resources#49
tas50 wants to merge 1 commit into
chef:mainfrom
tas50:fix/render-file-resource-selection

Conversation

@tas50

@tas50 tas50 commented Aug 22, 2026

Copy link
Copy Markdown

Fixes chefspec#858

Problem

render_file resolved the resource for a path like this:

@resource ||= @runner.find_resource(:cookbook_file, @path) ||
  @runner.find_resource(:file, @path) ||
  @runner.find_resource(:template, @path)

It stops at the first match and never considers the action. A recipe that renders a config file with a template and removes it with a guarded file resource declares two resources for the same path, so the file[:delete] resource wins and render_file reports the file as unrendered.

The confusing part is that create_template matches correctly at the same time, which makes this look like a rendering bug rather than a lookup bug.

template '/tmp/thing' do
  source 'thing.erb'
end

file '/tmp/thing' do
  action :delete
end
expected Chef run to render "/tmp/thing"

Fix

Collect all candidate resources for the path and prefer whichever one performs a create-like action, falling back to the first match so failure messages are unchanged when nothing creates the file.

Testing

Adds a template plus file[:delete] pair for the same path to the render_file acceptance example.

  • With the fix: 55 examples, 0 failures
  • With the fix reverted: 55 examples, 2 failures
  • Unit suite: 197 examples, 0 failures
  • All acceptance examples pass

@tas50
tas50 requested review from a team and jaymzh as code owners August 22, 2026 00:07
The render_file matcher looked up the resource for a path by trying
cookbook_file, then file, then template, and stopped at the first match
regardless of the action that resource performed.

A recipe that renders a config file with a template and removes it with a
guarded file resource therefore matched the file[:delete] resource and
reported the file as unrendered, even though create_template matched and
the template really was rendered.

Prefer whichever candidate resource actually performs a create-like
action and fall back to the first match so failure messages are unchanged
when nothing creates the file.

Fixes chefspec#858

Signed-off-by: Tim Smith <tsmith84@proton.me>
@tas50
tas50 force-pushed the fix/render-file-resource-selection branch from ce335f4 to 1bb0f9b Compare August 22, 2026 00:09
@tas50

tas50 commented Aug 22, 2026

Copy link
Copy Markdown
Author

CI on this PR is red for an unrelated reason: main's bundle cannot be installed on Linux, which breaks every job before any test runs. That is fixed separately in #56, which is green across Ruby 3.1 to 3.4. This PR should go green once #56 lands and this branch is rebased.

Locally, against a working bundle, this branch passes rake unit and the acceptance examples.

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.

render_file will select delete_file resources out of the resource collection and ignore template_create resources that would otherwise match

1 participant