Skip to content

Commit 87e267d

Browse files
Merge pull request #359 from puppetlabs/maint-fix_nightlies
(maint) - Write file type definitions to tempfile
2 parents ea9961f + 88238e1 commit 87e267d

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 1. Aggregation of file resource type definitions
2+
3+
Date: 2023-11-27
4+
5+
## Status
6+
7+
Accepted
8+
9+
## Context
10+
11+
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)).
12+
13+
## Decision
14+
15+
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.
16+
17+
## Consequences
18+
19+
* `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.

lib/puppet-languageserver-sidecar/puppet_helper.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +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
111+
finder.temp_file.unlink if item.key == 'file' && File.exist?(finder.temp_file.path) # Remove the temp_file.rb if it exists
112112
end
113113
end
114114

@@ -182,6 +182,7 @@ def initialize(puppet_env, object_types)
182182
# @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
183183
# @return [Array[String]] A list of all files that are found. This is the absolute path to the file.
184184
def find(from_root_path = nil)
185+
require 'tempfile'
185186
paths = []
186187
search_paths = @module_paths.nil? ? [] : @module_paths
187188
search_paths << @env_path unless @env_path.nil?
@@ -198,7 +199,8 @@ def find(from_root_path = nil)
198199
next unless path_in_root?(from_root_path, search_root) && Dir.exist?(search_root)
199200

200201
PuppetLanguageServerSidecar.log_message(:debug, "[PuppetPathFinder] Using '#{search_root}' as a directory to search")
201-
202+
# name of temp file to store the file type definitions (if any)
203+
@temp_file = Tempfile.new('file.rb')
202204
all_object_info.each do |object_type, paths_to_search|
203205
next unless object_types.include?(object_type)
204206

@@ -213,16 +215,11 @@ def find(from_root_path = nil)
213215
PuppetLanguageServerSidecar.log_message(:debug, "[PuppetPathFinder] Searching glob '#{glob}''")
214216

215217
Dir.glob(glob) do |filename|
216-
# name of temp file to store the file type definitions (if any)
217-
@temp_file = 'temp_file.rb'
218218
# if filename matches file.rb or /file/<any>.rb then we need to loop through each file type definition
219219
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
220+
PuppetLanguageServerSidecar.log_message(:debug, "[PuppetPathFinder] Found file type definition at '#{filename}'.")
221+
# Read each file type definition and write it to the temp file
222+
@temp_file.write(File.read(filename))
226223
else
227224
paths << filename
228225
end
@@ -231,7 +228,10 @@ def find(from_root_path = nil)
231228
end
232229
end
233230
#  Add the temp_file.rb to the paths array for searching (if exists)
234-
paths << @temp_file if @temp_file && File.exist?(@temp_file)
231+
if @temp_file && File.exist?(@temp_file.path)
232+
paths << @temp_file.path
233+
@temp_file.close
234+
end
235235
paths
236236
end
237237

0 commit comments

Comments
 (0)