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

__FILE__ is wrong when using PathType.CLASSPATH so require_relative can't work #1875

Closed
hakanai opened this issue Jul 31, 2014 · 2 comments
Closed
Assignees
Milestone

Comments

@hakanai
Copy link

hakanai commented Jul 31, 2014

If I load a file from the classpath like this:

private Structure createStructure() throws Exception {
    ScriptingContainer container = new ScriptingContainer();
    container.runScriptlet(PathType.CLASSPATH, "org/acme/app/scripts/dsl.rb");

    // ...
}

And then this script requires other scripts:

puts __FILE__
require_relative 'drop_context'

This prints out "org/acme/app/scripts/dsl.rb" even though I would expect "classpath:/org/acme/app/scripts/dsl.rb", so require_relative is doomed to fail as the file is not on the filesystem (and indeed this does fail - it resolves the path relative to my home directory.)

I tried working around this by passing PathType.ABSOLUTE and "classpath:/org/.../dsl.rb" instead, but this doesn't work - ScriptingContainer loads this script directly, so it doesn't know that classpath is a special URL.

I'm not sure what the workaround is from here. I guess the worst case is that I accept that require_relative can't possibly work and use require with absolute paths.

@hakanai
Copy link
Author

hakanai commented Aug 3, 2014

The workaround I found is to use runScriptlet(InputStream, String), passing the proper path:

        try (InputStream resource = getClass().getResourceAsStream("dsl.rb")) {
            container.runScriptlet(resource, "classpath:/org/acme/app/scripts/dsl.rb");
        }

@headius
Copy link
Member

headius commented Dec 4, 2014

I have a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants