Skip to content

Commit 819f684

Browse files
Allow to use --from-path with absolute paths (#16)
* Add test case for using --from path outside current classpath * Allow to use absolute path with --from-path option
1 parent 64cc7c9 commit 819f684

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

cli/src/main/scala/scala/scalanative/cli/ScalaNativeP.scala

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ object ScalaNativeP {
106106
relativeInJar == regularPath.toString()
107107
}
108108
@tailrec
109-
def findAndRead(
109+
def findInClasspathAndRead(
110110
classpath: Stream[VirtualDirectory],
111111
path: Path
112112
): Option[ByteBuffer] = {
@@ -116,15 +116,31 @@ object ScalaNativeP {
116116
val found = dir.files
117117
.find(virtualDirPathMatches(_, path))
118118
.map(dir.read(_))
119-
if (found.isEmpty) findAndRead(tail, path)
119+
if (found.isEmpty) findInClasspathAndRead(tail, path)
120120
else found
121121
}
122122
}
123+
124+
def tryReadFromPath(path: Path): Option[ByteBuffer] = {
125+
val file = path.toFile()
126+
val absPath = path.toAbsolutePath()
127+
// When classpath is explicitly provided don't try to read directly
128+
if (!options.usingDefaultClassPath || !file.exists()) None
129+
else
130+
util.Try {
131+
VirtualDirectory
132+
.real(absPath.getParent())
133+
.read(absPath.getFileName())
134+
}.toOption
135+
}
136+
123137
for {
124138
fileName <- options.classNames
125139
path = Paths.get(fileName).normalize()
126-
content <- findAndRead(cp, path)
127-
.orElse(fail(s"Not found file with name `${fileName}`"))
140+
content <-
141+
tryReadFromPath(path)
142+
.orElse(findInClasspathAndRead(cp, path))
143+
.orElse(fail(s"Not found file with name `${fileName}`"))
128144
} {
129145
val defns = deserializeBinary(content, fileName)
130146
printNIR(defns, options.verbose)

cli/src/sbt-test/integration/standalone/test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ $ exists Foo$.nir
1515

1616
-> runScript scala-native-p --from-path notExisting.nir
1717

18+
# Move nir file to directory outside classpath
19+
$ mkdir ../scala-native-test/
20+
$ copy-file Foo.nir ../scala-native-test//Foo2.nir
21+
$ exists ../scala-native-test/Foo2.nir
22+
> runScript scala-native-p --from-path ../scala-native-test/Foo2.nir
23+
1824
> runExec jar cf inside.jar Foo.class Foo.nir Foo$.class Foo$.nir
1925
> runScript scala-native-p --cp inside.jar --from-path Foo.nir Foo$.nir
2026
-> runScript scala-native-p --cp inside.jar --from-path Main$.nir

0 commit comments

Comments
 (0)