Skip to content

Normalize slashes in paths in TASTy binaries #10678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion compiler/src/dotty/tools/dotc/util/SourceFile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,15 @@ object SourceFile {
// As we already check that the prefix matches, the special handling for
// Windows is not needed.

refPath.relativize(sourcePath).toString
// We also consistently use forward slashes as path element separators
// for relative paths. If we didn't do that, it'd be impossible to parse
// them back, as one would need to know whether they were created on Windows
// and use both slashes as separators, or on other OS and use forward slash
// as separator, backslash as file name character.

import scala.jdk.CollectionConverters._
val path = refPath.relativize(sourcePath)
path.iterator.asScala.mkString("/")
else
sourcePath.toString
}
Expand Down
76 changes: 76 additions & 0 deletions compiler/test/dotty/tools/dotc/core/tasty/PathPicklingTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package dotty.tools.dotc.core.tasty

import java.io.{File => JFile, ByteArrayOutputStream, IOException}
import java.nio.file.{Files, NoSuchFileException, Path, Paths}

import scala.sys.process._

import org.junit.Test
import org.junit.Assert.{assertEquals, assertTrue, assertFalse, fail}

import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.ast.tpd.TreeOps
import dotty.tools.dotc.{Driver, Main}
import dotty.tools.dotc.decompiler
import dotty.tools.dotc.core.Comments.CommentsContext
import dotty.tools.dotc.core.Contexts.Context
import dotty.tools.dotc.core.Decorators.{toTermName, toTypeName}
import dotty.tools.dotc.core.Mode
import dotty.tools.dotc.core.Names.Name
import dotty.tools.dotc.interfaces.Diagnostic.ERROR
import dotty.tools.dotc.reporting.TestReporter
import dotty.tools.io.{Directory, File, Path}

import dotty.tools.vulpix.TestConfiguration

class PathPicklingTest {

@Test def test(): Unit = {
val out = JFile("out/testPathPickling")
val cwd = JFile("").getAbsolutePath()
delete(out)
out.mkdir()

locally {
val ignorantProcessLogger = ProcessLogger(_ => ())
val options = TestConfiguration.defaultOptions
.and("-d", s"$out/out.jar")
.and("-sourceroot", "tests/pos")
.and(s"$cwd/tests/pos/i10430/lib.scala", s"$cwd/tests/pos/i10430/app.scala")
val reporter = TestReporter.reporter(System.out, logLevel = ERROR)
val rep = Main.process(options.all, reporter)
assertFalse("Compilation failed.", rep.hasErrors)
}

val decompiled =
val outstream = new ByteArrayOutputStream()
val options = TestConfiguration.defaultOptions
.and("-print-tasty")
.and("-color:never")
.and(s"$out/out.jar")
val reporter = TestReporter.reporter(System.out, logLevel = ERROR)
val rep = Console.withOut(outstream) {
decompiler.Main.process(options.all, reporter)
}
assertFalse("Decompilation failed.", rep.hasErrors)
new String(outstream.toByteArray(), "UTF-8")

assertTrue(decompiled.contains(": i10430/lib.scala"))
assertTrue(decompiled.contains(": i10430/app.scala"))
assertTrue(decompiled.contains("[i10430/lib.scala]"))
assertTrue(decompiled.contains("[i10430/app.scala]"))

assertFalse(decompiled.contains(": i10430\\lib.scala"))
assertFalse(decompiled.contains(": i10430\\app.scala"))
assertFalse(decompiled.contains("[i10430\\lib.scala]"))
assertFalse(decompiled.contains("[i10430\\app.scala]"))
}

private def delete(file: JFile): Unit = {
if (file.isDirectory) file.listFiles.foreach(delete)
try Files.delete(file.toPath)
catch {
case _: NoSuchFileException => // already deleted, everything's fine
}
}
}