ruby-on-rails - 无方法错误 : undefined method `+' for nil:NilClass/ 'additional_paths' [capistrano+webpacker]

标签 ruby-on-rails ruby webpack capistrano

我不得不将我的 rails 版本从 5 更新到 6,在此之前一切正常。

问题只发生在 capistrano...当我运行时 rake assets:precompile即使使用新版本,一切正常。

这是错误:

deploy:assets:precompile
      01 $HOME/.rbenv/bin/rbenv exec bundle exec rake assets:precompile
      01 warning: parser/current is loading parser/ruby25, which recognizes
      01 warning: 2.5.7-compliant syntax, but you are running 2.5.3.
      01 warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.

yarn install v1.16.0
[1/4] Resolving packages...
⠁

success Already up-to-date.
Done in 0.71s.
      01 Warning: you are using an unstable release of Node.js (v11.15.0). If you encounter issues with Node.js, consider switching to an Active LTS release. More info: https://docs.npmjs.com/try-the-latest-stable-version-of-node
      01 Webpacker::Compiler.watched_paths has been deprecated. Set additional_paths in webpacker.yml instead.
      01 rake aborted!
      01 NoMethodError: undefined method `+' for nil:NilClass
      01 /web/export-at-ibm/shared/bundle/ruby/2.5.0/bundler/gems/webpacker-d099e066931b/lib/webpacker/configuration.rb:31:in `additional_paths'
      01 /web/export-at-ibm/shared/bundle/ruby/2.5.0/bundler/gems/webpacker-d099e066931b/lib/webpacker/configuration.rb:35:in `additional_paths_globbed'
      01 /web/export-at-ibm/shared/bundle/ruby/2.5.0/bundler/gems/webpacker-d099e066931b/lib/webpacker/compiler.rb:93:in `default_watched_paths'
      01 /web/export-at-ibm/shared/bundle/ruby/2.5.0/bundler/gems/webpacker-d099e066931b/lib/webpacker/compiler.rb:57:in `watched_files_digest'
      01 /web/export-at-ibm/shared/bundle/ruby/2.5.0/bundler/gems/webpacker-d099e066931b/lib/webpacker/compiler.rb:38:in `fresh?'
      01 /web/export-at-ibm/shared/bundle/ruby/2.5.0/bundler/gems/webpacker-d099e066931b/lib/webpacker/compiler.rb:43:in `stale?'
      01 /web/export-at-ibm/shared/bundle/ruby/2.5.0/bundler/gems/webpacker-d099e066931b/lib/webpacker/compiler.rb:22:in `compile'
      01 /web/export-at-ibm/shared/bundle/ruby/2.5.0/bundler/gems/webpacker-d099e066931b/lib/webpacker/commands.rb:51:in `compile'
      01 /web/export-at-ibm/shared/bundle/ruby/2.5.0/bundler/gems/webpacker-d099e066931b/lib/webpacker.rb:35:in `compile'
      01 /web/export-at-ibm/shared/bundle/ruby/2.5.0/bundler/gems/webpacker-d099e066931b/lib/tasks/webpacker/compile.rake:23:in `block (4 levels) in <top (required)>'
      01 /web/export-at-ibm/shared/bundle/ruby/2.5.0/bundler/gems/webpacker-d099e066931b/lib/webpacker.rb:28:in `ensure_log_goes_to_stdout'
      01 /web/export-at-ibm/shared/bundle/ruby/2.5.0/bundler/gems/webpacker-d099e066931b/lib/tasks/webpacker/compile.rake:22:in `block (3 levels) in <top (required)>'
      01 /web/export-at-ibm/shared/bundle/ruby/2.5.0/bundler/gems/webpacker-d099e066931b/lib/webpacker.rb:20:in `with_node_env'
      01 /web/export-at-ibm/shared/bundle/ruby/2.5.0/bundler/gems/webpacker-d099e066931b/lib/tasks/webpacker/compile.rake:21:in `block (2 levels) in <top (required)>'
      01 /web/export-at-ibm/shared/bundle/ruby/2.5.0/bundler/gems/webpacker-d099e066931b/lib/tasks/webpacker/compile.rake:14:in `block in enhance_assets_precompile'
      01 /web/export-at-ibm/shared/bundle/ruby/2.5.0/gems/rake-12.3.3/exe/rake:27:in `<top (required)>'
      01 /home/glbrgit/.rbenv/versions/2.5.3/bin/bundle:23:in `load'
      01 /home/glbrgit/.rbenv/versions/2.5.3/bin/bundle:23:in `<main>'
      01 Tasks: TOP => webpacker:compile
      01 (See full trace by running task with --trace)
#<Thread:0x00007fec80b805a0@/Users/thiagogs/.rvm/gems/ruby-2.5.3/gems/sshkit-1.21.0/lib/sshkit/runners/parallel.rb:10 run> terminated with exception (report_on_exception is true):
Traceback (most recent call last):
    1: from /Users/thiagogs/.rvm/gems/ruby-2.5.3/gems/sshkit-1.21.0/lib/sshkit/runners/parallel.rb:11:in `block (2 levels) in execute'
/Users/thiagogs/.rvm/gems/ruby-2.5.3/gems/sshkit-1.21.0/lib/sshkit/runners/parallel.rb:15:in `rescue in block (2 levels) in execute': Exception while executing as glbrgit@test-export.w3-969.ibm.com: rake exit status: 1 (SSHKit::Runner::ExecuteError)
rake stdout: warning: parser/current is loading parser/ruby25, which recognizes
warning: 2.5.7-compliant syntax, but you are running 2.5.3.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
yarn install v1.16.0
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.71s.

这些是我的文件:

配置/应用程序.rb

require 'rails'
# Pick the frameworks you want:
require 'active_model/railtie'
require 'active_job/railtie'
# require "active_record/railtie"
# require "active_storage/engine"
require 'action_controller/railtie'
require 'action_mailer/railtie'
require 'action_view/railtie'
require 'action_cable/engine'
require 'sprockets/railtie'
# require "rails/test_unit/railtie"
require 'i18n/backend/fallbacks'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

# Webpacker::Compiler.watched_paths << '../app/javascript'
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)

module ExportAtIbm
    class Application < Rails::Application
        I18n.available_locales = %w[en br]
        config.i18n.default_locale = :br
        config.i18n.fallbacks = [I18n.default_locale]

        config.middleware.use I18n::JS::Middleware

        # Initialize configuration defaults for originally generated Rails version.
        config.load_defaults 5.2

        config.autoload_paths << "#{Rails.root}/lib"
        config.eager_load_paths << "#{Rails.root}/lib"

        config.assets.paths << Rails.root.join('app', 'assets', 'font', 'roboto')
        config.assets.paths << Rails.root.join('vendor', 'assets', 'img')
        config.assets.paths << Rails.root.join('vendor', 'assets', 'mdb-addons')

        config.generators do |generator|
            generator.stylesheets = false
            generator.javascripts = false
            generator.helper = false

            generator.fixture_replacement :factory_bot, dir: 'spec/support/factory_bot/'
        end

        # Settings in config/environments/* take precedence over those specified here.
        # Application configuration should go into files in config/initializers
        # -- all .rb files in that directory are automatically loaded.
        Apipie.configure do |config|
            config.validate = false
        end

        config.assets.precompile << %w[i18n i18n/*]

        # Don't generate system test files.
        config.generators.system_tests = nil
        config.customs = {
            integrations: JSON.parse(config_for(:integrations).to_json, symbolize_names: true)
        }
    end
end

配置/部署.rb

# Server info
server 'test-export.w3-969.ibm.com', user: 'glbrgit', roles: %w[app worker], primary: true

# General configs
set :ssh_options,     forward_agent: true, user: 'glbrgit', keys: %w[~/.ssh/id_rsa.pub]
set :pty,             true
set :use_sudo,        false
set :stage,           :staging
set :deploy_via,      :remote_cache
set :deploy_to,       '/web/export-at-ibm'
set :rbenv_ruby,      '2.5.3'
set :branch,          'test-qa'
set :linked_files,    %w[config/master.key]
set :linked_dirs,     %w[assets/uploads]
append :rbenv_map_bins, 'puma', 'pumactl'

# Puma configs
set :puma_threads, [4, 16]
set :puma_workers, 6
set :puma_bind,       'tcp://0.0.0.0:3007'
set :puma_state,      "#{shared_path}/tmp/pids/puma.state"
set :puma_pid,        "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.access.log"
set :puma_error_log,  "#{release_path}/log/puma.error.log"
set :puma_worker_timeout, nil
set :puma_init_active_record, false # Change to false when not using ActiveRecord

# Create puma directories
namespace :puma do
    desc 'Pre-config and Create Directories for Puma Pids and Socket'
    task :make_dirs do
        on roles(:app) do
            execute "mkdir #{shared_path}/tmp/sockets -p"
            execute "mkdir #{shared_path}/tmp/pids -p"
        end
    end

    before :start, :make_dirs
end

# Deploys tasks
namespace :deploy do
    desc 'Make sure local git is in sync with remote.'
    task :check_revision do
        on roles(:app) do
            unless `git rev-parse HEAD` == `git rev-parse origin/test-qa`
                puts 'WARNING: HEAD is not the same as origin/test-qa'
                puts 'Run `git push` to sync changes.'
                exit
            end
        end
    end

    desc 'Initial Deploy'
    task :initial do
        on roles(:app) do
            before 'deploy:restart', 'puma:start'
            invoke 'deploy'
        end
    end

    desc 'Compile static dependencies'
    task :compile_assets do
        on roles(:app) do
            execute "cd #{release_path}/ && RAILS_ENV=staging ~/.rbenv/shims/bundle exec rake assets:svg"
        end
    end

    desc 'Run rake yarn:install'
    task :yarn_install do
        on roles(:app) do
            execute "cd #{release_path} && yarn install --force"
        end
    end

    desc 'Run rake i18n_js_export'
    task :i18n_js_export do
        on roles(:app) do
            execute "cd #{release_path}/ && RAILS_ENV=staging ~/.rbenv/shims/bundle exec rake i18n:js:export"
        end
    end

    before 'compile_assets', 'deploy:i18n_js_export'
    before 'compile_assets', 'deploy:yarn_install'
    after :finishing, :compile_assets
    after :finishing, 'deploy:cleanup'
    after :finishing, :restart
end


配置/webpacker.yml

# Note: You must restart bin/webpack-dev-server for changes to take effect

default: &default
  source_path: app/javascript
  source_entry_path: .
  public_root_path: public
  public_output_path: packs
  cache_path: tmp/cache/webpacker
  check_yarn_integrity: false
  compile: true

  # Additional paths webpack should lookup modules
  # ['app/assets', 'engine/foo/app/assets']
  resolved_paths: []

  additional_paths:
    - /app/javascript

  # Reload manifest.json on all requests so we reload latest compiled packs
  cache_manifest: false

  # Extract and emit a css file
  extract_css: true

  static_assets_extensions:
    - .jpg
    - .jpeg
    - .png
    - .gif
    - .tiff
    - .ico
    - .svg
    - .eot
    - .otf
    - .ttf
    - .woff
    - .woff2

  extensions:
    - .vue
    - .mjs
    - .js
    - .sass
    - .scss
    - .css
    - .module.sass
    - .module.scss
    - .module.css
    - .png
    - .svg
    - .gif
    - .jpeg
    - .jpg
    - .vue

development:
  <<: *default

  # Verifies that versions and hashed value of the package contents in the project's package.json
  check_yarn_integrity: true
  webpack_compile_output: true

  # Reference: https://webpack.js.org/configuration/dev-server/
  dev_server:
    https: false
    host: localhost
    port: 3035
    public: localhost:3035
    hmr: false
    # Inline should be set to true if using HMR
    inline: true
    overlay: true
    compress: true
    disable_host_check: true
    use_local_ip: false
    quiet: false
    headers:
      'Access-Control-Allow-Origin': '*'
    watch_options:
      ignored: '**/node_modules/**'
    mode: development

test:
  <<: *default
  compile: false
  cache_manifest: true

  # Compile test packs to a separate directory
  public_output_path: packs-test

staging:
  <<: *default

  # Production depends on precompilation of packs prior to booting for performance.
  compile: false

  # Extract and emit a css file
  extract_css: true

  # Cache manifest.json for performance
  cache_manifest: false

production:
  <<: *default

  # Production depends on precompilation of packs prior to booting for performance.
  compile: false

  # Extract and emit a css file
  extract_css: true

  # Cache manifest.json for performance
  cache_manifest: false


谁能帮我解决这个问题?

最佳答案

在你的 config/webpacker.yml 中,resolved_paths已被弃用,应替换为 additional_paths ( https://github.com/rails/webpacker/commit/0adb3dc342addaa00122f9b022bf05606225c39c#diff-04c6e90faac2675aa89e2176d2eec7d8R365 )
我希望这会有所帮助,我遇到了同样的问题,这解决了我的问题。

关于ruby-on-rails - 无方法错误 : undefined method `+' for nil:NilClass/ 'additional_paths' [capistrano+webpacker],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62440434/

相关文章:

ruby-on-rails - 更新操作中的 Rails HABTM ActiveRecord::RecordNotFound

javascript - @babel/typescript 在 webpack 构建时不会抛出错误

javascript - 如何在构建之前使用 npm 删除文件夹

ruby-on-rails - 无法使用 zeus 的 parallel_tests 创建并行数据库

ruby-on-rails - 点击计数 rails 中的链接文章

Ruby 条件语句/案例表达式

ruby - Ym4r 无法转换为 rails3 的字符串

webpack - 使用样式组件进行 Preact 会导致浏览器控制台出现错误

ruby-on-rails - Rails 4 form_for 标签将表单操作留空

ruby-on-rails - 使用带有载波 gem 的多个 S3 存储桶