Opal ruby Rake任务失败,出现错误FrozenError: 无法修改冻结的Array。

huangapple go评论53阅读模式
英文:

Opal ruby Rake task failing with error FrozenError: can't modify frozen Array

问题

在我的Web应用程序中,我正在尝试使用Opal来编写JavaScript。

ruby-3.1.2
opal-1.7.3
opal-jquery 0.4.6

我正在使用guard-rake自动编译对Opal文件所做的任何更改。以下是相关的代码片段:

Rakefile

require 'opal'
require 'opal-jquery'

namespace :opal do
  desc "Compile Opal files"
  task :compile do
    Opal.append_path "app/assets/opal"

    builder = Opal::Builder.new
    builder.build('opal')
    builder.build('opal-jquery')
    builder.build("main")

    File.binwrite "app/assets/js/opal-main.js", builder.to_s
  end
end

Guardfile

group :server do
  guard "puma", port: 2300 do
    watch(%r{config/*})
    watch(%r{lib/*})
    watch(%r{app/*})
  end

  guard 'rake', :task => 'opal:compile' do
    watch(%r{^app/assets/opal/.+\.rb})
  end
end

当我启动服务器时,rake任务成功运行,编译也成功。服务器日志如下所示:

15:31:07 - INFO - Using Guardfile at /.../myapp/Guardfile.
15:31:07 - INFO - Puma starting on port 2300 in development environment.
15:31:07 - INFO - Starting guard-rake opal:compile
[23559] Puma starting in cluster mode...
[23559] * Puma version: 6.2.2 (ruby 3.1.2-p20) ("Speaking of Now")
[23559] *  Min threads: 5
[23559] *  Max threads: 5
[23559] *  Environment: development
[23559] *   Master PID: 23559
[23559] *      Workers: 2
[23559] *     Restarts: (✔) hot (✖) phased
[23559] * Preloading application
15:31:08 - INFO - running opal:compile
15:31:08 - INFO - Guard is now watching at '/.../myapp'
[23559] * Listening on http://0.0.0.0:2300
[23559] Use Ctrl-C to stop
[23559] * Starting control server on http://127.0.0.1:9293
[23559] * Starting control server on http://[::1]:9293
[23559] - Worker 0 (PID: 23579) booted in 0.0s, phase: 0
[23559] - Worker 1 (PID: 23581) booted in 0.0s, phase: 0

然后,当我对添加到Opal的app/assets/opal路径下的任何文件进行更改时,guard-rake运行任务重新编译该路径下的Opal文件,但在服务器日志中我看到以下错误:

15:32:13 - ERROR - Guard::Rake failed to run rake task <opal:compile>, exception was:
> [#]   FrozenError: can't modify frozen Array: ["/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/opal-1.7.3/opal", "/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/opal-1.7.3/stdlib", "/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/opal-1.7.3/lib", "/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/ast-2.4.2/lib", "/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/ast-2.4.2/lib", "/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/parser-3.2.2.1/lib", "/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/opal-jquery-0.4.6/lib", "app/assets/opal"]

完整的日志如下:

15:32:13 - INFO - Restarting Puma...
[23559] - Gracefully shutting down workers...
15:32:13 - INFO - Puma restarted
15:32:13 - INFO - running opal:compile
15:32:13 - ERROR - Guard::Rake failed to run rake task <opal:compile>, exception was:
> [#] 	FrozenError: can't modify frozen Array: ["/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/opal-1.7.3/opal", "/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/opal-1.7.3/stdlib", "/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/opal-1.7.3/lib", "/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/ast-2.4.2/lib", "/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/ast-2.4.2/lib", "/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/parser-3.2.2.1/lib", "/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/opal-jquery-0.4.6/lib", "app/assets/opal"]
[23559] * Restarting...
[23559] Puma starting in cluster mode...
[23559] * Puma version: 6.2.2 (ruby 3.1.2-p20) ("Speaking of Now")
[23559] *  Min threads: 5
[23559] *  Max threads: 5
[23559] *  Environment: development
[23559] *   Master PID: 23559
[23559] *      Workers: 2
[23559] *     Restarts: (✔) hot (✖) phased
[23559] * Preloading application
[23559] * Inherited tcp://0.0.0.0:2300
[23559] Use Ctrl-C to stop
[23559] * Starting control server on http://127.0.0.1:9293
[235

<details>
<summary>英文:</summary>

In my web application I am trying to use [Opal](https://opalrb.com/) to program Javascript.

    ruby-3.1.2
    opal-1.7.3
    opal-jquery 0.4.6

I am using `guard-rake` to automatically compile any changes made to my opal files. Following are the relevant code snippets:

**Rakefile**

    require &#39;opal&#39;
    require &#39;opal-jquery&#39;
    
    namespace :opal do
      desc &quot;Compile Opal files&quot;
      task :compile do
        Opal.append_path &quot;app/assets/opal&quot;
    
        builder = Opal::Builder.new
        builder.build(&#39;opal&#39;)
        builder.build(&#39;opal-jquery&#39;)
        builder.build(&quot;main&quot;)
    
        File.binwrite &quot;app/assets/js/opal-main.js&quot;, builder.to_s
      end
    end

**Guardfile**

    group :server do
      guard &quot;puma&quot;, port: 2300 do
        watch(%r{config/*})
        watch(%r{lib/*})
        watch(%r{app/*})
      end
    
      guard &#39;rake&#39;, :task =&gt; &#39;opal:compile&#39; do
        watch(%r{^app/assets/opal/.+\.rb})
      end
    end

When I start the server the rake task runs successfully and the compilation also succeeds. The server logs are shown below

    15:31:07 - INFO - Using Guardfile at /.../myapp/Guardfile.
    15:31:07 - INFO - Puma starting on port 2300 in development environment.
    15:31:07 - INFO - Starting guard-rake opal:compile
    [23559] Puma starting in cluster mode...
    [23559] * Puma version: 6.2.2 (ruby 3.1.2-p20) (&quot;Speaking of Now&quot;)
    [23559] *  Min threads: 5
    [23559] *  Max threads: 5
    [23559] *  Environment: development
    [23559] *   Master PID: 23559
    [23559] *      Workers: 2
    [23559] *     Restarts: (✔) hot (✖) phased
    [23559] * Preloading application
    15:31:08 - INFO - running opal:compile
    15:31:08 - INFO - Guard is now watching at &#39;/.../myapp&#39;
    [23559] * Listening on http://0.0.0.0:2300
    [23559] Use Ctrl-C to stop
    [23559] * Starting control server on http://127.0.0.1:9293
    [23559] * Starting control server on http://[::1]:9293
    [23559] - Worker 0 (PID: 23579) booted in 0.0s, phase: 0
    [23559] - Worker 1 (PID: 23581) booted in 0.0s, phase: 0

Then when I make changes to any file under path `app/assets/opal` appended to Opal, `guard-rake` runs the task to recompile the Opal files under that path, but in the server logs I see following error

    15:32:13 - ERROR - Guard::Rake failed to run rake task &lt;opal:compile&gt;, exception was:
    &gt; [#]   FrozenError: can&#39;t modify frozen Array: [&quot;/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/opal-1.7.3/opal&quot;, &quot;/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/opal-1.7.3/stdlib&quot;, &quot;/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/opal-1.7.3/lib&quot;, &quot;/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/ast-2.4.2/lib&quot;, &quot;/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/ast-2.4.2/lib&quot;, &quot;/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/parser-3.2.2.1/lib&quot;, &quot;/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/opal-jquery-0.4.6/lib&quot;, &quot;app/assets/opal&quot;]

Full logs are shown below

    15:32:13 - INFO - Restarting Puma...
    [23559] - Gracefully shutting down workers...
    15:32:13 - INFO - Puma restarted
    15:32:13 - INFO - running opal:compile
    15:32:13 - ERROR - Guard::Rake failed to run rake task &lt;opal:compile&gt;, exception was:
    &gt; [#] 	FrozenError: can&#39;t modify frozen Array: [&quot;/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/opal-1.7.3/opal&quot;, &quot;/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/opal-1.7.3/stdlib&quot;, &quot;/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/opal-1.7.3/lib&quot;, &quot;/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/ast-2.4.2/lib&quot;, &quot;/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/ast-2.4.2/lib&quot;, &quot;/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/parser-3.2.2.1/lib&quot;, &quot;/home/jignesh/.rvm/gems/ruby-3.1.2@myapp/gems/opal-jquery-0.4.6/lib&quot;, &quot;app/assets/opal&quot;]
    [23559] * Restarting...
    [23559] Puma starting in cluster mode...
    [23559] * Puma version: 6.2.2 (ruby 3.1.2-p20) (&quot;Speaking of Now&quot;)
    [23559] *  Min threads: 5
    [23559] *  Max threads: 5
    [23559] *  Environment: development
    [23559] *   Master PID: 23559
    [23559] *      Workers: 2
    [23559] *     Restarts: (✔) hot (✖) phased
    [23559] * Preloading application
    [23559] * Inherited tcp://0.0.0.0:2300
    [23559] Use Ctrl-C to stop
    [23559] * Starting control server on http://127.0.0.1:9293
    [23559] * Starting control server on http://[::1]:9293
    [23559] - Worker 0 (PID: 23926) booted in 0.0s, phase: 0
    [23559] - Worker 1 (PID: 23928) booted in 0.0s, phase: 0

Can anybody please help in figuring out what is the issue causing the rake task fail? 

Is there any problem in how I have defined the rake task? 

Note: I am novice to Opal so please bear with any mistakes which looks silly to the experts.

Thanks.



</details>


# 答案1
**得分**: 1

以下是翻译好的部分:

原因很简单。

在编译之后,无法执行 `Opal.append_path "app/assets/opal"`。如果任务多次执行,可能会发生这种情况,这似乎是一个情况。您需要将该行移到 rake 块之外。

另外,我建议使用 `opal-browser` 而不是 `opal-jquery`。

<details>
<summary>英文:</summary>

The reason for that is pretty simple.

You can&#39;t execute `Opal.append_path &quot;app/assets/opal&quot;` after the compilation. Which may happen, if the task is executed multiple times, which appears to be a case. You need to move that line outside of the rake blocks.

Of another thing, I would recommend to use `opal-browser` instead of `opal-jquery`.

</details>



huangapple
  • 本文由 发表于 2023年5月10日 18:18:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217253.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定