Skip to content

Fix generate action --slice for parenthesized and single-line slice blocks in routes - #464

Closed
anyingiit wants to merge 1 commit into
hanami:mainfrom
anyingiit:fix-slice-route-matcher-parentheses
Closed

anyingiit wants to merge 1 commit into
hanami:mainfrom
anyingiit:fix-slice-route-matcher-parentheses

Conversation

@anyingiit

@anyingiit anyingiit commented Oct 22, 2026 •

Copy link
Copy Markdown

Requested by winshyon

Description

Fixes hanami/hanami#1284 (the issue is tracked in hanami/hanami, but the code lives here).

hanami generate action ... --slice=main locates the slice block in config/routes.rb with
/slice[[:space:]]*:main/. That only matches slice :main, at: "/" do, so a routes file that
declares slices with parentheses (as generated by e.g. hanamismith) fails:

class Routes < Hanami::Routes
  slice(:health, at: "/status") { root to: "show" }
  slice(:main, at: "/") { root to: "home.show" }
end
cannot find `(?-mix:slice[[:space:]]*:main)` in `config/routes.rb`

This PR:

  1. Widens the matcher in Generators::App::Action#add_route_to_block to
    /slice[[:space:]]*\(?[[:space:]]*:#{namespace}/, so slice(:main, at: "/") do ... end works.

  2. Handles single-line brace blocks such as slice(:main, at: "/") { root to: "home.show" }
    (the exact form from the issue). A route can't be injected into a one-line block, and with
    only the matcher change Dry::Files#inject_line_at_block_bottom would put the route in the
    wrong place (outside the block). So when the matched slice line is a single-line { ... }
    block, it is rewritten as a multi-line do ... end block that keeps the existing body and
    adds the new route:

    slice(:main, at: "/") do
      root to: "home.show"
      post "/subscribe", to: "home.subscribe"
    end

    Multi-line do ... end blocks still go through inject_line_at_block_bottom exactly as before.
    The existing duplicate check (block_contains?) runs first, so an existing route in a one-line
    block is still detected and skipped.

Reviewers may want to weigh in on the rewrite in (2): it changes the user's formatting of that one
slice line. The alternative would be to raise a clearer error for single-line blocks; happy to switch
to that if you prefer.

Motivation / disclosure: I had some spare AI-assistant quota (Claude Code) and am using it to try to
help projects with open good-first-issues. The change was prepared with Claude Code and verified as listed
below. If it doesn't fit, isn't up to your bar, or you'd simply rather not take it — please feel free to
close it, no hard feelings at all 🙂

Related issue

Fixes hanami/hanami#1284

Checklist

  • Tests pass locally:
    • New specs in spec/unit/hanami/cli/commands/app/generate/action_spec.rb
      ("appends routes within the proper slice block when slice arguments use parentheses",
      "expands single-line slice blocks to append routes") fail on main without the fix
      (TypeError from block_contains? because the slice isn't found) and pass with it.
    • bundle exec rspec spec/unit/hanami/cli/commands/app/generate/ → 169 examples, 0 failures, 1 pending.
    • bundle exec rspec spec/unit → the only failures are DB specs (no Postgres/MySQL available locally)
      and 4 gem/new_spec encoding failures due to the local locale; identical on main without this change.
    • bundle exec rubocop on the changed files → no offenses.
  • CHANGELOG.md is updated (if applicable) — entry under Unreleased → Fixed
  • Documentation is updated (if applicable) — n/a (no user-facing API change)

🤖 Generated with Claude Code

`hanami generate action --slice=main` looked up the slice block in
config/routes.rb with /slice[[:space:]]*:main/, which does not match
slices declared with parentheses, e.g. `slice(:main, at: "/") do`,
and failed with a "cannot find" error.

Allow an optional opening parenthesis before the slice name. Since a
route cannot be appended to a single-line block such as
`slice(:main, at: "/") { root to: "home.show" }`, expand such a block
into a multi-line `do ... end` block containing the new route.
@anyingiit
anyingiit force-pushed the fix-slice-route-matcher-parentheses branch from 53af38c to e33cbf8 Compare October 22, 2026 19:14
@timriley

Copy link
Copy Markdown
Member

Hi @anyingiit, thanks for thinking of us, but per our AI contributions policy, we are not interested in contributions from autonomous agents without meaningful human attention on the other side.

I'll be closing your PRs. If you're interested in working with us in a more direct, human-centred way, you're welcome to do that in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generate action cannot find slice in certain code formatting styles

2 participants