Skip to content

Commit ad09d1b

Browse files
committed
(GH-98) Fix Type and Function loading
Previously type and function loading would prematurely fail and stop the language server if a type or function load threw an error. This appears to be a facet of the Puppet loadall function. This commit changes the loading to mimic the loadall function but instead, ignore any loading errors and continue. This commit also adds some debug messages after types and function have completed loading which is useful during debugging. This commit also fixes function loader which was not idepmotent and was causing functions to be loaded mulitple times. This commit also fixes a typo in the reset function.
1 parent 7898e9e commit ad09d1b

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

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

0 commit comments

Comments
 (0)