New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Check generated RBI files for correctness #842
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,17 +9,24 @@ module RBIHelper | |
requires_ancestor { Thor::Shell } | ||
requires_ancestor { SorbetHelper } | ||
|
||
sig { params(gem_names: T::Array[String], gem_dir: String, dsl_dir: String).void } | ||
def update_gem_rbis_strictnesses(gem_names, gem_dir: DEFAULT_GEM_DIR, dsl_dir: DEFAULT_DSL_DIR) | ||
return unless File.directory?(dsl_dir) | ||
|
||
sig do | ||
params( | ||
command: String, | ||
gem_dir: String, | ||
dsl_dir: String, | ||
auto_strictness: T::Boolean, | ||
gems: T::Array[Gemfile::GemSpec], | ||
compilers: T::Enumerable[Class] | ||
).void | ||
end | ||
def validate_rbi_files(command:, gem_dir:, dsl_dir:, auto_strictness:, gems: [], compilers: []) | ||
error_url_base = Spoom::Sorbet::Errors::DEFAULT_ERROR_URL_BASE | ||
|
||
say("Typechecking RBI files... ") | ||
say("Checking generated RBI files... ") | ||
res = sorbet( | ||
"--no-config", | ||
"--error-url-base=#{error_url_base}", | ||
"--isolate-error-code 4010", | ||
"--stop-after namer", | ||
dsl_dir, | ||
gem_dir | ||
) | ||
|
@@ -28,10 +35,62 @@ def update_gem_rbis_strictnesses(gem_names, gem_dir: DEFAULT_GEM_DIR, dsl_dir: D | |
errors = Spoom::Sorbet::Errors::Parser.parse_string(res.err) | ||
|
||
if errors.empty? | ||
say("No error found", [:green, :bold]) | ||
say(" No errors found\n\n", [:green, :bold]) | ||
return | ||
end | ||
|
||
parse_errors = errors.select { |error| error.code < 4000 } | ||
|
||
if parse_errors.any? | ||
say_error(<<~ERR, :red) | ||
|
||
##### INTERNAL ERROR ##### | ||
|
||
There are parse errors in the generated RBI files. | ||
|
||
This seems related to a bug in Tapioca. | ||
Please open an issue at https://github.com/Shopify/tapioca/issues/new with the following information: | ||
|
||
Tapioca v#{Tapioca::VERSION} | ||
|
||
Command: | ||
#{command} | ||
|
||
ERR | ||
|
||
say_error(<<~ERR, :red) if gems.any? | ||
Gems: | ||
#{gems.map { |gem| " #{gem.name} (#{gem.version})" }.join("\n")} | ||
|
||
ERR | ||
|
||
say_error(<<~ERR, :red) if compilers.any? | ||
Compilers: | ||
#{compilers.map { |compiler| " #{compiler.name}" }.join("\n")} | ||
|
||
ERR | ||
|
||
say_error(<<~ERR, :red) | ||
Errors: | ||
#{parse_errors.map { |error| " #{error}" }.join("\n")} | ||
|
||
########################## | ||
|
||
ERR | ||
end | ||
|
||
if auto_strictness | ||
redef_errors = errors.select { |error| error.code == 4010 } | ||
update_gem_rbis_strictnesses(redef_errors, gem_dir) | ||
end | ||
|
||
Kernel.exit(1) if parse_errors.any? | ||
end | ||
|
||
private | ||
|
||
sig { params(errors: T::Array[Spoom::Sorbet::Errors::Error], gem_dir: String).void } | ||
def update_gem_rbis_strictnesses(errors, gem_dir) | ||
files = [] | ||
|
||
errors.each do |error| | ||
|
@@ -48,10 +107,8 @@ def update_gem_rbis_strictnesses(gem_names, gem_dir: DEFAULT_GEM_DIR, dsl_dir: D | |
files | ||
.uniq | ||
.sort | ||
.select do |file| | ||
name = gem_name_from_rbi_path(file) | ||
file.start_with?(gem_dir) && (gem_names.empty? || gem_names.include?(name)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before the auto fix of the typed-override was only applied on requested gems but it doesn't make sense as a newly generated RBI can produce conflicts in old RBIs so I removed the feature. |
||
end.each do |file| | ||
.select { |file| file.start_with?(gem_dir) } | ||
.each do |file| | ||
Spoom::Sorbet::Sigils.change_sigil_in_file(file, "false") | ||
say("\n Changed strictness of #{file} to `typed: false` (conflicting with DSL files)", [:yellow, :bold]) | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better to use the real Thor command here but I believe it would require a lot of refactoring
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is good enough.