Skip to content

Commit

Permalink
(PUP-11788) Account for JRuby behavior in Dir.glob
Browse files Browse the repository at this point in the history
JRuby does not properly match both directory and file wildcards when
using Dir.glob (e.g. Dir.glob('**/*.pp'), which can lead to incorrect
matches. Because of this, if a manifest name contains ".pp" in its
filename (e.g. foo.pp.pp), Puppet does not recognize it as a manifest
file when importing and parsing manifests.

This commit updates the perform_initial_import method to no longer rely
on that wildcard Dir.glob behavior to recursively traverse a directory
and instead uses only the directory wildcard and uses Array.select
to identify files ending in ".pp" from there.

(cherry picked from commit 663062a)
  • Loading branch information
mhashizume committed Oct 12, 2023
1 parent d835ef6 commit 10d219a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/puppet/node/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -591,10 +591,12 @@ def perform_initial_import
if file == NO_MANIFEST
empty_parse_result
elsif File.directory?(file)
parse_results = Puppet::FileSystem::PathPattern.absolute(File.join(file, '**/*.pp')).glob.sort.map do | file_to_parse |
parser.file = file_to_parse
parser.parse
end
# JRuby does not properly perform Dir.glob operations with wildcards, (see PUP-11788 and https://github.com/jruby/jruby/issues/7836).
# We sort the results because Dir.glob order is inconsistent in Ruby < 3 (see PUP-10115).
parse_results = Puppet::FileSystem::PathPattern.absolute(File.join(file, '**/*')).glob.select {|globbed_file| globbed_file.end_with?('.pp')}.sort.map do | file_to_parse |
parser.file = file_to_parse
parser.parse
end
# Use a parser type specific merger to concatenate the results
Puppet::Parser::AST::Hostclass.new('', :code => Puppet::Parser::ParserFactory.code_merger.concatenate(parse_results))
else
Expand Down
15 changes: 15 additions & 0 deletions spec/unit/file_system/path_pattern_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'spec_helper'
require 'puppet_spec/files'
require 'puppet/file_system'
require 'puppet/util'

describe Puppet::FileSystem::PathPattern do
include PuppetSpec::Files
Expand Down Expand Up @@ -132,6 +133,20 @@
File.join(dir, "found_two")])
end

it 'globs wildcard patterns properly' do
# See PUP-11788 and https://github.com/jruby/jruby/issues/7836.
pending 'JRuby does not properly handle Dir.glob' if Puppet::Util::Platform.jruby?

dir = tmpdir('globtest')
create_file_in(dir, 'foo.pp')
create_file_in(dir, 'foo.pp.pp')

pattern = Puppet::FileSystem::PathPattern.absolute(File.join(dir, '**/*.pp'))

expect(pattern.glob).to match_array([File.join(dir, 'foo.pp'),
File.join(dir, 'foo.pp.pp')])
end

def create_file_in(dir, name)
File.open(File.join(dir, name), "w") { |f| f.puts "data" }
end
Expand Down

0 comments on commit 10d219a

Please sign in to comment.