disneystreaming / smithy4s

https://disneystreaming.github.io/smithy4s/
Other
340 stars 69 forks source link

Scala JS source maps, map to CI runner dir? #705

Closed Quafadas closed 1 year ago

Quafadas commented 1 year ago

In my pet project, vite complains about source map errors. I believe one of the culprits to be smithy.

Here's a sample .js.map file.

{
"version": 3,
"file": "frontend.Clients$.js",
"mappings": "blah blah",
"sources": ["file:///C:/temp/ilsclaims/shared/src/example/Client.scala", "file:///home/runner/work/smithy4s/smithy4s/modules/http4s/src/smithy4s/http4s/SimpleProtocolBuilder.scala"],
"names": ["internal-b28b7af69320201d1cf206ebf28373980add1451", "eventClient", "internal-9fb50eb6088e86acde75de71513c77ec947f608e", "exposedEventClient", "notificationClient", "claimReportClient", "ilsopWrapperClient"],
"lineCount": 59
}

I think the important part, is the home/runner/work/smithy4s/etc/etc line, which obviously doesn't exist, in my local environment. Because I aggressively lurk discord, I believe the solution may look something like this;

https://discord.com/channels/632150470000902164/635668814956068864/1060561961810677760

https://github.com/typelevel/sbt-typelevel/blob/4da726ca021ea2c6459e8286d9ec49ab7c79a883/github/src/main/scala/org/typelevel/sbt/TypelevelScalaJSGitHubPlugin.scala#L35

I believe there to be a compiler flag, which could potentially point this back to this repository, for example. I will openly admit, that my understanding of this, is somewhat superficial...

armanbilge commented 1 year ago

Yup, you want to do what the sbt-typelevel plugin is doing here.

zetashift commented 1 year ago

So I'll try to put up a PR for this.

The solution being to pass an appropiate scalacOptions setting for sourcemaps:

From sbt-typelevel:

 scalacOptions ++= {
      val flag = if (tlIsScala3.value) "-scalajs-mapSourceURI:" else "-P:scalajs:mapSourceURI:"

      val tagOrHash =
        GitHelper.getTagOrHash(git.gitCurrentTags.value, git.gitHeadCommit.value)

      val l = (LocalRootProject / baseDirectory).value.toURI.toString

      tagOrHash.flatMap { v =>
        scmInfo.value.map { info =>
          val g =
            s"${info.browseUrl.toString.replace("github.com", "raw.githubusercontent.com")}/$v/"
          s"$flag$l->$g"
        }
      }
    },
armanbilge commented 1 year ago

Note that this assumes that you are setting scmInfo correctly :)

keynmol commented 1 year ago

@zetashift Go ahead, yes! this will be great.

One thing to note that might trip you up is that smithy4s' build uses sbt-projectmatrix, which specifically changes the baseDirectory of the project to something like target/.matrix/...

So if you see weird values - don't be alarmed and you might have to go through paths relative to ThisBuild / baseDirectory which should be unaffected

armanbilge commented 1 year ago

One thing to note that might trip you up is that smithy4s' build uses sbt-projectmatrix, which specifically changes the baseDirectory of the project to something like target/.matrix/...

I don't think that should matter, crossProject does similar shenanigans depending on whether its pure or not.

zetashift commented 1 year ago

Is there a way to get the tag or hash without involving a sbt plugin? In sbt typelevel, there are helpers of this: val tagOrHash = GitHelper.getTagOrHash(git.gitCurrentTags.value, git.gitHeadCommit.value)

Browsing through the sbt docs I can't seem to find anything about getting git values(which makes sense for a build tool :P).

Currently sketching out adding these options to jsDimSettings:

  lazy val jsDimSettings = simpleJSLayout ++ Seq(
    scalacOptions ++ {
      val flag =
        if (scalaVersion.value.startsWith("3")) "-scalajs-mapSourceURI:"
        else "-P:scalajs:mapSourceURI:"

    },

EDIT: the plugin mentioned would be https://github.com/sbt/sbt-git

kubukoz commented 1 year ago

yes, I'd suggest sbt-git. If we don't have that, it can probably be added

zetashift commented 1 year ago

yes, I'd suggest sbt-git. If we don't have that, it can probably be added

Yes, sbt-git is already in there...I forgot to sync my fork and thus was modifying a very old version 🤦

zetashift commented 1 year ago

I think the sourcemaps still aren't working:

image

(the picture should actually be pointing to a github url instead of /home/runner.../

The problem is that the "local" path doesn't point to sources... I think it has something to do with the fact that the project uses /modules/project_dir thus the "baseDirectory" might not be correct.

Baccata commented 1 year ago

Maybe sourceDirectory should have been used instead of baseDirectory ?

zetashift commented 1 year ago

This is the difference between sourceDirectory and baseDirectory

image

I am unsure which one is the right path, because of sbt-projectmatrix and if it even matters.

armanbilge commented 1 year ago

(the picture should actually be pointing to a github url instead of /home/runner.../

So I haven't been able to look into this in depth, but the fact that the source maps didn't change at all suggests something more fundamental is wrong, than the path is wrong.

If it was working, even if the path was wrong, at least the sourcemaps would be pointed to a wrong GitHub URL, instead of to the runner directory.

Sorry, I take that all back 😅 I guess it's the localSourcesPath that's wrong, which is why the sourcemap transformation is not applying? Edit: figured it out, see https://github.com/disneystreaming/smithy4s/pull/706/files#r1072602594