scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
230 stars 21 forks source link

Issue with macros and in-memory (virtual) source files #13010

Closed vasilmkd closed 1 week ago

vasilmkd commented 1 week ago

I opened the same ticket in the Scala 3 repo.

https://github.com/scala/scala3/issues/20591

The issue affects all versions of Scala, and the implementation details are relevant to both Scala 2 and Scala 3, as the sources in question have been transferred from Scala 2 to Scala 3 over the years.

Thanks in advance.

vasilmkd commented 1 week ago

To be clear, the issue is not immediately reproducible with MUnit and Scala 2. But the possibility of the same issue exists. Let's see how the discussion in the Scala 3 repo unfolds.

som-snytt commented 1 week ago

The disclaimer was always:

This library is considered experimental and should not be used unless you know what you are doing.

Demonstrate that sourcecode and munit for Scala 2 both use path for source path (and not file):

//> using scala 2.13
//> using options -Wnonunit-statement -Xlint -Xsource:3-cross
//> using dep com.lihaoyi::sourcecode:0.4.2
//> using dep com.lihaoyi::os-lib:0.10.2
//> using dep org.scalameta::munit:1.0.0
//> using dep org.scala-lang:scala-compiler:2.13.14

import java.io.{InputStreamReader, PrintStream, StringWriter}

import scala.reflect.io.VirtualFile
import scala.sys.process.*
import scala.tools.nsc.{Global, Settings}
import scala.util.Using
import scala.util.chaining.*

object Test extends App {
  val tmp = os.temp.dir()
  val s = new Settings().tap(_.processArgumentString(s"-usejavacp -d $tmp -Xlint"))
  val g = new Global(s)
  val f = new VirtualFile(name="vf", path="/p/vf")
  def code =
    s"""
     |object Test extends App {
     |  println {
     |    s"hello, new world at $${sourcecode.File()}"
     |  }
     |  println(new LocationTestSuite().munitTests().map(_.body()))
     |}
     |
     |class LocationTestSuite extends munit.FunSuite {
     |  test("location") {
     |    assert(true)
     |  }
     |}
    """.stripMargin
  Using.resource(new PrintStream(f.output))(_.print(code))
  val txt = Using.resource(new InputStreamReader(f.input)) { reader =>
    val w = new StringWriter
    reader.transferTo(w)
    w.toString
  }
  assert(txt == code)
  println {
    s"hello, world at ${sourcecode.File()}"
  }
  val r = new g.Run
  r.compileFiles(List(f))
  assert(!g.reporter.hasErrors)
  val cp = g.classPath.asClassPathStrings.filter(_.endsWith(".jar")).mkString(":") + s":$tmp"
  println {
    Seq("scala", "-cp", cp, "Test").!!
  }
}