-
Notifications
You must be signed in to change notification settings - Fork 28
Use typed override for spoom bump #966
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
Open
Morriar
wants to merge
1
commit into
main
Choose a base branch
from
at-bump-typed-override
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -3,6 +3,8 @@ | |
|
|
||
| require "find" | ||
| require "open3" | ||
| require "securerandom" | ||
| require "yaml" | ||
|
|
||
| module Spoom | ||
| module Cli | ||
|
|
@@ -72,6 +74,11 @@ def bump(directory = ".") | |
| exit(1) | ||
| end | ||
|
|
||
| unless context.sorbet_config.typed_overrides.empty? | ||
| say_error("Cannot run `spoom bump` on a project that already uses Sorbet's `--typed-override` option") | ||
| exit(1) | ||
| end | ||
|
|
||
| say("Checking files...") | ||
|
|
||
| files_to_bump = context.srb_files_with_strictness(from, include_rbis: false) | ||
|
|
@@ -90,18 +97,18 @@ def bump(directory = ".") | |
| exit(0) | ||
| end | ||
|
|
||
| Sorbet::Sigils.change_sigil_in_files(files_to_bump, to) | ||
|
|
||
| if force | ||
| Sorbet::Sigils.change_sigil_in_files(files_to_bump, to) unless dry | ||
| print_changes(files_to_bump, command: cmd, from: from, to: to, dry: dry, path: exec_path) | ||
| undo_changes(files_to_bump, from) if dry | ||
| exit(files_to_bump.empty?) | ||
| end | ||
|
|
||
| error_url_base = Spoom::Sorbet::Errors::DEFAULT_ERROR_URL_BASE | ||
| typed_override_file = write_typed_override_file(context, files_to_bump, to) | ||
| result = begin | ||
| T.unsafe(context).srb_tc( | ||
| *options[:sorbet_options].split(" "), | ||
| "--typed-override=#{typed_override_file}", | ||
| "--error-url-base=#{error_url_base}", | ||
| capture_err: true, | ||
| sorbet_bin: options[:sorbet], | ||
|
|
@@ -114,7 +121,6 @@ def bump(directory = ".") | |
| It means one of the file bumped to `typed: #{to}` made Sorbet crash. | ||
| Run `spoom bump -f` locally followed by `bundle exec srb tc` to investigate the problem. | ||
| ERR | ||
| undo_changes(files_to_bump, from) | ||
| exit(error.result.exit_code) | ||
| rescue Spoom::Sorbet::Error::Killed => error | ||
| say_error(<<~ERR, status: nil) | ||
|
|
@@ -123,21 +129,21 @@ def bump(directory = ".") | |
| It means Sorbet was killed while executing. Changes to files have not been applied. | ||
| Re-run `spoom bump` to try again. | ||
| ERR | ||
| undo_changes(files_to_bump, from) | ||
| exit(error.result.exit_code) | ||
| ensure | ||
| context.remove!(typed_override_file) | ||
| end | ||
|
|
||
| if result.status | ||
| Sorbet::Sigils.change_sigil_in_files(files_to_bump, to) unless dry | ||
| print_changes(files_to_bump, command: cmd, from: from, to: to, dry: dry, path: exec_path) | ||
| undo_changes(files_to_bump, from) if dry | ||
| exit(files_to_bump.empty?) | ||
| end | ||
|
|
||
| unless result.exit_code == 100 | ||
| # Sorbet will return exit code 100 if there are type checking errors. | ||
| # If Sorbet returned something else, it means it didn't terminate normally. | ||
| say_error(result.err, status: nil, nl: false) | ||
| undo_changes(files_to_bump, from) | ||
| exit(1) | ||
| end | ||
|
|
||
|
|
@@ -156,13 +162,11 @@ def bump(directory = ".") | |
| path | ||
| end.compact.uniq | ||
|
|
||
| undo_changes(files_with_errors, from) | ||
|
|
||
| say("Found #{errors.length} type checking error#{"s" if errors.length > 1}") if options[:count_errors] | ||
|
|
||
| files_changed = files_to_bump - files_with_errors | ||
| Sorbet::Sigils.change_sigil_in_files(files_changed, to) unless dry | ||
| print_changes(files_changed, command: cmd, from: from, to: to, dry: dry, path: exec_path) | ||
| undo_changes(files_to_bump, from) if dry | ||
| exit(files_changed.empty?) | ||
| end | ||
|
|
||
|
|
@@ -189,8 +193,15 @@ def print_changes(files, command:, from: "false", to: "true", dry: false, path: | |
| end | ||
| end | ||
|
|
||
| def undo_changes(files, from_strictness) | ||
| Sorbet::Sigils.change_sigil_in_files(files, from_strictness) | ||
| #: (Spoom::Context context, Array[String] files, String strictness) -> String | ||
| def write_typed_override_file(context, files, strictness) | ||
| path = ".spoom-typed-override-#{Process.pid}-#{SecureRandom.hex(8)}.yaml" | ||
| relative_paths = files.map do |file| | ||
| relative_path = file.delete_prefix("#{context.absolute_path}/") | ||
| "./#{relative_path}" | ||
| end | ||
| context.write!(path, YAML.dump({ strictness => relative_paths })) | ||
|
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. Do we delete the file when we're done? |
||
| path | ||
| end | ||
| end | ||
| end | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Can't we just use
Tempfile.new(['spoom-typed-override', '.yml'])?Then there's no need to have it hidden with the prefixed
.