New injections for forked elm packages, and add hot reloading#65
New injections for forked elm packages, and add hot reloading#65supermario merged 66 commits intolamdera-nextfrom
Conversation
Conflicts: extra/Lamdera/Injection.hs
Note that I’m unsure about the try-catch. Conflicts: extra/Lamdera/Injection.hs
|
Tested and can't seem to find any way this breaks things. Thank you for such a detailed and well thought out contribution, as always ❤️ |
|
|
||
| gitChangedFilesIn :: FilePath -> IO [FilePath] | ||
| gitChangedFilesIn dir = do | ||
| stdout <- System.Process.readProcess "git" ["-C", dir, "diff", "origin/master...", "--name-only"] "" |
There was a problem hiding this comment.
Just FYI, this doesn't work on forks by default :)
There was a problem hiding this comment.
I’m not sure I’m following – can you share more details?
There was a problem hiding this comment.
Sure!
My origin is my fork. GitHub forks, by default, only include the default branch: lamdera-next, there is no origin/master (it's upstream/master).
Does that make sense?
There was a problem hiding this comment.
But this command is run in the submodules?
There was a problem hiding this comment.
Yeah that confused me too. I guess git diff is comparing vs. lamdera's master? (which wouldn't have them... so that doesn't make sense 🤔)
All I can say is that git fetch upstream master:refs/remotes/origin/master in reactor fixed it, so it's about lamdera proper...
Here's the compiler logs if that helps anything
elm> build (exe) with ghc-9.2.8
Preprocessing executable 'lamdera' for elm-0.19.1..
Building executable 'lamdera' for elm-0.19.1..
[130 of 225] Compiling Deps.Website [Elm.Package changed]
[131 of 225] Compiling Lamdera.PackageReplacements
ld: warning: -single_module is obsolete
ld: warning: ignoring duplicate libraries: '-lc++', '-lz'
fatal: ambiguous argument 'origin/master...': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
/Users/dukese01/Developer/lamdera/extra/Lamdera/PackageReplacements.hs:21:3: error:
• Exception when trying to run compile-time code:
readCreateProcess: git "-C" "extra/package-replacements/elm/browser" "diff" "origin/master..." "--name-only" (exit 128): failed
Code: (PackageReplacementsTH.loadReplacements)
• In the untyped splice: $(PackageReplacementsTH.loadReplacements)
|
21 | $(PackageReplacementsTH.loadReplacements)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: [S-7282]
Stack failed to execute the build plan.
While executing the build plan, Stack encountered the error:
[S-7011]
While building package elm-0.19.1 (scroll up to its section to see the error) using:
/Users/dukese01/.stack/setup-exe-cache/aarch64-osx/Cabal-simple_w2MFVN35_3.6.3.0_ghc-9.2.8 --verbose=1 --builddir=.stack-work/dist/aarch64-osx/ghc-9.2.8 build exe:lamdera --ghc-options " -fdiagnostics-color=always"
Process exited with code: ExitFailure 1
There was a problem hiding this comment.
I tried the following:
git clone git@github.com:lamdera/compiler.git
cd compiler
git submodule update --init
git "-C" "extra/package-replacements/elm/browser" "diff" "origin/master..." "--name-only"
And it worked. So a fresh clone should work at least. I think.
I haven’t tested a fork, though. But I can’t understand yet how that would make anything different.
There was a problem hiding this comment.
So a fresh clone should work at least.
Huh, that's weird but it works now. Not sure what's different this time but 🤷🏻♂️
Well, sorry for bothering ya and thanks for bearing with me! ❤️
Summary
This adds https://github.com/lydell/elm-safe-virtual-dom to Lamdera, simplifies the internal implementation, and changes Freeze mode to use elm-watch-style hot reloading (which preserves things like scroll position) when possible during development.
Details
I’ve added hot reloading and stopping of apps directly to elm/core. Read about it here:
Forks:
As you can see above, lydell/core has a lamdera branch:
errorHandlerargument to apps. (Used by the new REPL stuff.)lydell/browser also has a lamdera branch:
Browser.Navigation.Key, to make it transferable from an old app to a new app. Random note:Browser.Navigation.Keyis no longer a special type and can even be sent to the backend and back to the frontend.Benefits of this PR:
lamdera live.Testing
lamdera livehot reloading.lamdera live).Installing the forked packages
Here’s the plan: Start out simple and make things more complicated in the future if needed.
ByteStringof alternate file content. Used to do the replacements.Validate versions
In the TemplateHaskell-created map, each elm/* package that we replace specifies a version. That’s the version that the forked package was based on. In elm.json, there will still be lines like
"elm/core": "1.0.5". What if the version in elm.json and the “fork version” do not match?First, let’s go through the scenarios where the fork version is higher than the version specified in elm.json:
1.xversions exists of all elm/* packages we replace. But we could code it to be a hard error.1.0.xversions exists of all elm/* packages we replace. But we could code it to be a hard error."elm/virtual-dom": "1.0.3"in their elm.json, and haven’t bothered updating to the recent 1.0.4 version. If we made this a hard error, it would be annoying for lots of people. I think it’s better to simply allow the fork patch version to be greater than specified. If we want to, we could inform about this somehow (log message, or maybe the comments in the compiled JS shown below is enough?).Then, let’s go through the opposite scenarios, where the fork version is lower than the version specified in elm.json:
"elm/virtual-dom": "1.0.5"in their elm.json, I think it should be a hard error, informing them that you can’t go above 1.0.4 with this release of the Lamdera compiler. (Silently using 1.0.4 anyway would be misleading, leading to a false sense of security.) They need to wait for a new Lamdera compiler version that has pulled in the security fix.lamdera inituses the known versions of elm/virtual-dom etc.In the compiled JS, it might be nice adding comments for debugging:
Replacements
When reading files from disk in
ELM_HOME, check if the file is in the replacement maps. If so, go with that file content instead of the actual files.This requires two changes to caches:
ELM_HOMEthere areartifacts.datfiles, which Lamdera already callartifacts.x.dat. Now, they’ll be called for exampleartifacts.lamdera-1.3.3-0.19.1.dat. This is because we can’t trust an artifacts file made by a previous Lamdera version. It might have had different package replacements.elm-stuff/there’s usually a0.19.1folder with artifacts. Now there will beelm-stuff/lamdera-1.3.3-0.19.1/instead. This is again because cached stuff from a previous Lamdera version cannot be trusted – they might contain the wrong package replacement code. (We don’t use the same approach forELM_HOME–~/.elm/lamdera-1.3.3-0.19.1/– because IDE:s expect packages to be in~/.elm/0.19.1/.)Pros and cons
The benefit of having hard-coded (TemplateHaskell) maps of replacements is simplicity. We know that the provided packages are going to work with Lamdera and hot reloading.
The downside is that we need to make a new compiler release if there’s a hotfix in one of the replaced elm/* packages. However, that happens so infrequently that it feels safe to try out this approach.
Hot reloading
In this PR, I tried to change as little as possible. Both in terms of code, and in terms of user experience.
The default is still to refresh the page when an Elm file changes. But in Freeze mode, we now do hot reloading instead of page refresh when possible.
I decided to change as little as possible on the Haskell side, and implemented almost everything needed for hot reloading in Live.elm and live.js.
The web socket server still just sends an
rmessage when an Elm file has changed. When handling that message, the following happens:/to get the page as HTML, with the inline<script>tag.// lamdera-elm-js-startand// lamdera-elm-js-endmarkers.init. If not, refresh the page and letinitreset and notify about this.Previous (maybe future) idea
The last commit of #29 adds two interesting things:
maintype hasn’t changed.) It does that by adding a script tag pointing to the URL/_x/js./_x/js.If the endpoint returns JavaScript like this, hot reload should Just Work: