Skip to content

Commit

Permalink
[Truffle] Fixed loading core files on Windows.
Browse files Browse the repository at this point in the history
My previous fix for relative path names broke things on Windows. The problem is canonicalizing a path makes it fully qualified. On Linux & Mac, that's not a problem because the classpath root and the filesystem root use the same charact ('/'), but on Windows the filesystem root is 'C:\'. We need to normalize the path instead and then replace any backslashes with forward slashes to make it a valid path for the resource loader.
nirvdrum committed May 12, 2016
1 parent 9cc6f52 commit fdb2b6d
Showing 1 changed file with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -19,6 +19,8 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Locale;

public class SourceLoader {
@@ -58,20 +60,20 @@ private Source loadResource(String path) throws IOException {
}

final Class<?> relativeClass;
final String relativePath;
final Path relativePath;

if (path.startsWith(TRUFFLE_SCHEME)) {
relativeClass = RubyContext.class;
relativePath = path.substring(TRUFFLE_SCHEME.length());
relativePath = FileSystems.getDefault().getPath(path.substring(TRUFFLE_SCHEME.length()));
} else if (path.startsWith(JRUBY_SCHEME)) {
relativeClass = Ruby.class;
relativePath = path.substring(JRUBY_SCHEME.length());
relativePath = FileSystems.getDefault().getPath(path.substring(JRUBY_SCHEME.length()));
} else {
throw new UnsupportedOperationException();
}

final String canonicalPath = (new File(relativePath)).getCanonicalPath();
final InputStream stream = relativeClass.getResourceAsStream(canonicalPath);
final Path normalizedPath = relativePath.normalize();
final InputStream stream = relativeClass.getResourceAsStream(normalizedPath.toString().replace('\\', '/'));

if (stream == null) {
throw new FileNotFoundException(path);

0 comments on commit fdb2b6d

Please sign in to comment.