Skip to content

Commit 782adbb

Browse files
Merge pull request #10678 from dotty-staging/normalize-tasty-path-slash
Normalize slashes in paths in TASTy binaries
2 parents d61d7f9 + 0c17ff4 commit 782adbb

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

compiler/src/dotty/tools/dotc/util/SourceFile.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,15 @@ object SourceFile {
233233
// As we already check that the prefix matches, the special handling for
234234
// Windows is not needed.
235235

236-
refPath.relativize(sourcePath).toString
236+
// We also consistently use forward slashes as path element separators
237+
// for relative paths. If we didn't do that, it'd be impossible to parse
238+
// them back, as one would need to know whether they were created on Windows
239+
// and use both slashes as separators, or on other OS and use forward slash
240+
// as separator, backslash as file name character.
241+
242+
import scala.jdk.CollectionConverters._
243+
val path = refPath.relativize(sourcePath)
244+
path.iterator.asScala.mkString("/")
237245
else
238246
sourcePath.toString
239247
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package dotty.tools.dotc.core.tasty
2+
3+
import java.io.{File => JFile, ByteArrayOutputStream, IOException}
4+
import java.nio.file.{Files, NoSuchFileException, Path, Paths}
5+
6+
import scala.sys.process._
7+
8+
import org.junit.Test
9+
import org.junit.Assert.{assertEquals, assertTrue, assertFalse, fail}
10+
11+
import dotty.tools.dotc.ast.tpd
12+
import dotty.tools.dotc.ast.tpd.TreeOps
13+
import dotty.tools.dotc.{Driver, Main}
14+
import dotty.tools.dotc.decompiler
15+
import dotty.tools.dotc.core.Comments.CommentsContext
16+
import dotty.tools.dotc.core.Contexts.Context
17+
import dotty.tools.dotc.core.Decorators.{toTermName, toTypeName}
18+
import dotty.tools.dotc.core.Mode
19+
import dotty.tools.dotc.core.Names.Name
20+
import dotty.tools.dotc.interfaces.Diagnostic.ERROR
21+
import dotty.tools.dotc.reporting.TestReporter
22+
import dotty.tools.io.{Directory, File, Path}
23+
24+
import dotty.tools.vulpix.TestConfiguration
25+
26+
class PathPicklingTest {
27+
28+
@Test def test(): Unit = {
29+
val out = JFile("out/testPathPickling")
30+
val cwd = JFile("").getAbsolutePath()
31+
delete(out)
32+
out.mkdir()
33+
34+
locally {
35+
val ignorantProcessLogger = ProcessLogger(_ => ())
36+
val options = TestConfiguration.defaultOptions
37+
.and("-d", s"$out/out.jar")
38+
.and("-sourceroot", "tests/pos")
39+
.and(s"$cwd/tests/pos/i10430/lib.scala", s"$cwd/tests/pos/i10430/app.scala")
40+
val reporter = TestReporter.reporter(System.out, logLevel = ERROR)
41+
val rep = Main.process(options.all, reporter)
42+
assertFalse("Compilation failed.", rep.hasErrors)
43+
}
44+
45+
val decompiled =
46+
val outstream = new ByteArrayOutputStream()
47+
val options = TestConfiguration.defaultOptions
48+
.and("-print-tasty")
49+
.and("-color:never")
50+
.and(s"$out/out.jar")
51+
val reporter = TestReporter.reporter(System.out, logLevel = ERROR)
52+
val rep = Console.withOut(outstream) {
53+
decompiler.Main.process(options.all, reporter)
54+
}
55+
assertFalse("Decompilation failed.", rep.hasErrors)
56+
new String(outstream.toByteArray(), "UTF-8")
57+
58+
assertTrue(decompiled.contains(": i10430/lib.scala"))
59+
assertTrue(decompiled.contains(": i10430/app.scala"))
60+
assertTrue(decompiled.contains("[i10430/lib.scala]"))
61+
assertTrue(decompiled.contains("[i10430/app.scala]"))
62+
63+
assertFalse(decompiled.contains(": i10430\\lib.scala"))
64+
assertFalse(decompiled.contains(": i10430\\app.scala"))
65+
assertFalse(decompiled.contains("[i10430\\lib.scala]"))
66+
assertFalse(decompiled.contains("[i10430\\app.scala]"))
67+
}
68+
69+
private def delete(file: JFile): Unit = {
70+
if (file.isDirectory) file.listFiles.foreach(delete)
71+
try Files.delete(file.toPath)
72+
catch {
73+
case _: NoSuchFileException => // already deleted, everything's fine
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)