Skip to content

scala -with-compiler is a no-op #15245

Open
@bishabosha

Description

@bishabosha

-with-compiler is meant to be required to run an application that depends on either scala3-staging or scala3-tasty-inspector, (or even scala3-compiler and tasty-core) and that flag magically adds it to the classpath.

as an added bonus these libraries should not be on the classpath of the compiled code either, so there should also be compilation errors for a script, not just runtime errors for an executed class/jar.

Compiler version

3.1.2

Minimized code

#! /usr/bin/env dist/target/pack/bin/scala
import scala.quoted.*

// make available the necessary compiler for runtime code generation
given staging.Compiler = staging.Compiler.make(getClass.getClassLoader)

def repeatString(times: Expr[Int], str: Expr[String])(using Quotes): Expr[String] =
  val n = times.valueOrAbort
  val msg = str.valueOrAbort
  Expr(msg * n)

@main def repeat(n: Int, msg: String): Unit =

  val result = staging.run {
    repeatString(Expr(n), Expr(msg))
  }

  println(result)

Output

$ chmod +x repeat.scala
$ ./repeat.scala 2 hello
hellohello
$

Expectation

-- [E006] Not Found Error: /Users/jamie/workspace/scripts/./repeat.scala:5:6 ---
5 |given staging.Compiler = staging.Compiler.make(getClass.getClassLoader)
  |      ^^^^^^^
  |      Not found: staging

longer explanation available when compiling with `-explain`
-- [E006] Not Found Error: /Users/jamie/workspace/scripts/./repeat.scala:5:25 --
5 |given staging.Compiler = staging.Compiler.make(getClass.getClassLoader)
  |                         ^^^^^^^
  |                         Not found: staging

longer explanation available when compiling with `-explain`
-- [E006] Not Found Error: /Users/jamie/workspace/scripts/./repeat.scala:14:15 -
14 |  val result = staging.run {
   |               ^^^^^^^
   |               Not found: staging

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions