Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: load cannot be invoked on a path unless require has been called on it first #2629

Open
AndyObtiva opened this issue Jan 10, 2024 · 0 comments
Labels

Comments

@AndyObtiva
Copy link

AndyObtiva commented Jan 10, 2024

Describe the bug

I cannot use load to load files from a gem unless I have called require on them first. This differs from Ruby behavior where I can call load on any Ruby file the first time.

Opal version: 1.8.2

To Reproduce

Code like this does not work if the file hasn't been required.

load 'path/to/unrequired_file.rb'

For example, if I replace the contents of this file https://github.com/AndyObtiva/sample-glimmer-dsl-web-rails7-app/blob/master/app/assets/opal/sample_selector.rb with the code below, it does not work:

require 'opal'

require 'glimmer-dsl-web' # does not load samples
load 'glimmer-dsl-web/samples/hello/hello_world.rb'

It fails with an error:

runtime.source.js:1958 Uncaught LoadError: cannot load such file -- glimmer-dsl-web/samples/hello/hello_world
    at Opal.send2 (runtime.source.js:1958:1)
    at Opal.send (runtime.source.js:1946:1)
    at Function.$$exception (error.source.rb:33:5)
    at Function.$$raise (kernel.source.rb:669:32)
    at $raise (runtime.source.js:146:1)
    at Opal.load_normalized (runtime.source.js:2764:1)
    at Opal.load (runtime.source.js:2777:1)
    at klass.$$load (kernel.source.rb:587:6)
    at $$7 (sample_selector.source.rb:23:19)
    at Opal.yieldX (runtime.source.js:1734:1)
    at $call_proc (proc.source.rb:45:1)
    at Function.$$call (proc.source.rb:54:1)
    at $$1 (listener_proxy.source.rb:38:45)
    at HTMLAnchorElement.wrapper (element.source.rb:683:1)
    at HTMLAnchorElement.dispatch (jquery.source.js:5430:1)
    at elemData.handle (jquery.source.js:5234:1)

If I do this, it works:

require 'path/to/unrequired_file.rb'
load 'path/to/unrequired_file.rb'

Like updating the example above with this code:

require 'opal'

require 'glimmer-dsl-web' # does not load samples
require 'glimmer-dsl-web/samples/hello/hello_world.rb'
load 'glimmer-dsl-web/samples/hello/hello_world.rb'

This breaks Ruby expectations because in Ruby, I can load a file the very first time without having required the file first. This enables me to build an app that dynamically loads files based on user choices. I use this approach in desktop sample runner apps, and I tried using the same approach in this Opal app: https://github.com/AndyObtiva/sample-glimmer-dsl-web-rails7-app . Right now, because of the error I get, I am forced to track if a file has been required first, and if not required, then I call require the first time. Afterwards, I call load on subsequent times.

I think this needs to be fixed to give us the same behavior we expect from Ruby.

@AndyObtiva AndyObtiva added the bug label Jan 10, 2024
@AndyObtiva AndyObtiva changed the title Bug: load cannot be invoked on a path unless require has been on it first Bug: load cannot be invoked on a path unless require has been called on it first Jan 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant