Skip to content

Commit

Permalink
Merge pull request #2111 from Shopify/ar/handle-no-gem-gracefully
Browse files Browse the repository at this point in the history
[Tapioca Addon] Handle missing gems gracefully in tapioca gem command
  • Loading branch information
alexcrocha authored Dec 18, 2024
2 parents c0e04e5 + 48799d0 commit 14c955d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/tapioca/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def todo
default: {}
option :lsp_addon,
type: :boolean,
desc: "Generate DSL RBIs from the LSP addon. Internal to tapioca and not intended for end-users",
desc: "Generate DSL RBIs from the LSP add-on. Internal to Tapioca and not intended for end-users",
default: false,
hide: true
def dsl(*constant_or_paths)
Expand Down Expand Up @@ -266,6 +266,11 @@ def dsl(*constant_or_paths)
type: :boolean,
desc: "Halt upon a load error while loading the Rails application",
default: true
option :lsp_addon,
type: :boolean,
desc: "Generate Gem RBIs from the LSP add-on. Internal to Tapioca and not intended for end-users",
default: false,
hide: true
def gem(*gems)
set_environment(options)

Expand Down Expand Up @@ -300,6 +305,7 @@ def gem(*gems)
dsl_dir: options[:dsl_dir],
rbi_formatter: rbi_formatter(options),
halt_upon_load_error: options[:halt_upon_load_error],
lsp_addon: options[:lsp_addon],
}

command = if verify
Expand Down
5 changes: 4 additions & 1 deletion lib/tapioca/commands/abstract_gem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class AbstractGem < Command
dsl_dir: String,
rbi_formatter: RBIFormatter,
halt_upon_load_error: T::Boolean,
lsp_addon: T.nilable(T::Boolean),
).void
end
def initialize(
Expand All @@ -45,7 +46,8 @@ def initialize(
auto_strictness: true,
dsl_dir: DEFAULT_DSL_DIR,
rbi_formatter: DEFAULT_RBI_FORMATTER,
halt_upon_load_error: true
halt_upon_load_error: true,
lsp_addon: false
)
@gem_names = gem_names
@exclude = exclude
Expand All @@ -59,6 +61,7 @@ def initialize(
@auto_strictness = auto_strictness
@dsl_dir = dsl_dir
@rbi_formatter = rbi_formatter
@lsp_addon = lsp_addon

super()

Expand Down
2 changes: 2 additions & 0 deletions lib/tapioca/commands/gem_generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def gems_to_generate(gem_names)
gem = @bundle.gem(gem_name)

if gem.nil?
next if @lsp_addon

raise Thor::Error, set_color("Error: Cannot find gem '#{gem_name}'", :red)
end

Expand Down
14 changes: 14 additions & 0 deletions spec/tapioca/cli/gem_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,20 @@ class Secret; end
assert_success_status(result)
end

it "skips missing gems and continues with warning when --lsp_addon is used" do
result = @project.tapioca("gem non_existent_gem --lsp_addon")

assert_empty_stderr(result)
assert_success_status(result)
end

it "fails with error when gem cannot be found" do
result = @project.tapioca("gem non_existent_gem")

assert_stderr_includes(result, "Error: Cannot find gem 'non_existent_gem'")
refute_success_status(result)
end

it "does not crash when the extras gem is loaded" do
foo = mock_gem("foo", "0.0.1") do
write!("lib/foo.rb", FOO_RB)
Expand Down

0 comments on commit 14c955d

Please sign in to comment.