Skip to content

Commit

Permalink
Merge pull request #359 from puppetlabs/maint-fix_nightlies
Browse files Browse the repository at this point in the history
(maint) - Write file type definitions to tempfile
  • Loading branch information
gavindidrichsen authored Nov 27, 2023
2 parents ea9961f + 88238e1 commit 87e267d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
19 changes: 19 additions & 0 deletions docs/architecture/decisions/0001-aggregate-file-definitions.md
Original file line number Diff line number Diff line change
@@ -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.
22 changes: 11 additions & 11 deletions lib/puppet-languageserver-sidecar/puppet_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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?
Expand All @@ -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)

Expand All @@ -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/<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
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
Expand All @@ -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

Expand Down

0 comments on commit 87e267d

Please sign in to comment.