sirthias / borer

Efficient CBOR and JSON (de)serialization in Scala
https://sirthias.github.io/borer/
Mozilla Public License 2.0
220 stars 13 forks source link

Source maps should point to github #684

Closed raquo closed 8 months ago

raquo commented 9 months ago

When publishing libraries to Scala.js we're distributing SJSIR files that contain your local paths to the source files. Those paths are used in JS sourcemaps, e.g. to show Scala code in the browser debugger.

The problem is, your local paths don't exist on users' computers, so currently Borer sourcemaps are not possible to resolve:

Screen Shot 2023-12-19 at 14 53 09

The typical solution is to rewrite those paths to point to github instead. In my own Scala.js libraries I add a snippet like this to the js project in sbt:

    scalacOptions ++= sys.env.get("CI").map { _ =>
      val localSourcesPath = baseDirectory.value.toURI
      val remoteSourcesPath = s"https://raw.githubusercontent.com/sirthias/borer/${git.gitHeadCommit.value.get}/"
      val sourcesOptionName = if (scalaVersion.value.startsWith("2.")) "-P:scalajs:mapSourceURI" else "-scalajs-mapSourceURI"

      s"${sourcesOptionName}:$localSourcesPath->$remoteSourcesPath"
    },

and make sure that CI env variable is set when releasing (you can skip that filter, but then you will have source maps pointing to incorrect github URLs if you're testing out a Borer version that isn't (yet) published to github.

That snippet needs sbt-git plugin. That snippet so far has worked well, and you can sanity-check that by finding the github URL paths in your library's sjsir files.

There's also https://github.com/ThoughtWorksInc/sbt-scala-js-map but I haven't tried it myself.

sirthias commented 8 months ago

Ok, thank you @raquo, that really should be fixed! I'll look into it.

sirthias commented 8 months ago

I added the sbt-scala-js-map plugin, which looks like it should take care of things automatically. We'll see with the next release (coming in a few minutes) whether there's sth else that's missing.

sirthias commented 8 months ago

I just released 1.13.0 which should now have source map URLs pointing to github, if the sbt-scala-js-map really automagically fixed it. I tried inspecting a SJSIR file content with scalajsp to somehow see where it's pointing but scalajsp doesn't appear to actually show this. Could you check, whether the issues is now fixed, @raquo ?

raquo commented 8 months ago

Thanks! Unfortunately it does not seem that the plugin took effect.

I look at sjsir files in the dumbest way, as binary-plaintext, in IntelliJ. This is a file from borer 1.13.0, note the local file path:

Screen Shot 2023-12-22 at 14 00 51

For comparison, this is a file from laminar, with the github path: Screen Shot 2023-12-22 at 13 52 58

I'm guessing you should be able to see the same if you open the sjsir file in a plain text editor.

I'm not sure what the sbt-scala-js-map plugin needs in your build.sbt to be picked up. I'm using my own snippet because I don't understand how it works, and there isn't much in the readme. But I searched in github to see what people are doing, and maybe you need something like this? https://github.com/nightscape/MetaRx/blob/190aaa6ffe20a1faee88a92629e1abff41073e3f/build.sbt#L56 No idea if that's the case, to be honest.

sirthias commented 8 months ago

Ok, I think I found a nice and simple solution that actually works (without the sbt-scala-js-map plugin). It'll be published with the next release.

raquo commented 8 months ago

Nice, and thanks for the LocalRootProject tip, turns out I need it too in one of my libs (that is also cross-platform/multi-module)

sirthias commented 8 months ago

The LocalRootProject incantation is something I found by chance in the monix build.sbt, so the πŸ™ should actually go to @alexandru. Thank you, Alex!

alexandru commented 8 months ago

πŸ™ I copied it from somewhere else, but don't remember from where πŸ™‚

plokhotnyuk commented 8 months ago

@raquo @sirthias @alexandru Thanks a lot to all you!

I used your solutions to fix a similar issue in jsoniter-scala.

sirthias commented 8 months ago

Looks like this will become Scala lore, passed around on the hallways, quietly doing it's thing, with the original inventor long forgotten in the mists of time... πŸ™ƒ