Skip to content

Commit 54be85e

Browse files
authored
Merge pull request #101 from glennsarti/fix-function-loading
(GH-98) Fix function and type loading initial fix
2 parents 9305fcb + da5f13d commit 54be85e

File tree

13 files changed

+154
-14
lines changed

13 files changed

+154
-14
lines changed

server/lib/puppet-languageserver.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,7 @@ def self.init_puppet(options)
118118
end
119119

120120
def self.init_puppet_worker(options)
121-
Puppet.initialize_settings
122-
123-
log_message(:info, 'Creating puppet function environment...')
124-
autoloader = Puppet::Parser::Functions.autoloader
125-
autoloader.loadall
121+
options[:puppet_settings].nil? ? Puppet.initialize_settings : Puppet.initialize_settings(options[:puppet_settings])
126122

127123
log_message(:info, "Using Facter v#{Facter.version}")
128124
if options[:preload_puppet]

server/lib/puppet-languageserver/facter_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def self._load_facts
3939
_reset
4040
Facter.loadfacts
4141
@fact_hash = Facter.to_hash
42+
PuppetLanguageServer.log_message(:debug, "[FacterHelper::_load_facts] Finished loading #{@fact_hash.keys.count} facts")
4243
end
4344
private_class_method :_load_facts
4445
end

server/lib/puppet-languageserver/puppet_helper.rb

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ module PuppetHelper
1010
@function_module = nil
1111

1212
def self.reset
13-
@ops_lock.synchronize do
14-
_reset
13+
@ops_lock_types.synchronize do
14+
@ops_lock_funcs.synchronize do
15+
_reset
16+
end
1517
end
1618
end
1719

@@ -60,7 +62,7 @@ def self.type_names
6062
# Functions
6163
def self.load_functions
6264
@ops_lock_funcs.synchronize do
63-
_load_functions
65+
_load_functions if @function_module.nil?
6466
end
6567
end
6668

@@ -119,8 +121,17 @@ def self._load_types
119121
@types_hash = {}
120122
# This is an expensive call
121123
# From https://github.com/puppetlabs/puppet/blob/ebd96213cab43bb2a8071b7ac0206c3ed0be8e58/lib/puppet/metatype/manager.rb#L182-L189
122-
typeloader = Puppet::Util::Autoload.new(self, 'puppet/type')
123-
typeloader.loadall
124+
125+
autoloader = Puppet::Util::Autoload.new(self, 'puppet/type')
126+
autoloader.files_to_load.each do |file|
127+
name = file.gsub(autoloader.path + '/','')
128+
begin
129+
result = autoloader.load(name)
130+
PuppetLanguageServer.log_message(:error, "[PuppetHelper::_load_types] type #{file} did not load") unless result
131+
rescue StandardError => err
132+
PuppetLanguageServer.log_message(:error, "[PuppetHelper::_load_types] Error loading type #{file}: #{err}")
133+
end unless autoloader.loaded?(name)
134+
end
124135

125136
Puppet::Type.eachtype do |type|
126137
next if type.name == :component
@@ -129,16 +140,31 @@ def self._load_types
129140
@types_hash[type.name] = type
130141
end
131142

143+
type_count = @types_hash.count
144+
PuppetLanguageServer.log_message(:debug, "[PuppetHelper::_load_types] Finished loading #{type_count} types")
145+
132146
nil
133147
end
134148
private_class_method :_load_types
135149

136150
def self._load_functions
137151
autoloader = Puppet::Parser::Functions.autoloader
138-
autoloader.loadall
152+
153+
# This is an expensive call
154+
autoloader.files_to_load.each do |file|
155+
name = file.gsub(autoloader.path + '/','')
156+
begin
157+
result = autoloader.load(name)
158+
PuppetLanguageServer.log_message(:error, "[PuppetHelper::_load_functions] function #{file} did not load") unless result
159+
rescue StandardError => err
160+
PuppetLanguageServer.log_message(:error, "[PuppetHelper::_load_functions] Error loading function #{file}: #{err}")
161+
end unless autoloader.loaded?(name)
162+
end
139163

140164
@function_module = Puppet::Parser::Functions.environment_module(Puppet.lookup(:current_environment))
141165

166+
function_count = @function_module.all_function_info.keys.map(&:to_s).count
167+
PuppetLanguageServer.log_message(:debug, "[PuppetHelper::_load_functions] Finished loading #{function_count} functions")
142168
nil
143169
end
144170
private_class_method :_load_functions

server/spec/fixtures/cache/facts.d/.gitkeep

Whitespace-only changes.

server/spec/fixtures/cache/lib/facter/.gitkeep

Whitespace-only changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require 'a_bad_gem_that_does_not_exist'
2+
3+
module Puppet::Parser::Functions
4+
newfunction(:bad_file, :type => :rvalue, :doc => <<-EOS
5+
Function that will fail loading
6+
EOS
7+
) do |arguments|
8+
# Do nothing
9+
end
10+
end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Puppet::Parser::Functions
2+
newfunction(:bad_function, :type => :rvalue, :doc => <<-EOS
3+
Function that will fail loading
4+
EOS
5+
) do |arguments|
6+
7+
require 'a_bad_gem_that_does_not_exist'
8+
end
9+
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#
2+
# concat.rb
3+
#
4+
5+
module Puppet::Parser::Functions
6+
newfunction(:concat, :type => :rvalue, :doc => <<-EOS
7+
Appends the contents of multiple arrays into array 1.
8+
9+
*Example:*
10+
11+
concat(['1','2','3'],['4','5','6'],['7','8','9'])
12+
13+
Would result in:
14+
15+
['1','2','3','4','5','6','7','8','9']
16+
EOS
17+
) do |arguments|
18+
19+
# Check that more than 2 arguments have been given ...
20+
raise(Puppet::ParseError, "concat(): Wrong number of arguments " +
21+
"given (#{arguments.size} for < 2)") if arguments.size < 2
22+
23+
a = arguments[0]
24+
25+
# Check that the first parameter is an array
26+
unless a.is_a?(Array)
27+
raise(Puppet::ParseError, 'concat(): Requires array to work with')
28+
end
29+
30+
result = a
31+
arguments.shift
32+
33+
arguments.each do |x|
34+
result = result + (x.is_a?(Array) ? x : [x])
35+
end
36+
37+
return result
38+
end
39+
end
40+
41+
# vim: set ts=2 sw=2 et :
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Text fixture puppet.conf which redirects all Puppet configuration into the
2+
# fixtures directory
3+
[main]
4+
environment = test-fixtures
5+
environmentpath = $codedir/environments
6+
codedir = $vardir/..
7+
logdir = $vardir/../logdir

server/spec/fixtures/environments/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)