In Rails 8.1, how does bin/rails tell its own commands from Rake tasks, and how do bin/rails --help and bin/rails -T differ?
answer
- command class first, Rake fallback
- --help hides the app's own tasks
- -T lists every described task
- bin/rake stats removed in 8.1
- tmp:clear spares tmp/pids
basics
~20 sbin/rails first looks for a Rails command class with that name (server, console, stats, notes, about); if none exists it hands the name to Rake. --help lists commands and framework tasks but hides the app's own lib/tasks; -T lists every described Rake task.
solid answer
~40 s`Rails::Command.invoke` splits the name, looks for a Thor-based command class (files named `*_command.rb` under `rails/commands`), and runs it; otherwise it loads the Rakefile, which calls `Rails.application.load_tasks`, and lets Rake run the task. An unknown name gets `Unrecognized command` with a did-you-mean hint. `bin/rails --help` prints the common commands, then every other command and the described Rake tasks defined outside the app, deliberately excluding tasks from your `Rakefile` and `lib/tasks`. `bin/rails -T` (`--tasks`) goes to Rake and lists every task with a `desc`, yours included. `bin/rails db:migrate -h` describes one task. `stats`, `notes` and `about` are commands, so `bin/rake stats`, deprecated in 8.0, is gone in 8.1; `tmp:clear` and `log:clear` are Rake tasks.
code
bash · 7 linesbin/rails --help # commands + framework/gem tasks, not lib/tasks
bin/rails -T # every described Rake task, yours included
bin/rails -T rentals # filter the task list by pattern
bin/rails db:migrate -h # describe one task
bin/rails stats # a command; bin/rake stats is gone in 8.1
bin/rails tmp:clear # cache, sockets, screenshots, storage (not pids)
LOGS=all bin/rails log:cleargo deeper
Type bin/rails for both commands and tasks, and use bin/rails -T to find tasks. Know about, stats and notes exist.
Explain the dispatch: command class first, Rake fallback, and why --help hides the app's own tasks while -T shows them. Know what tmp:clear and log:clear touch.
Read an upgrade note like the bin/rake stats removal correctly, update scripts and CI accordingly, and extend stats or notes through register_directory and register_tags.
Keep the app's operational entry points discoverable: described tasks, consistent namespaces, and documentation that points people at bin/rails -T rather than tribal knowledge.
## Two kinds of thing behind one executable `bin/rails` runs two different kinds of program: - **Rails commands**: Thor classes in `railties/lib/rails/commands` (and any gem's `rails/commands` path), each in a file ending `_command.rb`. Examples: `server`, `console`, `dbconsole`, `runner`, `about`, `stats`, `notes`, `routes`, `boot`, `credentials`. - **Rake tasks**: defined in `.rake` files, from Rails' own `rails/tasks` (`tmp:clear`, `log:clear`, `time:zones`), Active Record's `databases.rake` (`db:*`), gems' railties, and your app's `lib/tasks/*.rake`. Typing `bin/rails` for both is why people say Rails replaced `rake` for daily work. ## How dispatch decides `Rails::Command.invoke` handles every invocation: 1. Expand an alias (`s`, `c`, `db`, `r`, `g`, `d`, `t`). 2. Split `namespace:command` and look for a command class that defines it. 3. If one exists, run it. 4. Otherwise call Rake: load the `Rakefile` (which runs `Rails.application.load_tasks`) and run the task with the remaining arguments. 5. If Rake does not know the name either, print `Unrecognized command "..."` with a did-you-mean suggestion and exit 1. Because step 4 passes arguments to Rake's parser, Rake's own flags apply to tasks: `-T`, `--trace`, and `-e` meaning `--execute`. ## --help versus -T | Invocation | Shows | |---|---| | `bin/rails --help` (or `-h`, `help`) | the common commands, then every other command plus described Rake tasks defined **outside** the app | | `bin/rails -T` / `--tasks` | Rake's task list: every task with a `desc`, **including** your `lib/tasks` | | `bin/rails db:migrate -h` | Rake's description of that one task | | `bin/rails stats --help` | the command's options from its Thor definition | The filter on `--help` is intentional (Rails' tests assert it): the help screen documents the framework, while `-T` is how you find your own tasks. A task without `desc` appears in neither list. ## Commands and tasks you meet daily - `bin/rails about`: Rails, Ruby, RubyGems and Rack versions, the middleware, application root, environment, database adapter and schema version. - `bin/rails stats`: lines of code, classes and methods per directory, plus the code-to-test ratio. Extra directories go in via `Rails::CodeStatistics.register_directory("Services", "app/services")`; the old `STATS_DIRECTORIES` constant is gone. - `bin/rails notes`: comments tagged `FIXME`, `OPTIMIZE` or `TODO` in `app`, `config`, `db`, `lib` and `test`; `--annotations` (`-a`) filters tags and `config.annotations.register_tags` adds new ones. - `bin/rails tmp:clear`: empties `tmp/cache`, `tmp/sockets`, `tmp/screenshots` and `tmp/storage`, but leaves `tmp/pids`, so a running server's PID file survives. - `bin/rails log:clear`: truncates `log/<env>.log` for each environment in `config/environments` to zero bytes; `LOGS=all` covers every `log/*.log`, `LOGS=test` just one. ## What changed in 8.x - **8.0** deprecated `bin/rake stats` and the `STATS_DIRECTORIES` constant. - **8.1** removed both. `stats` exists only as a command, so `bin/rake stats` now fails because Rake has no task by that name. `bin/rake` is still generated and still runs any Rake task, but it does not know Rails commands. The rule of thumb: type `bin/rails` for everything, use `-T` to find tasks, and `--help` to learn the framework's surface. ## Small flags worth knowing - `bin/rails -T rentals` filters Rake's list by a pattern, handy in an app with hundreds of tasks. - `bin/rails some:task --trace` passes Rake's `--trace` through, printing the full backtrace and the order tasks were invoked. - `bin/rails -v` (or `--version`, or the `version` command) prints the Rails version without booting the app for a full `about` report. - `bin/rails <command> --help` works for every Thor command, because the help text comes from the command's own `desc` and `class_option` declarations. Knowing which side of the line a name falls on explains most surprises: a command takes Thor options such as `-e`, while a task takes Rake's options and reads its settings from environment variables such as `RAILS_ENV`, `VERSION` or `LOGS`.
- How do you add app/services to the bin/rails stats report in Rails 8.1?Call `Rails::CodeStatistics.register_directory("Services", "app/services")`, for example from an initializer or a Rake file; pass `test_directory: true` for a test folder so it counts toward test LOC. Appending to `STATS_DIRECTORIES`, the old approach, was deprecated in 8.0 and removed in 8.1.
- Why does a task in lib/tasks not show up in bin/rails --help?The help screen lists commands and only those described Rake tasks whose definition lives outside the application root, so tasks from the app's `Rakefile` and `lib/tasks` are filtered out on purpose. Use `bin/rails -T` (or `--tasks`), which asks Rake for every task with a `desc`.
- bin/rails --help lists every task defined in lib/tasks
- bin/rake stats still works in Rails 8.1 with a deprecation warning
- bin/rails tmp:clear also deletes tmp/pids/server.pid
- bin/rails notes searches only the app directory
- bin/rake can run bin/rails console and server too
Assumes Rails 8.1, which removed bin/rake stats and STATS_DIRECTORIES after their 8.0 deprecation.