Skip to content

Fix #9783: Scala.js: Implement the -scalajs-mapSourceURI option. #10439

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 1 commit into from
Nov 22, 2020
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
39 changes: 32 additions & 7 deletions compiler/src/dotty/tools/backend/sjs/JSPositions.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,40 @@
package dotty.tools.backend.sjs

import java.net.{URI, URISyntaxException}

import dotty.tools.dotc.core._
import Contexts._

import dotty.tools.dotc.report

import dotty.tools.dotc.util.{SourceFile, SourcePosition}
import dotty.tools.dotc.util.Spans.Span

import org.scalajs.ir

/** Conversion utilities from dotty Positions to IR Positions. */
class JSPositions()(using Context) {
import JSPositions._

private val sourceURIMaps: List[URIMap] = {
ctx.settings.scalajsMapSourceURI.value.flatMap { option =>
val uris = option.split("->")
if (uris.length != 1 && uris.length != 2) {
report.error("-scalajs-mapSourceURI needs one or two URIs as argument (separated by '->').")
Nil
} else {
try {
val from = new URI(uris.head)
val to = uris.lift(1).map(str => new URI(str))
URIMap(from, to) :: Nil
} catch {
case e: URISyntaxException =>
report.error(s"${e.getInput} is not a valid URI")
Nil
}
}
}
}

private def sourceAndSpan2irPos(source: SourceFile, span: Span): ir.Position = {
if (!span.exists) ir.Position.NoPosition
Expand Down Expand Up @@ -59,16 +84,16 @@ class JSPositions()(using Context) {
)
case file =>
val srcURI = file.toURI
def matches(pat: java.net.URI) = pat.relativize(srcURI) != srcURI

// TODO
/*scalaJSOpts.sourceURIMaps.collectFirst {
case ScalaJSOptions.URIMap(from, to) if matches(from) =>
sourceURIMaps.collectFirst {
case URIMap(from, to) if from.relativize(srcURI) != srcURI =>
val relURI = from.relativize(srcURI)
to.fold(relURI)(_.resolve(relURI))
} getOrElse*/
srcURI
}.getOrElse(srcURI)
}
}
}
}

object JSPositions {
final case class URIMap(from: URI, to: Option[URI])
}
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/config/ScalaSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class ScalaSettings extends Settings.SettingGroup {

/** Scala.js-related settings */
val scalajsGenStaticForwardersForNonTopLevelObjects: Setting[Boolean] = BooleanSetting("-scalajs-genStaticForwardersForNonTopLevelObjects", "Generate static forwarders even for non-top-level objects (Scala.js only)")
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)")
Copy link
Member

@smarter smarter Nov 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To simplify cross-compilation and not have to update the existing documentation, would it make sense to make -P:scalajs:foo an alias of -scalajs-foo ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would we do that? -P is a generic system to pass options to a plugin. We would have to special case scalajs in several places.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't looked at how -P works so I can't say, but it seems like even if it's a bit hacky it would be worth it, but I don't know, your choice!


/** -X "Advanced" settings
*/
Expand Down
12 changes: 12 additions & 0 deletions project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ object Build {

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


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

// Configure the source maps to point to GitHub for releases
scalacOptions ++= {
if (isRelease) {
val baseURI = (baseDirectory in LocalRootProject).value.toURI
val dottyVersion = version.value
Seq(s"-scalajs-mapSourceURI:$baseURI->$dottyGithubRawUserContentUrl/v$dottyVersion/")
} else {
Nil
}
},

// Make sure `scala3-bootstrapped/test` doesn't fail on this project for no reason
test in Test := {},
testOnly in Test := {},
Expand Down