Skip to content

Commit c87471d

Browse files
author
jordanbreen28
committed
(CAT-1493) - Fix missing file resource type parameters
Prior to this commit, when using the autocompletion feature of puppet-editor-services, the file resource type would be missing some parameters/properties in the dropdown completion list. It was found that this was due to the way the file resource type was structured in the puppet source code. The type defintion can be found in both /lib/puppet/type and /lib/puppet/type/file/. The way we can work around this is by altering the search glob, and combining the type defintions into one single file. This file is then deleted on completion.
1 parent d42b6b4 commit c87471d

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

lib/puppet-languageserver-sidecar/puppet_helper.rb

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def self.retrieve_via_puppet_strings(cache, options = {})
9292
paths = finder.find(options[:root_path])
9393

9494
paths.each do |path|
95-
file_doc = PuppetLanguageServerSidecar::PuppetStringsHelper.file_documentation(path, cache)
95+
file_doc = PuppetLanguageServerSidecar::PuppetStringsHelper.file_documentation(path, finder.puppet_path, cache)
9696
next if file_doc.nil?
9797

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

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

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

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

214215
Dir.glob(glob) do |filename|
215-
paths << filename
216+
# name of temp file to store the file type definitions (if any)
217+
@temp_file = 'temp_file.rb'
218+
# if filename matches file.rb or /file/<any>.rb then we need to loop through each file type definition
219+
if filename.match?(%r{/type/file/.*.rb|/type/file.rb})
220+
# Create/Open the temp file and write the file type definitions to it
221+
File.open(@temp_file, 'a') do |f|
222+
# Read each file type definition and write it to the temp file
223+
PuppetLanguageServerSidecar.log_message(:debug, "[PuppetPathFinder] Found file type definition at '#{filename}'.")
224+
f.puts(File.read(filename))
225+
end
226+
else
227+
paths << filename
228+
end
216229
end
217230
end
218231
end
219232
end
220-
233+
#  Add the temp_file.rb to the paths array for searching (if exists)
234+
paths << @temp_file if @temp_file && File.exist?(@temp_file)
221235
paths
222236
end
223237

@@ -258,7 +272,7 @@ def all_object_info
258272
{ relative_dir: 'lib/puppet/parser/functions', glob: '/**/*.rb' } # Contains functions written in Ruby for the legacy Puppet::Parser::Functions API
259273
],
260274
type: [
261-
{ relative_dir: 'lib/puppet/type', glob: '/*.rb' } # Contains Puppet resource types. We don't care about providers. Types cannot exist in subdirs
275+
{ relative_dir: 'lib/puppet/type', glob: '/{,file/}*.rb' } # Contains Puppet resource types. Resource types like `file` can live in subdirs, hence the glob
262276
]
263277
}
264278
end

lib/puppet-languageserver-sidecar/puppet_strings_helper.rb

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ def self.instance
66
@instance ||= Helper.new
77
end
88

9-
def self.file_documentation(path, cache = nil)
10-
instance.file_documentation(path, cache)
9+
def self.file_documentation(path, puppet_path, cache = nil)
10+
instance.file_documentation(path, puppet_path, cache)
1111
end
1212

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

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

7575
# Populate the documentation cache from the YARD information
76-
@helper_cache.populate_from_yard_registry!
76+
@helper_cache.populate_from_yard_registry!(puppet_path)
7777

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

101-
def populate_from_yard_registry!
101+
def populate_from_yard_registry!(puppet_path)
102102
# Extract all of the information
103103
# Ref - https://github.com/puppetlabs/puppet-strings/blob/87a8e10f45bfeb7b6b8e766324bfb126de59f791/lib/puppet-strings/json.rb#L10-L16
104104
populate_classes_from_yard_registry!
105105
populate_data_types_from_yard_registry!
106106
populate_functions_from_yard_registry!
107-
populate_types_from_yard_registry!
107+
populate_types_from_yard_registry!(puppet_path)
108108
end
109109

110110
def populate_from_sidecar_cache!(path, cache)
@@ -263,10 +263,10 @@ def populate_functions_from_yard_registry!
263263
end
264264
end
265265

266-
def populate_types_from_yard_registry!
266+
def populate_types_from_yard_registry!(puppet_path)
267267
::YARD::Registry.all(:puppet_type).map(&:to_hash).each do |item|
268-
source_path = item[:file]
269268
type_name = item[:name].to_s
269+
source_path = item[:file]
270270
@cache[source_path] = FileDocumentation.new(source_path) if @cache[source_path].nil?
271271

272272
obj = PuppetLanguageServer::Sidecar::Protocol::PuppetType.new
@@ -295,6 +295,13 @@ def populate_types_from_yard_registry!
295295
end
296296
end
297297

298+
if obj.key == 'file'
299+
# Special case for file type
300+
# we need to set the source and calling_source to the correct file definition
301+
path = File.join(puppet_path, 'lib/puppet/type')
302+
obj.source = "#{path}/#{obj.key}.rb"
303+
obj.calling_source = obj.source
304+
end
298305
@cache[source_path].types << obj
299306
end
300307
end

0 commit comments

Comments
 (0)