Skip to content

Commit 10d219a

Browse files
committed
(PUP-11788) Account for JRuby behavior in Dir.glob
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)
1 parent d835ef6 commit 10d219a

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

lib/puppet/node/environment.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -591,10 +591,12 @@ def perform_initial_import
591591
if file == NO_MANIFEST
592592
empty_parse_result
593593
elsif File.directory?(file)
594-
parse_results = Puppet::FileSystem::PathPattern.absolute(File.join(file, '**/*.pp')).glob.sort.map do | file_to_parse |
595-
parser.file = file_to_parse
596-
parser.parse
597-
end
594+
# JRuby does not properly perform Dir.glob operations with wildcards, (see PUP-11788 and https://github.com/jruby/jruby/issues/7836).
595+
# We sort the results because Dir.glob order is inconsistent in Ruby < 3 (see PUP-10115).
596+
parse_results = Puppet::FileSystem::PathPattern.absolute(File.join(file, '**/*')).glob.select {|globbed_file| globbed_file.end_with?('.pp')}.sort.map do | file_to_parse |
597+
parser.file = file_to_parse
598+
parser.parse
599+
end
598600
# Use a parser type specific merger to concatenate the results
599601
Puppet::Parser::AST::Hostclass.new('', :code => Puppet::Parser::ParserFactory.code_merger.concatenate(parse_results))
600602
else

spec/unit/file_system/path_pattern_spec.rb

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'spec_helper'
22
require 'puppet_spec/files'
33
require 'puppet/file_system'
4+
require 'puppet/util'
45

56
describe Puppet::FileSystem::PathPattern do
67
include PuppetSpec::Files
@@ -132,6 +133,20 @@
132133
File.join(dir, "found_two")])
133134
end
134135

136+
it 'globs wildcard patterns properly' do
137+
# See PUP-11788 and https://github.com/jruby/jruby/issues/7836.
138+
pending 'JRuby does not properly handle Dir.glob' if Puppet::Util::Platform.jruby?
139+
140+
dir = tmpdir('globtest')
141+
create_file_in(dir, 'foo.pp')
142+
create_file_in(dir, 'foo.pp.pp')
143+
144+
pattern = Puppet::FileSystem::PathPattern.absolute(File.join(dir, '**/*.pp'))
145+
146+
expect(pattern.glob).to match_array([File.join(dir, 'foo.pp'),
147+
File.join(dir, 'foo.pp.pp')])
148+
end
149+
135150
def create_file_in(dir, name)
136151
File.open(File.join(dir, name), "w") { |f| f.puts "data" }
137152
end

0 commit comments

Comments
 (0)