You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
the Coq mode for Dune is getting more mature, and a big blocker now is integration with Proof General. I am unsure how to best approach this; basically PG should add the right flags to make libraries compiled in _build into scope.
A possibility is indeed to let dune handle this; so PG could call dune coqtop foo.v and indeed coqtop would be started with the right settings. WDYT?
It could, but I'm not sure I'm willing to do this; notably the _CoqProject format will have trouble keeping with Dune features; also it pollutes the source tree. Experience with generating .merlin files in OCaml indicates that this is a path we don't want to follow.
Also, we need some indication of where each .v file came from, so that jump-to-definition can go to the right place.
What do you concretely mean? If you use dune coqtop foo.v you would be using the right file already; otherwise you could implement something like tuareg does [it understands _build]
dune provides some APIs to query projects, but I guess I'd need to see the concrete use case. I'm not sure if this index functionality is something to have in PG, or instead PG should rely on an external too such as ocp-index for OCaml.
It could, but I'm not sure I'm willing to do this; notably the _CoqProject format will have trouble keeping with Dune features; also it pollutes the source tree. Experience with generating .merlin files in OCaml indicates that this is a path we don't want to follow.
OK. Then running dune coq sounds good. How do we detect dune projects reliably? Do we just search for dune-project files?
What do you concretely mean?
When I press M-. in company-coq it issues a Locate query for the current symbol. Then it uses Locate Library to find where the library came from. For example M-. on nat yields this:
<prompt>Coq < 17 || 0 < </prompt>Timeout 1 Locate nat.
Inductive Coq.Init.Datatypes.nat
<prompt>Coq < 19 || 0 < </prompt>Timeout 1 Locate Library Coq.Init.Datatypes.
Coq.Init.Datatypes has been loaded from file
/home/clement/.opam/default/lib/coq/theories/Init/Datatypes.vo
Then it jumps to the corresponding .v file: /home/clement/.opam/default/lib/coq/theories/Init/Datatypes.v
With dune this doesn't work: instead of going to the original .v file it jumps to the copy that dune made in _build. For OCaml merlin detects this. I need to detect it too in company-coq. So the question is: how do I find which original file a .v file in _build is a copy of?
(We discussed this on gitter a while ago:
@cpitclaudel
Hey @ejgallego, we've been using dune to build our latest Coq project, and it's working very nice. Thanks again!
There's one thing I need to tweak in company-coq for it to work perfectly, though: currently jump-to-definition goes to teh .v file in _build/default because that's where the .vo and .v file are. Do you have ideas on how company-coq could learn where the original .v file was?
@SkySkimmer
remove the _build/default prefix?
@cpitclaudel
Yes, but that feels a bit ah-hod, and IIUC dune doesn't always use that prefix.
@SkySkimmer
how does merlin do it? I know dune generates some .merlin files for it but don't know how it links the B and S lines together
@cpitclaudel
I don't know either :) But you're onto something. Maybe dune could generate a _CoqProject (I think it currently doesn't?) that would include the right info
Emilio Jesús Gallego Arias
@ejgallego
Yup merlin + dune is an open problem, upstream devs are working on it, it is a bit painful as you have to call dune at every check.
What I am thinking is to provide a dune info command that editors could use, given a file foo.v, dune info foo.v would provide:
flags
list of dependencies
original location
as indeed you are correct @cpitclaudel the location of the files is highly dependent on quite a few factors
as for merlin, it will basically do something like that in the future, likely keeping a pipe open to dune, as it is expensive sometimes, think of the whole duniverse
@cpitclaudel
Very interesting, thanks a lot. But since Dune already generates a merlin file, could it not enhance that with extra info so that it doesn't have to be called each time? (this might be naive)
And in the Coq case, couldn't we put the relevant info in the _CoqProject?
I'm thinking something like a map file that would say where each .vo file came from
Emilio Jesús Gallego Arias @ejgallego @cpitclaudel both .merlin and _CoqProject have quite serious problems, for example they cannot do per-file flags, etc... So indeed Dune will stop generating .merlin files pretty soon I heard.
I'm thinking something like a map file that would sya where each .vo file came from
Yup, dune info should provide that, however I'm curious why do you need to find the information about the .vo files instead of the .v file, which is what the editor uses?
@cpitclaudel
To locate a symbol I do Locate xyz.and then Locate Library A.B.C, which points me to a .vo file
@ejgallego
To locate a symbol I do Locate xyz.and then Locate Library A.B.C, which points me to a .vo file
I see, indeed that's a bit tricky, as imagine A.vo is coming from a generated A.v file which is not in the source tree, I'll have a look how merlin does handle locate, but indeed we could provide a reverse-map
OK. Then running dune coq sounds good. How do we detect dune projects reliably? Do we just search for dune-project files?
That's the standard method, we could add a variable in case the user wants to override?
We discussed this on gitter a while ago
Oh yes I remember that, I thought it had happened in an issue but I couldn't locate it.
Actually OCaml has the same problem sometimes [you use M-. but it sends you to the _build dir] but tuareg got a hack for it; I'll have a look and discuss with dune / OCaml upstream to see what the plan regarding this is.
Actually that doesn't bother me too much; for now the large majority of users will be satisfied by that; we could add a locate-build-prefix var for projects that use a special prefix.
The medium term plan is to have something similar to the LSP protocol locate call; still the relation between the lsp-server and the build tool has not been fully determined tho.
Isn't it the dune file that we want to detect here? dune-project is not necessary is it?
Be careful, we use the _CoqProject informations for other actions than just launching coqtop.
For instanceIt is used by "auto-compilation on Require" (which detects if dependencies of a file needs recompilation and preforms it), and by company-coq as already remarked. @cpitclaudel how many other of your features need the _CoqProject infos?
So if we accept dune project files we need to either parse it or be able to query dune for several things.
@cpitclaudel how many other of your features need the _CoqProject infos?
It's not so much that company-coq needs _CoqProject; it's that dune copies source files into a separate directory, so it adds extra work to find out where things came from.
Maybe dune could add a comment at the end of the file, or something. Or maybe it could have a command to figure out where a file came from.
Note that dune coqtop foo.v will update the dependencies of foo.v automatically, as it does for the OCaml version. It will also set the right flags for foo.v.
@ejgallego IIUC you suggest that the main place from PG codebase where coqtop is called to run the REPL, we should replace for example (if I open a file from ValidSDP library) the command:
Not even -topfile should be needed IMO, but indeed yes, dune knows how to compute the flags and includes for a particular file so IMO it is better not to duplicate that logic in emacs; the same could be applied to coqidetop, good suggestion indeed!
Reacted by Erik Martin-Dorel and Martin Constantino–Bodin
Isn't it the dune file that we want to detect here? dune-project is not necessary is it?
also we may want to detect if dune itself is in the path.
anyway correct me if I'm wrong, but it seems we'll also need to keep _CoqProject support functional (say, as a fallback if the dune binary and project file is not found)
dune-project indicates the root, IMHO it is better to issue locate-dominating-file buffer-file-name for dune-project and assume that's the root of the project. dune files can appear in many subdirs but are really small pieces.
I guess it boils down to whether you want to do dune coqtop theories/foo.v or cd theories && dune coqtop foo.v
I tend to prefer working from the root; keep in mind that dune coqtop foo.v would compile foo's deps, so IMHO reported paths in case of errors would look more natural.
Keeping _CoqProject is fine, whether a project wants to support both build systems is IMHO down to them.
Regarding locate I suggest we follow fow now the "hack" approach as done in ocp-index ; the issue is being discussed upstream so it seems to me that waiting to see what happens there may be sensible vs rolling our own complex solution.
dune-project indicates the root, IMHO it is better to issue locate-dominating-file buffer-file-name for dune-project and assume that's the root of the project. dune files can appear in many subdirs but are really small pieces.
That sounds good, though we should use default-directory, not buffer-file-name. Also, this will break for some projects using dune, so we should be careful to add a setting to disable it (my Coq projects using dune have multiple libraries depending on each other, and these dependencies are captured in a _CoqProject but not in a dune file. Maybe the latest changes make that unnecessary?
I guess it boils down to whether you want to do dune coqtop theories/foo.v or cd theories && dune coqtop foo.v
Note that doing the former will change the way commands producing files behave, I think (not necessarily a bad thing, just pointing it out).
Keeping _CoqProject is fine, whether a project wants to support both build systems is IMHO down to them.
I think it's also that we want to support projects with just one of these systems.
@cpitclaudel Please only answer after your defence!
If dune copies the files around, shouldn't it have a function that translates a copied filename back to the original file name? Wouldn't it be cleaner to pipe the location you get from Coq through that dune back-translation function?
By the way @ejgallego does dune rename coq modules as it does for ML modules with the dune__exe__ prefix? Isn't that also a source of hacks somewhere in merlin or tuareg?
Now that support for coqtop is available in dune (since version 3.1.0), I'm wondering how we should move forward on the PG side. I'm definitely not the right person for the job, since I'm hopeless with Emacs.
Roughly, what needs to be done to have reasonable support is (I think) the following:
Detect the presence of a dune workspace (its root contains a dune-project file), fallback to normal behaviour if there is none.
Decide whether we want to run coqtop via dune, not sure how. Maybe only do it if there is no _CoqProject file?
Running the Coq toplevel with command dune coq top <file> -- -emacs, where file is a path to the Coq source file. Ideally, the command would be run from the root of the dune workspace, with a relative path to the file. (There are some issues with absolute paths and symbolic links in dune, so using an absolute path is not ideal even if it works in most cases.)
Enable some hacks to improve usability, like what @MackieLoeffel proposed here to enable jump-to-definition in the original source file (not its copy under dune's _build directory).
Thanks for all the work @rlepigre , a few comments:
1 and 2: Indeed there are some heuristics to guess that, usually locate-dominating-file helps. Also, dune has several commands that may help, and a RPC protocol which is more fancy and can provide config à la merlin.
I guess a good start would be to find a project root (using locate-dominating-file) and start from there.
For 3. we have a bug in dune, once we fix it, it should work to run dune coq top from any point, but indeed I guess PG may enjoy knowing the root.
For 4. I'd just copy what tuareg does, a very simple but effective hack: if the file starts with _build, strip the prefix 😆
Any PR welcome of course!
One thing that has not been mentioned yet: a PR should also adapt the auto-compilation stuff. This would probably be easy since dune would perform dependency analysis by itself, but gluing this with the code for Coq and Rocq will probably be a pain. Also it would be nice to add tests.
So to sum up: nothing difficult but certainly needs some time to come up with something acceptable. I will try to find some time this summer (no guarantee).
Activity
cpitclaudel commentedon Apr 2, 2020
Sounds reasonable, but we'd need to be able to determine when dune is in use.
Could dune generate a _CoqProject file for us? In my projects I just write it by hand:
Also, we need some indication of where each .v file came from, so that jump-to-definition can go to the right place.
ejgallego commentedon Apr 3, 2020
It could, but I'm not sure I'm willing to do this; notably the
_CoqProjectformat will have trouble keeping with Dune features; also it pollutes the source tree. Experience with generating.merlinfiles in OCaml indicates that this is a path we don't want to follow.What do you concretely mean? If you use
dune coqtop foo.vyou would be using the right file already; otherwise you could implement something like tuareg does [it understands _build]duneprovides some APIs to query projects, but I guess I'd need to see the concrete use case. I'm not sure if this index functionality is something to have in PG, or instead PG should rely on an external too such asocp-indexfor OCaml.ejgallego commentedon Apr 3, 2020
Yeah, already we cannot generate a
_CoqProjectthat works for a simple case with two composed libraries, and those with different flags.cpitclaudel commentedon Apr 3, 2020
OK. Then running
dune coqsounds good. How do we detect dune projects reliably? Do we just search fordune-projectfiles?When I press
M-.in company-coq it issues aLocatequery for the current symbol. Then it usesLocate Libraryto find where the library came from. For exampleM-.onnatyields this:Then it jumps to the corresponding .v file:
/home/clement/.opam/default/lib/coq/theories/Init/Datatypes.vWith dune this doesn't work: instead of going to the original .v file it jumps to the copy that dune made in
_build. For OCaml merlin detects this. I need to detect it too in company-coq. So the question is: how do I find which original file a.vfile in_buildis a copy of?(We discussed this on gitter a while ago:
ejgallego commentedon Apr 3, 2020
That's the standard method, we could add a variable in case the user wants to override?
Oh yes I remember that, I thought it had happened in an issue but I couldn't locate it.
Actually OCaml has the same problem sometimes [you use M-. but it sends you to the _build dir] but tuareg got a hack for it; I'll have a look and discuss with dune / OCaml upstream to see what the plan regarding this is.
ocp-indexseems to use a hack, see https://github.com/OCamlPro/ocp-index/blob/master/libs/indexBuild.ml#L276Actually that doesn't bother me too much; for now the large majority of users will be satisfied by that; we could add a
locate-build-prefixvar for projects that use a special prefix.The medium term plan is to have something similar to the LSP protocol locate call; still the relation between the lsp-server and the build tool has not been fully determined tho.
Matafou commentedon Apr 6, 2020
Isn't it the
dunefile that we want to detect here?dune-projectis not necessary is it?Be careful, we use the _CoqProject informations for other actions than just launching coqtop.
For instanceIt is used by "auto-compilation on Require" (which detects if dependencies of a file needs recompilation and preforms it), and by company-coq as already remarked. @cpitclaudel how many other of your features need the _CoqProject infos?
So if we accept dune project files we need to either parse it or be able to query dune for several things.
cpitclaudel commentedon Apr 6, 2020
It's not so much that company-coq needs _CoqProject; it's that dune copies source files into a separate directory, so it adds extra work to find out where things came from.
Maybe dune could add a comment at the end of the file, or something. Or maybe it could have a command to figure out where a file came from.
Matafou commentedon Apr 6, 2020
OK and no other feature of company-coq needs the compile-options?
cpitclaudel commentedon Apr 6, 2020
Everything is queried through Coq, so they do need the options, but indirectly ^^
ejgallego commentedon Apr 6, 2020
Note that
dune coqtop foo.vwill update the dependencies offoo.vautomatically, as it does for the OCaml version. It will also set the right flags forfoo.v.erikmd commentedon Apr 6, 2020
@ejgallego IIUC you suggest that the main place from PG codebase where
coqtopis called to run the REPL, we should replace for example (if I open a file from ValidSDP library) the command:coqtop -topfile …/validsdp/theories/validsdp.v -emacs -I …/validsdp/plugins/soswitness/src -R …/validsdp/plugins/soswitness/theories ValidSDP -R …/validsdp/plugins/soswitness/src ValidSDP -R …/validsdp/plugins/soswitness/test-suite ValidSDP -R …/validsdp/test-suite ValidSDP -R …/validsdp/theories ValidSDPwith just
dune coqtop -topfile …/validsdp/theories/validsdp.v -emacs?If yes, would this work as well with
dune coqidetop …? (to be able to use Coq's XML protocol)ejgallego commentedon Apr 6, 2020
Not even
-topfileshould be needed IMO, but indeed yes,duneknows how to compute the flags and includes for a particular file so IMO it is better not to duplicate that logic in emacs; the same could be applied tocoqidetop, good suggestion indeed!erikmd commentedon Apr 6, 2020
BTW I have the same question as
@Matafou
also we may want to detect if
duneitself is in the path.anyway correct me if I'm wrong, but it seems we'll also need to keep
_CoqProjectsupport functional (say, as a fallback if the dune binary and project file is not found)ejgallego commentedon Apr 6, 2020
dune-projectindicates the root, IMHO it is better to issuelocate-dominating-file buffer-file-namefordune-projectand assume that's the root of the project.dunefiles can appear in many subdirs but are really small pieces.I guess it boils down to whether you want to do
dune coqtop theories/foo.vorcd theories && dune coqtop foo.vI tend to prefer working from the root; keep in mind that
dune coqtop foo.vwould compile foo's deps, so IMHO reported paths in case of errors would look more natural.Keeping
_CoqProjectis fine, whether a project wants to support both build systems is IMHO down to them.Regarding
locateI suggest we follow fow now the "hack" approach as done inocp-index; the issue is being discussed upstream so it seems to me that waiting to see what happens there may be sensible vs rolling our own complex solution.cpitclaudel commentedon Apr 6, 2020
That sounds good, though we should use default-directory, not buffer-file-name. Also, this will break for some projects using dune, so we should be careful to add a setting to disable it (my Coq projects using dune have multiple libraries depending on each other, and these dependencies are captured in a _CoqProject but not in a dune file. Maybe the latest changes make that unnecessary?
Note that doing the former will change the way commands producing files behave, I think (not necessarily a bad thing, just pointing it out).
I think it's also that we want to support projects with just one of these systems.
22 remaining items
hendriktews commentedon Nov 25, 2021
@cpitclaudel Please only answer after your defence!
If dune copies the files around, shouldn't it have a function that translates a copied filename back to the original file name? Wouldn't it be cleaner to pipe the location you get from Coq through that dune back-translation function?
cpitclaudel commentedon Nov 26, 2021
It should, and I wish it did. but AFAIK it doesn't at the moment, and the issue has been with us for long enough that I now think worth a hack ^^
(Sorry, couldn't resist. Take care.)
ejgallego commentedon Nov 30, 2021
tuareg just strips the
_buildpart from filesnames, it is a hack but so far it has worked well [until dune rpc is more usable I guess]Matafou commentedon Nov 30, 2021
By the way @ejgallego does dune rename coq modules as it does for ML modules with the dune__exe__ prefix? Isn't that also a source of hacks somewhere in merlin or tuareg?
ejgallego commentedon Nov 30, 2021
@Matafou not that I know of yet, that's mostly for object files tho so it is hard to observe.
Matafou commentedon Nov 30, 2021
Indeed the ML module names are correct error messages. Does it mean dune performs on-the-fly translation of names on error messages?
MackieLoeffel commentedon Dec 22, 2021
Based on the suggestions above, I am currently using the following hack to make
M-.in company-coq go to the correct file:This simple heuristic seems to work so far for my use cases but I don't know how robust it is.
Matafou commentedon Dec 27, 2021
Thanks! We will try something similar.
cpitclaudel commentedon Dec 27, 2021
@MackieLoeffel LGTM, do you want to make it into a PR in company-coq?
MackieLoeffel commentedon Dec 28, 2021
Done in cpitclaudel/company-coq#257
rlepigre commentedon Jun 2, 2022
Now that support for
coqtopis available in dune (since version 3.1.0), I'm wondering how we should move forward on the PG side. I'm definitely not the right person for the job, since I'm hopeless with Emacs.Roughly, what needs to be done to have reasonable support is (I think) the following:
dune-projectfile), fallback to normal behaviour if there is none.coqtopviadune, not sure how. Maybe only do it if there is no_CoqProjectfile?dune coq top <file> -- -emacs, where file is a path to the Coq source file. Ideally, the command would be run from the root of the dune workspace, with a relative path to the file. (There are some issues with absolute paths and symbolic links indune, so using an absolute path is not ideal even if it works in most cases.)_builddirectory).ejgallego commentedon Jun 3, 2022
Thanks for all the work @rlepigre , a few comments:
1 and 2: Indeed there are some heuristics to guess that, usually
locate-dominating-filehelps. Also, dune has several commands that may help, and a RPC protocol which is more fancy and can provide config à la merlin.I guess a good start would be to find a project root (using locate-dominating-file) and start from there.
For 3. we have a bug in dune, once we fix it, it should work to run
dune coq topfrom any point, but indeed I guess PG may enjoy knowing the root.For 4. I'd just copy what tuareg does, a very simple but effective hack: if the file starts with _build, strip the prefix 😆
elisesouche commentedon Jun 11, 2025
Has been there progress on this? Is there something we can do to help?
Matafou commentedon Jun 11, 2025
Any PR welcome of course!
One thing that has not been mentioned yet: a PR should also adapt the auto-compilation stuff. This would probably be easy since dune would perform dependency analysis by itself, but gluing this with the code for Coq and Rocq will probably be a pain. Also it would be nice to add tests.
So to sum up: nothing difficult but certainly needs some time to come up with something acceptable. I will try to find some time this summer (no guarantee).