Skip to content

Commit

Permalink
(GH-46) Detect Puppet Environment correctly
Browse files Browse the repository at this point in the history
Previously the puppet environment detection normally defaulted to root however
this did not work correctly when the user running the language-server was
non-root.  This commit instead updates the environment detection to correctly
find the puppet environment, and defaults to the previous behaviour if the new
method failed to work.
  • Loading branch information
glennsarti committed Jul 19, 2018
1 parent e768efc commit 6eb083b
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/puppet-languageserver/puppet_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ def self.prune_resource_parameters(resources)
end
private_class_method :prune_resource_parameters

def self.current_environment
begin
env = Puppet.lookup(:environments).get!(Puppet.settings[:environment])
return env unless env.nil?
rescue Puppet::Environments::EnvironmentNotFound
PuppetLanguageServer.log_message(:warning, "[PuppetHelper::current_environment] Unable to load environment #{Puppet.settings[:environment]}")
rescue StandardError => ex
PuppetLanguageServer.log_message(:warning, "[PuppetHelper::current_environment] Error loading environment #{Puppet.settings[:environment]}: #{ex}")
end
Puppet.lookup(:current_environment)
end
private_class_method :current_environment

# Class and Defined Type loading
def self._load_default_classes
@default_classes_loaded = false
Expand Down Expand Up @@ -325,7 +338,7 @@ def self._load_default_types

# From https://github.com/puppetlabs/puppet/blob/ebd96213cab43bb2a8071b7ac0206c3ed0be8e58/lib/puppet/metatype/manager.rb#L182-L189
autoloader = Puppet::Util::Autoload.new(self, 'puppet/type')
current_env = Puppet.lookup(:current_environment)
current_env = current_environment
type_count = 0

# This is an expensive call
Expand Down Expand Up @@ -389,7 +402,7 @@ def self._load_default_functions
PuppetLanguageServer.log_message(:debug, '[PuppetHelper::_load_default_functions] Starting')

autoloader = Puppet::Parser::Functions.autoloader
current_env = Puppet.lookup(:current_environment)
current_env = current_environment
function_count = 0

# Functions that are already loaded (e.g. system default functions like alert)
Expand Down

0 comments on commit 6eb083b

Please sign in to comment.