rails: when creating a new rails 7.1 with bun, the JS side errors out on foreman (bin/dev)

After creating a new rails 7.1 app with bun enabled, the generated package.json doesn’t have @hotwired/turbo-rails added, so the Procfile.dev fails on the js process/container.

Steps to reproduce

➜ rails new pot -j bun -c tailwind

Expected behavior

I should get the Procfile.dev to be up and running.

Actual behavior

The JS process fails:

pot on  main [?] via ⬢ v18.15.0 via 🍞 v1.0.4 via 💎 v3.2.2 
➜ bin/dev 
20:14:02 web.1  | started with pid 9278
20:14:02 js.1   | started with pid 9279
20:14:02 css.1  | started with pid 9280
20:14:02 js.1   | $ bun bun.config.js --watch
20:14:02 css.1  | $ tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css --minify --watch
20:14:02 js.1   | Build failed
20:14:02 js.1   | 
20:14:02 js.1   | 
20:14:02 js.1   | error: Could not resolve: "@hotwired/turbo-rails". Maybe you need to "bun install"?
20:14:02 js.1   | 
20:14:02 js.1   | import "@hotwired/turbo-rails"
20:14:02 js.1   |        ^
20:14:02 js.1   | /Users/by/w/pot/app/javascript/application.js:2:8 64
20:14:02 css.1  | 
20:14:02 css.1  | Rebuilding...
20:14:02 css.1  | 
20:14:02 css.1  | Done in 103ms.
20:14:02 web.1  | DEBUGGER: Debugger can attach via UNIX domain socket (/var/folders/4p/zm2r8q_x41d8tvt4n3d1b_fh0000gn/T/ruby-debug-sock-501/ruby-debug-by-9278)
20:14:02 web.1  | => Booting Puma
20:14:02 web.1  | => Rails 7.1.0 application starting in development 
20:14:02 web.1  | => Run `bin/rails server --help` for more startup options
20:14:02 web.1  | Puma starting in single mode...
20:14:02 web.1  | * Puma version: 6.4.0 (ruby 3.2.2-p53) ("The Eagle of Durango")
20:14:02 web.1  | *  Min threads: 5
20:14:02 web.1  | *  Max threads: 5
20:14:02 web.1  | *  Environment: development
20:14:02 web.1  | *          PID: 9278
20:14:02 web.1  | * Listening on http://127.0.0.1:3000
20:14:02 web.1  | * Listening on http://[::1]:3000
20:14:02 web.1  | Use Ctrl-C to stop

Fix

Add the two hotwired packages:

➜ bun a @hotwired/turbo-rails
bun add v1.0.4 (745b6b94)

 installed @hotwired/turbo-rails@7.3.0


 3 packages installed [2.41s]

➜ bun a @hotwired/stimulus   
bun add v1.0.4 (745b6b94)

 installed @hotwired/stimulus@3.2.2


 1 packages installed [1353.00ms]

And here’s the updated package.json:

{
  "name": "app",
  "private": "true",
  "scripts": {
    "build": "bun bun.config.js",
    "build:css": "tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css --minify"
  },
  "dependencies": {
    "@hotwired/stimulus": "^3.2.2",
    "@hotwired/turbo-rails": "^7.3.0",
    "autoprefixer": "latest",
    "postcss": "latest",
    "tailwindcss": "latest"
  }
}

System configuration

Rails version: 7.1

Ruby version: 3.2.2

About this issue

  • Original URL
  • State: closed
  • Created 9 months ago
  • Comments: 16 (14 by maintainers)

Most upvoted comments

I’ve just published new versions of turbo-rails and stimulus-rails so new apps should be ok from now on.

@paulreece this is happening because someone on the Hotwire team still needs to cut a release of hotwire-rails and stimulus-rails that contains the bun code.

Yes and to be clear the main issue here is if the user wants to use bun both as a runtime and a package manager.

The issue will effectively be solved as soon as the new hotwire-rails and stimulus-rails versions are released.

@whyczz See the release notes here - It was released in March and @terracatta added the bun file a few weeks ago.

Okay, I went down a rabbit hole here and it looks like turbo-rails is not installing lib/install/turbo_with_bun.rb which would install turbo with bun:

if (js_entrypoint_path = Rails.root.join("app/javascript/application.js")).exist?
  say "Import Turbo"
  append_to_file "app/javascript/application.js", %(import "@hotwired/turbo-rails"\n)
else
  say "You must import @hotwired/turbo-rails in your JavaScript entrypoint file", :red
end

say "Install Turbo"
run "bun add @hotwired/turbo-rails"

Rails is still recognizing you chose bun here:

Writing lockfile to /Users/by/w/zot/Gemfile.lock
         run  bundle binstubs bundler
       rails  javascript:install:bun

After I temporarily uninstalled yarn I ran into the same issues as you. I think this is a bug, but with turbo-rails so maybe it should get filed over there?

You can see here we only get two of the install files and not the bun one:

turbo-rails-1.4.0/lib/install via 💎 v3.2.2
❯ ls
turbo_needs_redis.rb	turbo_with_importmap.rb	turbo_with_node.rb
(base)

@afcapel @kevinmcconnell any idea why the gem is installing without turbo_with_bun.rb?