Skip to content

Commit c9533da

Browse files
committed
Fix #9783: Scala.js: Implement the -scalajs-mapSourceURI option.
We apply it to the scala3-library-bootstrappedJS project, so that source maps can correctly work for the Scala 3 library. Additionally, it serves as a smoke test.
1 parent 8f42062 commit c9533da

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

compiler/src/dotty/tools/backend/sjs/JSPositions.scala

+32-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
11
package dotty.tools.backend.sjs
22

3+
import java.net.{URI, URISyntaxException}
4+
35
import dotty.tools.dotc.core._
46
import Contexts._
57

8+
import dotty.tools.dotc.report
9+
610
import dotty.tools.dotc.util.{SourceFile, SourcePosition}
711
import dotty.tools.dotc.util.Spans.Span
812

913
import org.scalajs.ir
1014

1115
/** Conversion utilities from dotty Positions to IR Positions. */
1216
class JSPositions()(using Context) {
17+
import JSPositions._
18+
19+
private val sourceURIMaps: List[URIMap] = {
20+
ctx.settings.scalajsMapSourceURI.value.flatMap { option =>
21+
val uris = option.split("->")
22+
if (uris.length != 1 && uris.length != 2) {
23+
report.error("-scalajs-mapSourceURI needs one or two URIs as argument (separated by '->').")
24+
Nil
25+
} else {
26+
try {
27+
val from = new URI(uris.head)
28+
val to = uris.lift(1).map(str => new URI(str))
29+
URIMap(from, to) :: Nil
30+
} catch {
31+
case e: URISyntaxException =>
32+
report.error(s"${e.getInput} is not a valid URI")
33+
Nil
34+
}
35+
}
36+
}
37+
}
1338

1439
private def sourceAndSpan2irPos(source: SourceFile, span: Span): ir.Position = {
1540
if (!span.exists) ir.Position.NoPosition
@@ -59,16 +84,16 @@ class JSPositions()(using Context) {
5984
)
6085
case file =>
6186
val srcURI = file.toURI
62-
def matches(pat: java.net.URI) = pat.relativize(srcURI) != srcURI
63-
64-
// TODO
65-
/*scalaJSOpts.sourceURIMaps.collectFirst {
66-
case ScalaJSOptions.URIMap(from, to) if matches(from) =>
87+
sourceURIMaps.collectFirst {
88+
case URIMap(from, to) if from.relativize(srcURI) != srcURI =>
6789
val relURI = from.relativize(srcURI)
6890
to.fold(relURI)(_.resolve(relURI))
69-
} getOrElse*/
70-
srcURI
91+
}.getOrElse(srcURI)
7192
}
7293
}
7394
}
7495
}
96+
97+
object JSPositions {
98+
final case class URIMap(from: URI, to: Option[URI])
99+
}

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class ScalaSettings extends Settings.SettingGroup {
6767

6868
/** Scala.js-related settings */
6969
val scalajsGenStaticForwardersForNonTopLevelObjects: Setting[Boolean] = BooleanSetting("-scalajs-genStaticForwardersForNonTopLevelObjects", "Generate static forwarders even for non-top-level objects (Scala.js only)")
70+
val scalajsMapSourceURI: Setting[List[String]] = MultiStringSetting("-scalajs-mapSourceURI", "uri1[->uri2]", "rebases source URIs from uri1 to uri2 (or to a relative URI) for source maps (Scala.js only)")
7071

7172
/** -X "Advanced" settings
7273
*/

project/Build.scala

+12
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ object Build {
7878

7979
val dottyOrganization = "org.scala-lang"
8080
val dottyGithubUrl = "https://github.com/lampepfl/dotty"
81+
val dottyGithubRawUserContentUrl = "https://raw.githubusercontent.com/lampepfl/dotty"
8182

8283

8384
val isRelease = sys.env.get("RELEASEBUILD") == Some("yes")
@@ -773,6 +774,17 @@ object Build {
773774
unmanagedSourceDirectories in Compile :=
774775
(unmanagedSourceDirectories in (`scala3-library-bootstrapped`, Compile)).value,
775776

777+
// Configure the source maps to point to GitHub for releases
778+
scalacOptions ++= {
779+
if (isRelease) {
780+
val baseURI = (baseDirectory in LocalRootProject).value.toURI
781+
val dottyVersion = version.value
782+
Seq(s"-scalajs-mapSourceURI:$baseURI->$dottyGithubRawUserContentUrl/v$dottyVersion/")
783+
} else {
784+
Nil
785+
}
786+
},
787+
776788
// Make sure `scala3-bootstrapped/test` doesn't fail on this project for no reason
777789
test in Test := {},
778790
testOnly in Test := {},

0 commit comments

Comments
 (0)