diff --git a/docs/architecture/decisions/0001-aggregate-file-definitions.md b/docs/architecture/decisions/0001-aggregate-file-definitions.md new file mode 100644 index 00000000..ed804545 --- /dev/null +++ b/docs/architecture/decisions/0001-aggregate-file-definitions.md @@ -0,0 +1,19 @@ +# 1. Aggregation of file resource type definitions + +Date: 2023-11-27 + +## Status + +Accepted + +## Context + +In [this issue](https://github.com/puppetlabs/puppet-editor-services/issues/349) raised on the `puppet-editor-services` repo, it was noted that the file resource type had multiple missing parameters in the dropdown autocompletion list. After some investigation, it was discovered that the parameters were missing due to the way the file resource type is defined in the puppet source code, with having multiple definitions in separate files. puppet-editor-services was only designed to collect the parameters in the initial definition found in `lib/puppet/type/file.rb`, and collected all parameters from this type declaration as you would expect. However, it would ignore all other parameters which were defined in the files `lib/puppet/type/file/*.rb`, and thus exlcuding them from the autocompletion list. (see [here](https://github.com/puppetlabs/puppet/tree/main/lib/puppet/type/file)). + +## Decision + +A decision was taken in [this pr](https://github.com/puppetlabs/puppet-editor-services/pull/353) (later updated to write to a tempfile [here](https://github.com/puppetlabs/puppet-editor-services/pull/359)) to aggregate all seperate file type defintions and write these to a single file, this could then be used as a single point of reference for the language server. This allowed puppet-editor-services to collect all parameters of the file type as expected. + +## Consequences + +* `Go To Definition` will direct the user to the initial file type declaration at `/lib/puppet/type/file.rb`, not to the other type definitions. diff --git a/lib/puppet-languageserver-sidecar/puppet_helper.rb b/lib/puppet-languageserver-sidecar/puppet_helper.rb index 18029356..8adcb891 100644 --- a/lib/puppet-languageserver-sidecar/puppet_helper.rb +++ b/lib/puppet-languageserver-sidecar/puppet_helper.rb @@ -108,7 +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 + finder.temp_file.unlink if item.key == 'file' && File.exist?(finder.temp_file.path) # Remove the temp_file.rb if it exists end end @@ -182,6 +182,7 @@ def initialize(puppet_env, object_types) # @param from_root_path [String] The path which files can be found within. If nil, only the default Puppet locations are searched e.g. vardir # @return [Array[String]] A list of all files that are found. This is the absolute path to the file. def find(from_root_path = nil) + require 'tempfile' paths = [] search_paths = @module_paths.nil? ? [] : @module_paths search_paths << @env_path unless @env_path.nil? @@ -198,7 +199,8 @@ def find(from_root_path = nil) next unless path_in_root?(from_root_path, search_root) && Dir.exist?(search_root) PuppetLanguageServerSidecar.log_message(:debug, "[PuppetPathFinder] Using '#{search_root}' as a directory to search") - + # name of temp file to store the file type definitions (if any) + @temp_file = Tempfile.new('file.rb') all_object_info.each do |object_type, paths_to_search| next unless object_types.include?(object_type) @@ -213,16 +215,11 @@ def find(from_root_path = nil) PuppetLanguageServerSidecar.log_message(:debug, "[PuppetPathFinder] Searching glob '#{glob}''") Dir.glob(glob) do |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/.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 + PuppetLanguageServerSidecar.log_message(:debug, "[PuppetPathFinder] Found file type definition at '#{filename}'.") + # Read each file type definition and write it to the temp file + @temp_file.write(File.read(filename)) else paths << filename end @@ -231,7 +228,10 @@ def find(from_root_path = nil) 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) + if @temp_file && File.exist?(@temp_file.path) + paths << @temp_file.path + @temp_file.close + end paths end