<div dir="ltr">Thanks Matthew for your pointers.<div><br></div><div>Since originally posting, I managed to simplify the problem by terminating the compilation server at the end of a build, which allows to introduce the assumption that the code doesn't change during the lifetime of the server.</div><div><br></div><div>Now, I'm observing that sometimes different compilation requests place the same package databases at different paths using the -package-db flags. From the point of view of GHC, it is as if the package databases had been moved from one location to another. In newer requests, GHC still looks for the interface files at the old locations, and fails when it doesn't find them.</div><div><br></div><div>Another difference between requests is that, even for a same package database, different interface files are present, depending on what the module under compilation imports transitively. This is causing failures sometimes but not always, I still need to pin exactly the circumstances. The error manifests as an attempt to load a missing interface file that is apparently not transitively needed.</div><div><br></div><div>If I understand correctly, all the packages pointed with -package-id and -package-db end up in the EPS. And this means that we can't expect to update the locations of the interface files without discarding and repopulating the EPS, correct? I'm thinking of this as approximately as costly as restarting the compilation server.</div><div><br></div><div>I can reasonably ensure that package databases aren't moved around between compilation requests. But from the standpoint of the build system, it would require some compromises to demand that all of the interface files of a package be available even when not all of them are transitively imported. Can we hope to have GHC cope with this dynamic membership of modules to Haskell packages during the build? Is this an ability that 8.10.7 already has?</div><div><br></div><div>Thanks,</div><div>Facundo</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, May 5, 2022 at 5:13 AM Matthew Pickering <<a href="mailto:matthewtpickering@gmail.com">matthewtpickering@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi Facundo<br>
<br>
Some pointers...<br>
<br>
1. Only put things in the EPS if they are not going to change<br>
throughout the whole compilation<br>
2. Treat everything which can change as a home package<br>
2a. I suppose you have performed your own dependency analysis, so<br>
build your own `ModGraph` and start looking from `load'`, you might<br>
just want to call `upsweep_mod/compileOne'` directly yourself.<br>
2b. I suppose you are NOT targeting 9.4.1 yet, but that will make<br>
things easier as you can use support for multiple home packages,<br>
otherwise you will get into severe difficulties if you load a package<br>
you later want to compile into the EPS. The only thing you can do here<br>
      is restart the compilation session I think.<br>
3. To my knowledge, there is no issue using different -this-unit-id in<br>
the same session. Not sure what errors you have seen.<br>
4. You need to use --make mode rather than -c (oneshot) because<br>
oneshot mode loads all interfaces into the EPS (see point 1)<br>
<br>
ghcide is the closest program to this kind of compilation server you<br>
imagine so you can look at how that uses the GHC API.<br>
<br>
Cheers,<br>
<br>
Matt<br>
<br>
<br>
On Thu, May 5, 2022 at 1:06 AM Domínguez, Facundo<br>
<<a href="mailto:facundo.dominguez@tweag.io" target="_blank">facundo.dominguez@tweag.io</a>> wrote:<br>
><br>
> Dear ghc devs,<br>
><br>
> I'm using the ghc API to write a compilation server (a.k.a. persistent worker). The idea is to serve requests to compile individual modules. In this fashion, we can compile modules with different compilation flags and yet pay only once for the startup overheads of the compiler.<br>
><br>
> One challenge of this approach is to reuse as much as possible from the ghc API session/environment from one compilation request to the next, so we save the trouble of reconstructing it each time. This message is to ask for advise on how to better accomplish this reuse.<br>
><br>
> I tried reusing the whole environment for multiple requests, but I'm conjecturing that this might cause troubles when the requests require building modules with different values of -this-unit-id. Another problem that streams from this is that recompiling a module which defines a type class instance fails because it encounters in the environment the type class instance from the<br>
> previous compilation.<br>
><br>
> My work-in-progress implementation is here [1]. There appears to be multiple ways to compile a module in the API, so far I have been trying DriverPipeline.compileFile.<br>
><br>
> My best lead right now is to look for inspiration in how GHCi implements the load command, but this does a sort of --make compilation while I'm going here for the one-shot style.<br>
><br>
> Thanks in advance,<br>
> Facundo<br>
><br>
> [1] <a href="https://github.com/tweag/rules_haskell/blob/16ba422457ea4daa5dbf40d46327ebcb20588e97/tools/haskell_module_worker/src/Compile.hs#L188" rel="noreferrer" target="_blank">https://github.com/tweag/rules_haskell/blob/16ba422457ea4daa5dbf40d46327ebcb20588e97/tools/haskell_module_worker/src/Compile.hs#L188</a><br>
> _______________________________________________<br>
> ghc-devs mailing list<br>
> <a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a><br>
> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs</a><br>
</blockquote></div>