Skip to content

Commit

Permalink
(CAT-1493) - Fix missing file resource type parameters
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jordanbreen28 committed Oct 26, 2023
1 parent d42b6b4 commit 7500c73
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions lib/puppet-languageserver-sidecar/puppet_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,16 @@ def self.retrieve_via_puppet_strings(cache, options = {})
next unless object_types.include?(:type)

file_doc.types.each do |item|
result.append!(item) unless name == 'whit' || name == 'component'
if item.key == 'file'
search_root = File.expand_path(finder.puppet_path)
path = File.join(search_root, 'lib/puppet/type')
# Manually set the source and calling_source for the file type
item.source = "#{path}/#{item.key}.rb"
item.calling_source = item.source
# Remove the temp_file.rb if it exists

Check failure on line 116 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : windows-latest Ruby 2.7 / spec

Layout/CommentIndentation: Incorrect indentation detected (column 13 instead of 12).

Check failure on line 116 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : ubuntu-latest Ruby 2.7 / spec

Layout/CommentIndentation: Incorrect indentation detected (column 13 instead of 12).

Check failure on line 116 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : ubuntu-latest Ruby 3.2 / spec

Layout/CommentIndentation: Incorrect indentation detected (column 13 instead of 12).

Check failure on line 116 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : windows-latest Ruby 3.2 / spec

Layout/CommentIndentation: Incorrect indentation detected (column 13 instead of 12).
File.delete(finder.temp_file) if File.exist?(finder.temp_file)

Check failure on line 117 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : windows-latest Ruby 2.7 / spec

Lint/NonAtomicFileOperation: Use atomic file operation method `FileUtils.rm_f`.

Check failure on line 117 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : windows-latest Ruby 2.7 / spec

Lint/NonAtomicFileOperation: Remove unnecessary existence check `File.exist?`.

Check failure on line 117 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : ubuntu-latest Ruby 2.7 / spec

Lint/NonAtomicFileOperation: Use atomic file operation method `FileUtils.rm_f`.

Check failure on line 117 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : ubuntu-latest Ruby 2.7 / spec

Lint/NonAtomicFileOperation: Remove unnecessary existence check `File.exist?`.

Check failure on line 117 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : ubuntu-latest Ruby 3.2 / spec

Lint/NonAtomicFileOperation: Use atomic file operation method `FileUtils.rm_f`.

Check failure on line 117 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : ubuntu-latest Ruby 3.2 / spec

Lint/NonAtomicFileOperation: Remove unnecessary existence check `File.exist?`.

Check failure on line 117 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : windows-latest Ruby 3.2 / spec

Lint/NonAtomicFileOperation: Use atomic file operation method `FileUtils.rm_f`.

Check failure on line 117 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : windows-latest Ruby 3.2 / spec

Lint/NonAtomicFileOperation: Remove unnecessary existence check `File.exist?`.
end
result.append!(item) unless %w[whit component].include?(item.key)

Check failure on line 119 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : windows-latest Ruby 2.7 / spec

Performance/CollectionLiteralInLoop: Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant.

Check failure on line 119 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : ubuntu-latest Ruby 2.7 / spec

Performance/CollectionLiteralInLoop: Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant.

Check failure on line 119 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : ubuntu-latest Ruby 3.2 / spec

Performance/CollectionLiteralInLoop: Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant.

Check failure on line 119 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : windows-latest Ruby 3.2 / spec

Performance/CollectionLiteralInLoop: Avoid immutable Array literals in loops. It is better to extract it into a local variable or a constant.
end
end

Expand Down Expand Up @@ -153,6 +162,7 @@ def self.current_environment
# DataType ruby files or manifests.
class PuppetPathFinder
attr_reader :object_types
attr_accessor :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 +222,25 @@ def find(from_root_path = nil)
PuppetLanguageServerSidecar.log_message(:debug, "[PuppetPathFinder] Searching glob '#{glob}''")

Dir.glob(glob) do |filename|
paths << filename
# if filename matches file.rb or file/ then we need to loop through each file type definition

Check failure on line 225 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : windows-latest Ruby 2.7 / spec

Layout/CommentIndentation: Incorrect indentation detected (column 14 instead of 16).

Check failure on line 225 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : ubuntu-latest Ruby 2.7 / spec

Layout/CommentIndentation: Incorrect indentation detected (column 14 instead of 16).

Check failure on line 225 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : ubuntu-latest Ruby 3.2 / spec

Layout/CommentIndentation: Incorrect indentation detected (column 14 instead of 16).

Check failure on line 225 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : windows-latest Ruby 3.2 / spec

Layout/CommentIndentation: Incorrect indentation detected (column 14 instead of 16).
if filename.match?(%r{/file/|file.rb})
# Create a temp file to store the file type definitions

Check failure on line 227 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : windows-latest Ruby 2.7 / spec

Layout/CommentIndentation: Incorrect indentation detected (column 16 instead of 18).

Check failure on line 227 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : ubuntu-latest Ruby 2.7 / spec

Layout/CommentIndentation: Incorrect indentation detected (column 16 instead of 18).

Check failure on line 227 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : ubuntu-latest Ruby 3.2 / spec

Layout/CommentIndentation: Incorrect indentation detected (column 16 instead of 18).

Check failure on line 227 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : windows-latest Ruby 3.2 / spec

Layout/CommentIndentation: Incorrect indentation detected (column 16 instead of 18).
@temp_file = 'temp_file.rb'
# 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)

Check failure on line 242 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : windows-latest Ruby 2.7 / spec

Layout/LeadingCommentSpace: Missing space after `#`.

Check failure on line 242 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : ubuntu-latest Ruby 2.7 / spec

Layout/LeadingCommentSpace: Missing space after `#`.

Check failure on line 242 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : ubuntu-latest Ruby 3.2 / spec

Layout/LeadingCommentSpace: Missing space after `#`.

Check failure on line 242 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : windows-latest Ruby 3.2 / spec

Layout/LeadingCommentSpace: Missing space after `#`.
paths << @temp_file if @temp_file && File.exist?(@temp_file)
paths
end

Expand Down Expand Up @@ -258,7 +281,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: '/{,*/}*.rb' }, # Contains Puppet resource types. Resource types like `file` can live in subdirs, hence the glob

Check failure on line 284 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : windows-latest Ruby 2.7 / spec

Style/TrailingCommaInArrayLiteral: Avoid comma after the last item of an array.

Check failure on line 284 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : ubuntu-latest Ruby 2.7 / spec

Style/TrailingCommaInArrayLiteral: Avoid comma after the last item of an array.

Check failure on line 284 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : ubuntu-latest Ruby 3.2 / spec

Style/TrailingCommaInArrayLiteral: Avoid comma after the last item of an array.

Check failure on line 284 in lib/puppet-languageserver-sidecar/puppet_helper.rb

View workflow job for this annotation

GitHub Actions / Spec : windows-latest Ruby 3.2 / spec

Style/TrailingCommaInArrayLiteral: Avoid comma after the last item of an array.
]
}
end
Expand Down

0 comments on commit 7500c73

Please sign in to comment.