Skip to content

DevContainer Template not as_jsonable when pretend running rails new testapp --devcontainer --pretend #53441

@plattenschieber

Description

@plattenschieber

Steps to reproduce

rails new testapp --devcontainer --pretend

Expected behavior

It should run through as it does when leaving out the --pretend flag.

Root cause?!

I've been debugging through the code, but don't understand what's different in the --pretend mode when rails is templating the devcontainer.json.tt:

template "devcontainer/devcontainer.json", ".devcontainer/devcontainer.json"

but somehow in --pretend mode, one of the features seems to be a hash and raises block in template': undefined method 'as_json' for an instance of Hash (NoMethodError) when tried to be as_jsoned

<%= features.map { |key, value| "\"#{key}\": #{value.as_json}" }.join(",\n    ") %>

Possibly mitigation

I've changed the raising method to to_json and added a require 'json' to the fail, which solved the issue:

diff --git a/devcontainer.json.tt b/devcontainer.json.tt
index 55eba9d..47dd156 100644
--- a/devcontainer.json.tt
+++ b/devcontainer.json.tt
@@ -1,3 +1,4 @@
+<% require 'json' %>
 // For format details, see https://aka.ms/devcontainer.json. For config options, see the
 // README at: https://github.com/devcontainers/templates/tree/main/src/ruby
 {
@@ -8,7 +9,7 @@
 
   // Features to add to the dev container. More info: https://containers.dev/features.
   "features": {
-    <%= features.map { |key, value| "\"#{key}\": #{value.as_json}" }.join(",\n    ") %>
+    <%= features.map { |key, value| "\"#{key}\": #{value.to_json}" }.join(",\n    ") %>
   },
 
 <%- if !container_env.empty? -%>
@@ -18,7 +19,7 @@
 <%- end -%>
 
   // Use 'forwardPorts' to make a list of ports inside the container available locally.
-  "forwardPorts": <%= forward_ports.as_json %>,
+  "forwardPorts": <%= forward_ports.to_json %>,
 
   // Configure tool-specific properties.
   // "customizations": {},

Actual behavior

/workspaces/test🔒 via 💎 v3.3.4 
⬢ [Docker] ❯ bundle exec rails --version
Rails 7.2.1.2

/workspaces/test🔒 via 💎 v3.3.4 
⬢ [Docker] ❯ bundle exec rails new testapp --devcontainer -p
      create  
      create  README.md
      [...]
      create  .devcontainer
bundler: failed to load command: rails (/home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/bin/rails)
/home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.2.1.2/lib/rails/generators/rails/devcontainer/templates/devcontainer/devcontainer.json.tt:11:in `block in template': undefined method `as_json' for an instance of Hash (NoMethodError)
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.2.1.2/lib/rails/generators/rails/devcontainer/templates/devcontainer/devcontainer.json.tt:11:in `each'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.2.1.2/lib/rails/generators/rails/devcontainer/templates/devcontainer/devcontainer.json.tt:11:in `map'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.2.1.2/lib/rails/generators/rails/devcontainer/templates/devcontainer/devcontainer.json.tt:11:in `template'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/3.3.0/erb.rb:429:in `eval'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/3.3.0/erb.rb:429:in `result'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/actions/file_manipulation.rb:128:in `block in template'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/actions/create_file.rb:54:in `render'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/actions/create_file.rb:64:in `block (2 levels) in invoke!'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/actions/create_file.rb:64:in `open'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/actions/create_file.rb:64:in `block in invoke!'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/actions/empty_directory.rb:117:in `invoke_with_conflict_check'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/actions/create_file.rb:61:in `invoke!'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/actions.rb:93:in `action'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/actions/create_file.rb:25:in `create_file'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/actions/file_manipulation.rb:124:in `template'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.2.1.2/lib/rails/generators/rails/devcontainer/devcontainer_generator.rb:34:in `create_devcontainer'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/command.rb:28:in `run'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/invocation.rb:127:in `invoke_command'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/invocation.rb:134:in `block in invoke_all'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/invocation.rb:134:in `each'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/invocation.rb:134:in `map'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/invocation.rb:134:in `invoke_all'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.2.1.2/lib/rails/generators/rails/app/app_generator.rb:284:in `devcontainer'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.2.1.2/lib/rails/generators/app_base.rb:167:in `public_send'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.2.1.2/lib/rails/generators/app_base.rb:167:in `build'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.2.1.2/lib/rails/generators/rails/app/app_generator.rb:472:in `create_devcontainer_files'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/command.rb:28:in `run'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/invocation.rb:127:in `invoke_command'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/invocation.rb:134:in `block in invoke_all'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/invocation.rb:134:in `each'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/invocation.rb:134:in `map'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/invocation.rb:134:in `invoke_all'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/group.rb:243:in `dispatch'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/base.rb:584:in `start'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.2.1.2/lib/rails/commands/application/application_command.rb:28:in `perform'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/command.rb:28:in `run'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor/invocation.rb:127:in `invoke_command'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.2.1.2/lib/rails/command/base.rb:178:in `invoke_command'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/thor-1.3.2/lib/thor.rb:538:in `dispatch'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.2.1.2/lib/rails/command/base.rb:73:in `perform'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.2.1.2/lib/rails/command.rb:71:in `block in invoke'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.2.1.2/lib/rails/command.rb:149:in `with_argv'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.2.1.2/lib/rails/command.rb:69:in `invoke'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.2.1.2/lib/rails/cli.rb:20:in `<top (required)>'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/3.3.0/bundled_gems.rb:74:in `require'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/3.3.0/bundled_gems.rb:74:in `block (2 levels) in replace_require'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/railties-7.2.1.2/exe/rails:10:in `<top (required)>'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/bin/rails:25:in `load'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/bin/rails:25:in `<top (required)>'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/site_ruby/3.3.0/bundler/cli/exec.rb:58:in `load'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/site_ruby/3.3.0/bundler/cli/exec.rb:58:in `kernel_load'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/site_ruby/3.3.0/bundler/cli/exec.rb:23:in `run'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/site_ruby/3.3.0/bundler/cli.rb:455:in `exec'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/site_ruby/3.3.0/bundler/vendor/thor/lib/thor/command.rb:28:in `run'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/site_ruby/3.3.0/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/site_ruby/3.3.0/bundler/vendor/thor/lib/thor.rb:527:in `dispatch'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/site_ruby/3.3.0/bundler/cli.rb:35:in `dispatch'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/site_ruby/3.3.0/bundler/vendor/thor/lib/thor/base.rb:584:in `start'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/site_ruby/3.3.0/bundler/cli.rb:29:in `start'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/bundler-2.5.22/exe/bundle:28:in `block in <top (required)>'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/site_ruby/3.3.0/bundler/friendly_errors.rb:117:in `with_friendly_errors'
        from /home/vscode/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/bundler-2.5.22/exe/bundle:20:in `<top (required)>'
        from /home/vscode/.rbenv/versions/3.3.4/bin/bundle:25:in `load'
        from /home/vscode/.rbenv/versions/3.3.4/bin/bundle:25:in `<main>'

System configuration

Rails version:
Rails 7.2.1.2, also happens at edge version
Ruby version:
ruby 3.3.4 (2024-07-09 revision be1089c8ec) [aarch64-linux]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions