Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
[Truffle] Fix path resolution: either absolute or relative.
* No more resolution based on unintended CWD. * Rename fileName to path since it is a full path.
- Loading branch information
Showing
with
32 additions
and 32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -58,24 +58,20 @@ public boolean require(String path, String feature, RubyNode currentNode) throws | ||
return true; | ||
} | ||
|
||
if (new File(feature).isAbsolute()) { | ||
// Try as a full path | ||
if (requireInPath(null, feature, currentNode)) { | ||
return true; | ||
} | ||
} else { | ||
// Try each load path in turn | ||
for (Object pathObject : context.getCoreLibrary().getLoadPath().slowToArray()) { | ||
final String loadPath = pathObject.toString(); | ||
|
||
if (requireInPath(loadPath, feature, currentNode)) { | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
|
||
@@ -90,31 +86,33 @@ public boolean require(String path, String feature, RubyNode currentNode) throws | ||
} | ||
|
||
private boolean requireInPath(String path, String feature, RubyNode currentNode) throws IOException { | ||
String fullPath = new File(path, feature).getPath(); | ||
|
||
if (requireFile(fullPath, currentNode)) { | ||
return true; | ||
} | ||
|
||
if (requireFile(fullPath + ".rb", currentNode)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private boolean requireFile(String path, RubyNode currentNode) throws IOException { | ||
// We expect '/' in various classpath URLs, so normalize Windows file paths to use '/' | ||
path = path.replace('\\', '/'); | ||
|
||
if (path.startsWith("uri:classloader:/")) { | ||
// TODO CS 13-Feb-15 this uri:classloader:/ and core:/ thing is a hack - simplify it | ||
|
||
for (Object loaded : Arrays.asList(context.getCoreLibrary().getLoadedFeatures().slowToArray())) { | ||
if (loaded.toString().equals(path)) { | ||
return true; | ||
} | ||
} | ||
|
||
String coreFileName = path.substring("uri:classloader:/".length()); | ||
|
||
coreFileName = FileSystems.getDefault().getPath(coreFileName).normalize().toString(); | ||
|
||
@@ -123,36 +121,38 @@ private boolean requireFile(String fileName, RubyNode currentNode) throws IOExce | ||
} | ||
|
||
context.getCoreLibrary().loadRubyCore(coreFileName, "uri:classloader:/"); | ||
context.getCoreLibrary().getLoadedFeatures().slowPush(context.makeString(path)); | ||
|
||
return true; | ||
} | ||
else if (path.startsWith("core:/")) { | ||
for (Object loaded : Arrays.asList(context.getCoreLibrary().getLoadedFeatures().slowToArray())) { | ||
if (loaded.toString().equals(path)) { | ||
return true; | ||
} | ||
} | ||
|
||
final String coreFileName = path.substring("core:/".length()); | ||
|
||
if (context.getRuntime().getLoadService().getClassPathResource(context.getRuntime().getJRubyClassLoader(), coreFileName) == null) { | ||
return false; | ||
} | ||
|
||
|
||
context.getCoreLibrary().loadRubyCore(coreFileName, "core:/"); | ||
context.getCoreLibrary().getLoadedFeatures().slowPush(context.makeString(path)); | ||
|
||
return true; | ||
} else { | ||
final File file = new File(path); | ||
|
||
assert file.isAbsolute(); | ||
|
||
if (!file.isAbsolute() || !file.isFile()) { | ||
This comment has been minimized.
This comment has been minimized.
eregon
Author
Member
|
||
return false; | ||
} | ||
|
||
final String expandedPath = RubyFile.expandPath(context, path); | ||
|
||
for (Object loaded : Arrays.asList(context.getCoreLibrary().getLoadedFeatures().slowToArray())) { | ||
if (loaded.toString().equals(expandedPath)) { | ||
@@ -163,7 +163,7 @@ else if (fileName.startsWith("core:/")) { | ||
context.getCoreLibrary().getLoadedFeatures().slowPush(context.makeString(expandedPath)); | ||
|
||
// TODO (nirvdrum 15-Jan-15): If we fail to load, we should remove the path from the loaded features because subsequent requires of the same statement may succeed. | ||
context.loadFile(path, currentNode); | ||
} | ||
|
||
return true; | ||
I think this patch makes it so that you can't
require_relative 'foo'
any more - as the path is..
and that fails theisAbsolute
test.