Use Dune to compile a specific file, in memory (stdin?)

Suppose I have a dune project with such a basic structure:

dune-project
src/
  dune
  main.ml
lib/
  dune
  myFile.ml

If I have a different version of myFile (or main.ml) in-memory only, I want to use dune to compile that version of the file and get the compilation output for errors or warnings.

is one of this possible?:

  • can I ask dune to compile that edited source code from stdin and get the compilation output ? (ideal scenario)
  • can I save the file to a temp directory and ask dune to build just that file from a project directory ?
  • If not possible can I ask dune for some compilation flags and use ocamlc to do the compilation ?

context: this is for editor integration. I don’t use LSP/merlin.

I don’t think this is doable.

Sure, but you will need to also generate a corresponding dune file.

context: this is for editor integration. I don’t use LSP/merlin.

Maybe it would be easier if you explained what your endgoal is. The next version of dune will have an RPC protocol to interact with editors (see Dune RPC — Dune documentation), perhaps it wil be helpful?

Cheers,
Nicolas

My end goal is to get compilation errors/warnings for an edited file that is in memory only, to report these errors as user edit the file.

I didn’t know about dune rpc, but in my view, it would be better if I can avoid to start a daemon process and just use a one time background process when needed.

I didn’t know about dune rpc, but in my view, it would be better if I can avoid to start a daemon process and just use a one time background process when needed.

If your plan is to provide information as soon as the user inputs something you probably want to spare the starting time of the dune process which can be quite long in big projects.

Probably, you can get from the dune the list of path where is looks for project files and dependencies

Then you copy your .ml file a temp directory and run capturing the stdout/stderr

ocamlc.opt -c -I dir1 -I dir2 ... file.ml

It is doable, ftovagliari/ocamleditor does this. More here

A possible optimizations

  • ocamlc -stop-after {parsing|typing}

@vrotaru nice hint, I’ll try that.
is there a command to get the list of path ?

Sorry, no. I have very little experience with dune.

dune build --verbose will print all that info, but there must be a better way.