Skip to content

Commit 7aec1d6

Browse files
Fix relative paths in using directives
1 parent 228d25c commit 7aec1d6

File tree

6 files changed

+62
-36
lines changed

6 files changed

+62
-36
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package scala.build.errors
2+
3+
import scala.build.Position
4+
5+
final class ForbiddenPathReferenceError(
6+
val virtualRoot: String,
7+
val positionOpt: Option[Position]
8+
) extends BuildException(
9+
s"Can't reference paths from sources from $virtualRoot",
10+
positionOpt.toSeq
11+
)

modules/build/src/main/scala/scala/build/preprocessing/directives/Directive.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package scala.build.preprocessing.directives
22

33
import scala.build.Position
4+
import scala.build.errors.{BuildException, ForbiddenPathReferenceError}
5+
import scala.build.preprocessing.ScopePath
46

57
final case class Directive(
68
tpe: Directive.Type,
@@ -14,4 +16,12 @@ object Directive {
1416
sealed abstract class Type(val name: String) extends Product with Serializable
1517
case object Using extends Type("using")
1618
case object Require extends Type("require")
19+
20+
def osRoot(cwd: ScopePath, pos: Option[Position]): Either[BuildException, os.Path] =
21+
cwd.root match {
22+
case Left(virtualRoot) =>
23+
Left(new ForbiddenPathReferenceError(virtualRoot, pos))
24+
case Right(root) =>
25+
Right(root / cwd.path)
26+
}
1727
}
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package scala.build.preprocessing.directives
22

33
import scala.build.EitherCps.{either, value}
4-
import scala.build.Os
54
import scala.build.Position
65
import scala.build.errors.BuildException
76
import scala.build.options.{BuildOptions, ClassPathOptions}
@@ -20,13 +19,16 @@ case object UsingCustomJarDirectiveHandler extends UsingDirectiveHandler {
2019
def handle(directive: Directive, cwd: ScopePath): Option[Either[BuildException, BuildOptions]] =
2120
directive.values match {
2221
case Seq("jar" | "jars", paths @ _*) =>
23-
val paths0 = paths.map(os.Path(_, Os.pwd))
24-
val options = BuildOptions(
25-
classPathOptions = ClassPathOptions(
26-
extraClassPath = paths0
22+
val res = either {
23+
val root = value(Directive.osRoot(cwd, Some(directive.position)))
24+
val paths0 = paths.map(os.Path(_, root))
25+
BuildOptions(
26+
classPathOptions = ClassPathOptions(
27+
extraClassPath = paths0
28+
)
2729
)
28-
)
29-
Some(Right(options))
30+
}
31+
Some(res)
3032
case _ =>
3133
None
3234
}
@@ -36,20 +38,18 @@ case object UsingCustomJarDirectiveHandler extends UsingDirectiveHandler {
3638
values: Seq[Any],
3739
cwd: ScopePath,
3840
positionOpt: Option[Position]
39-
): Either[BuildException, BuildOptions] = {
41+
): Either[BuildException, BuildOptions] = either {
4042

43+
val root = value(Directive.osRoot(cwd, positionOpt))
4144
val extraJars = DirectiveUtil.stringValues(values).map { p =>
42-
// FIXME Not the right cwd
4345
// FIXME Handle malformed paths here
44-
os.Path(p, Os.pwd)
46+
os.Path(p, root)
4547
}
4648

47-
val options = BuildOptions(
49+
BuildOptions(
4850
classPathOptions = ClassPathOptions(
4951
extraClassPath = extraJars
5052
)
5153
)
52-
53-
Right(options)
5454
}
5555
}

modules/build/src/main/scala/scala/build/preprocessing/directives/UsingJavaHomeDirectiveHandler.scala

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package scala.build.preprocessing.directives
22

33
import scala.build.EitherCps.{either, value}
4-
import scala.build.Os
54
import scala.build.Position
65
import scala.build.errors.BuildException
76
import scala.build.options.{BuildOptions, JavaOptions}
@@ -19,13 +18,16 @@ case object UsingJavaHomeDirectiveHandler extends UsingDirectiveHandler {
1918
def handle(directive: Directive, cwd: ScopePath): Option[Either[BuildException, BuildOptions]] =
2019
directive.values match {
2120
case Seq("java-home" | "javaHome", path) =>
22-
val home = os.Path(path, Os.pwd) // FIXME Might throw on invalid path
23-
val options = BuildOptions(
24-
javaOptions = JavaOptions(
25-
javaHomeOpt = Some(home)
21+
val res = either {
22+
val root = value(Directive.osRoot(cwd, Some(directive.position)))
23+
val home = os.Path(path, root)
24+
BuildOptions(
25+
javaOptions = JavaOptions(
26+
javaHomeOpt = Some(home)
27+
)
2628
)
27-
)
28-
Some(Right(options))
29+
}
30+
Some(res)
2931
case _ => None
3032
}
3133

@@ -40,9 +42,9 @@ case object UsingJavaHomeDirectiveHandler extends UsingDirectiveHandler {
4042
.lastOption
4143
.toRight("No value passed to javaHome directive")
4244
}
45+
val root = value(Directive.osRoot(cwd, positionOpt))
4346
// FIXME Might throw
44-
// FIXME Wrong cwd
45-
val home = os.Path(rawHome, Os.pwd)
47+
val home = os.Path(rawHome, root)
4648
BuildOptions(
4749
javaOptions = JavaOptions(
4850
javaHomeOpt = Some(home)

modules/build/src/main/scala/scala/build/preprocessing/directives/UsingResourcesDirectiveHandler.scala

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package scala.build.preprocessing.directives
22

3-
import scala.build.Os
3+
import scala.build.EitherCps.{either, value}
44
import scala.build.Position
55
import scala.build.errors.BuildException
66
import scala.build.options.{BuildOptions, ClassPathOptions}
@@ -19,13 +19,16 @@ case object UsingResourcesDirectiveHandler extends UsingDirectiveHandler {
1919
def handle(directive: Directive, cwd: ScopePath): Option[Either[BuildException, BuildOptions]] =
2020
directive.values match {
2121
case Seq("resource" | "resources", paths @ _*) =>
22-
val paths0 = paths.map(os.Path(_, Os.pwd)) // FIXME Wrong cwd, might throw too
23-
val options = BuildOptions(
24-
classPathOptions = ClassPathOptions(
25-
extraClassPath = paths0
22+
val res = either {
23+
val root = value(Directive.osRoot(cwd, Some(directive.position)))
24+
val paths0 = paths.map(os.Path(_, root))
25+
BuildOptions(
26+
classPathOptions = ClassPathOptions(
27+
extraClassPath = paths0
28+
)
2629
)
27-
)
28-
Some(Right(options))
30+
}
31+
Some(res)
2932
case _ =>
3033
None
3134
}
@@ -35,14 +38,14 @@ case object UsingResourcesDirectiveHandler extends UsingDirectiveHandler {
3538
values: Seq[Any],
3639
cwd: ScopePath,
3740
positionOpt: Option[Position]
38-
): Either[BuildException, BuildOptions] = {
41+
): Either[BuildException, BuildOptions] = either {
42+
val root = value(Directive.osRoot(cwd, positionOpt))
3943
val paths = DirectiveUtil.stringValues(values)
40-
val paths0 = paths.map(os.Path(_, Os.pwd)) // FIXME Wrong cwd, might throw too
41-
val options = BuildOptions(
44+
val paths0 = paths.map(os.Path(_, root))
45+
BuildOptions(
4246
classPathOptions = ClassPathOptions(
4347
extraClassPath = paths0
4448
)
4549
)
46-
Right(options)
4750
}
4851
}

modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,8 +1003,8 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
10031003
val resourceContent = "Hello from resources"
10041004
TestInputs(
10051005
Seq(
1006-
os.rel / "resources" / "test" / "data" -> resourceContent,
1007-
os.rel / "Test.scala" ->
1006+
os.rel / "src" / "proj" / "resources" / "test" / "data" -> resourceContent,
1007+
os.rel / "src" / "proj" / "Test.scala" ->
10081008
s"""$directive
10091009
|object Test {
10101010
| def main(args: Array[String]): Unit = {
@@ -1020,7 +1020,7 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
10201020
}
10211021
test("resources") {
10221022
resourcesInputs().fromRoot { root =>
1023-
os.proc(TestUtil.cli, "run", ".", "--resources", "./resources").call(cwd = root)
1023+
os.proc(TestUtil.cli, "run", "src", "--resources", "./src/proj/resources").call(cwd = root)
10241024
}
10251025
}
10261026
test("resources via directive") {

0 commit comments

Comments
 (0)