Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(CAT-1493) - Fix missing file resource type parameters #353

Merged
merged 2 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ task :gem_revendor do
},
{
:directory => 'puppetfile-resolver',
:github_repo => 'https://github.com/jordanbreen28/puppetfile-resolver.git',
:github_ref => 'd058f6b8b285dba2af0aeb59722ea5de23c3c13f',
:github_repo => 'https://github.com/puppetlabs/puppetfile-resolver.git',
:github_ref => 'v0.6.3',
},
{
:directory => 'molinillo',
Expand Down
24 changes: 19 additions & 5 deletions lib/puppet-languageserver-sidecar/puppet_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def self.retrieve_via_puppet_strings(cache, options = {})
paths = finder.find(options[:root_path])

paths.each do |path|
file_doc = PuppetLanguageServerSidecar::PuppetStringsHelper.file_documentation(path, cache)
file_doc = PuppetLanguageServerSidecar::PuppetStringsHelper.file_documentation(path, finder.puppet_path, cache)
next if file_doc.nil?

if object_types.include?(:class) # rubocop:disable Style/IfUnlessModifier This reads better
Expand All @@ -108,6 +108,7 @@ def self.retrieve_via_puppet_strings(cache, options = {})

file_doc.types.each do |item|
result.append!(item) unless name == 'whit' || name == 'component'
FileUtils.rm_f(finder.temp_file) if item.key == 'file' && finder.temp_file # Remove the temp_file.rb if it exists
end
end

Expand Down Expand Up @@ -152,7 +153,7 @@ def self.current_environment
# A helper class to find the paths for different kinds of things related to Puppet, for example
# DataType ruby files or manifests.
class PuppetPathFinder
attr_reader :object_types
attr_reader :object_types, :puppet_path, :temp_file

# @param puppet_env [Puppet::Node::Environment] The environment to search within
# @param object_types [Symbol] The types of objects that will be searched for. See available_documentation_types for the complete list
Expand Down Expand Up @@ -212,12 +213,25 @@ def find(from_root_path = nil)
PuppetLanguageServerSidecar.log_message(:debug, "[PuppetPathFinder] Searching glob '#{glob}''")

Dir.glob(glob) do |filename|
paths << filename
# name of temp file to store the file type definitions (if any)
@temp_file = 'temp_file.rb'
# if filename matches file.rb or /file/<any>.rb then we need to loop through each file type definition
if filename.match?(%r{/type/file/.*.rb|/type/file.rb})
# Create/Open the temp file and write the file type definitions to it
File.open(@temp_file, 'a') do |f|
# Read each file type definition and write it to the temp file
PuppetLanguageServerSidecar.log_message(:debug, "[PuppetPathFinder] Found file type definition at '#{filename}'.")
f.puts(File.read(filename))
end
else
paths << filename
end
end
end
end
end

#  Add the temp_file.rb to the paths array for searching (if exists)
paths << @temp_file if @temp_file && File.exist?(@temp_file)
paths
end

Expand Down Expand Up @@ -258,7 +272,7 @@ def all_object_info
{ relative_dir: 'lib/puppet/parser/functions', glob: '/**/*.rb' } # Contains functions written in Ruby for the legacy Puppet::Parser::Functions API
],
type: [
{ relative_dir: 'lib/puppet/type', glob: '/*.rb' } # Contains Puppet resource types. We don't care about providers. Types cannot exist in subdirs
{ relative_dir: 'lib/puppet/type', glob: '/{,file/}*.rb' } # Contains Puppet resource types. Resource types like `file` can live in subdirs, hence the glob
]
}
end
Expand Down
23 changes: 15 additions & 8 deletions lib/puppet-languageserver-sidecar/puppet_strings_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ def self.instance
@instance ||= Helper.new
end

def self.file_documentation(path, cache = nil)
instance.file_documentation(path, cache)
def self.file_documentation(path, puppet_path, cache = nil)
instance.file_documentation(path, puppet_path, cache)
end

def self.require_puppet_strings
Expand Down Expand Up @@ -40,7 +40,7 @@ class Helper
# @param [String] path The absolute path to the file that will be documented
# @param [PuppetLanguageServerSidecar::Cache] cache A Sidecar cache which stores already parsed documents as serialised FileDocumentation objects
# @return [FileDocumentation, nil] Returns the documentation for the path, or nil if it cannot be extracted
def file_documentation(path, cache = nil)
def file_documentation(path, puppet_path, cache = nil)
return nil unless PuppetLanguageServerSidecar::PuppetStringsHelper.require_puppet_strings

@helper_cache = FileDocumentationCache.new if @helper_cache.nil?
Expand Down Expand Up @@ -73,7 +73,7 @@ def file_documentation(path, cache = nil)
::YARD::CLI::Yardoc.run(*args)

# Populate the documentation cache from the YARD information
@helper_cache.populate_from_yard_registry!
@helper_cache.populate_from_yard_registry!(puppet_path)

# Save to the permanent cache
@helper_cache.save_to_sidecar_cache(path, cache) unless cache.nil? || !cache.active?
Expand All @@ -98,13 +98,13 @@ def document(path)
@cache[path]
end

def populate_from_yard_registry!
def populate_from_yard_registry!(puppet_path)
# Extract all of the information
# Ref - https://github.com/puppetlabs/puppet-strings/blob/87a8e10f45bfeb7b6b8e766324bfb126de59f791/lib/puppet-strings/json.rb#L10-L16
populate_classes_from_yard_registry!
populate_data_types_from_yard_registry!
populate_functions_from_yard_registry!
populate_types_from_yard_registry!
populate_types_from_yard_registry!(puppet_path)
end

def populate_from_sidecar_cache!(path, cache)
Expand Down Expand Up @@ -263,10 +263,10 @@ def populate_functions_from_yard_registry!
end
end

def populate_types_from_yard_registry!
def populate_types_from_yard_registry!(puppet_path)
::YARD::Registry.all(:puppet_type).map(&:to_hash).each do |item|
source_path = item[:file]
type_name = item[:name].to_s
source_path = item[:file]
@cache[source_path] = FileDocumentation.new(source_path) if @cache[source_path].nil?

obj = PuppetLanguageServer::Sidecar::Protocol::PuppetType.new
Expand Down Expand Up @@ -295,6 +295,13 @@ def populate_types_from_yard_registry!
end
end

if obj.key == 'file'
# Special case for file type
# we need to set the source and calling_source to the correct file definition
path = File.join(puppet_path, 'lib/puppet/type')
obj.source = "#{path}/#{obj.key}.rb"
obj.calling_source = obj.source
end
@cache[source_path].types << obj
end
end
Expand Down