Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using menhir --explain #6865

Closed
thierry-martinez opened this issue Jan 10, 2023 · 19 comments · Fixed by #9512
Closed

Using menhir --explain #6865

thierry-martinez opened this issue Jan 10, 2023 · 19 comments · Fixed by #9512
Labels
Milestone

Comments

@thierry-martinez
Copy link

thierry-martinez commented Jan 10, 2023

Before dune.3.0, with the following stanza,

(menhir (modules ...) (flags --explain))

when the menhir grammars have conflicts, the reports that menhir generates to describe the conflicts (.conflicts files, one for each .mli file) were available in _build/$profile/.

Since dune.3.0, sandboxing prevents dune from keeping these files, forcing the user to run menhir --explain manually in the source tree to get the reports.

Desired Behavior

The .conflicts files could be copied to the _build/$profile/ directory: that would restore the previous behavior (before dune.3.0), which was better than the current situation, even if I would prefer never have to touch to the ``_build/` directory.

Another behavior could be to promote the .conflicts files in the source tree, which would be close to the experience of using menhir without dune, and which would made the .conflicts file easier to notice. Another possibility could be to dump these files to the standard output when they exist (which can be convenient, but with the same drawbacks than with warnings, that are not displayed again in case of rebuild).

Example

A file test.conflicts should be available somewhere (or its contents should be displayed) after compiling the following file.

test.mly:

%token A
%start<unit> a
%%
let a := A; {} | A; {} 

dune:

(library (name test))
(menhir (modules test) (flags --explain))

This issue was previously discussed here: https://discuss.ocaml.org/t/how-to-use-menhir-explain-with-dune/11132

@Alizter
Copy link
Collaborator

Alizter commented Jan 10, 2023

You can disable sandboxing with --sandbox none. Does that work?

@rgrinberg
Copy link
Member

Promoting it doesn't seem right as it seems like a file with error messages/diagnostics. I wonder why menhir doesn't just dump this output into stderr? @fpottier Why is the separate file necessary?

@thierry-martinez
Copy link
Author

@Alizter, thank you for the tip: indeed, --sandbox none works (.conflicts files are available in _build/$profile). I think I would still prefer not to have to disable sandboxing to get these files.

@thierry-martinez
Copy link
Author

@rgrinberg, indeed, these are diagnostics. Piping them to stdout/stderr would be an option, but what would be the right place with dune for storing such files with errors or diagnostics if not in the source tree? Storing them in _build/ looks a bit arcane.

@nojb
Copy link
Collaborator

nojb commented Jan 11, 2023

Promoting it doesn't seem right as it seems like a file with error messages/diagnostics. I wonder why menhir doesn't just dump this output into stderr? @fpottier Why is the separate file necessary?

This was also the behaviour of "yacc" tools. I think the reason is that this output can be rather large so it is not very useful to read them on the console; one would rather use a text editor.

Thinking out loud, what about making this file an explicit output of the Menhir rules?

@rgrinberg
Copy link
Member

This was also the behaviour of "yacc" tools. I think the reason is that this output can be rather large so it is not very useful to read them on the console; one would rather use a text editor.

Right, but I think we already talked about saving the stderr output of compilation commands to replay warnings. I would imagine this to apply to menhir commands as well. In which case, it would be possible to browse the output with the editor.

@Lelio-Brun
Copy link

@Alizter, thank you for the tip: indeed, --sandbox none works (.conflicts files are available in _build/$profile). I think I would still prefer not to have to disable sandboxing to get these files.

Strangely it does not work in my use case, using dune 3.7.1, either with dune build or dune exec.

@Alizter Alizter added this to the 3.8.0 milestone Apr 19, 2023
@Alizter
Copy link
Collaborator

Alizter commented Apr 19, 2023

@Lelio-Brun is the command running? Can you check what --always-show-command-line says and check any related _build directories?

@Lelio-Brun
Copy link

> dune build --sandbox=none --always-show-command-line
File "src/dune", line 7, characters 0-45:
7 | (menhir
8 |  (modules parser)
9 |  (flags --explain))
(cd _build/.sandbox/9796...938b/default && /home/xxx/_opam/bin/menhir --explain src/parser.mly --base src/parser --infer-read-reply src/parser__mock.mli.inferred)

Thanks, so two remarks:

  1. The file parser.conflicts is indeed present in the sandbox 979...938b
  2. Removing --sandbox=none produces exactly the same outcome

@Alizter
Copy link
Collaborator

Alizter commented Apr 20, 2023

It is strange that --sandbox none did nothing here. @rgrinberg any ideas what might be happening?

@rgrinberg
Copy link
Member

Isn't it just stale artifact deletion? Dune would delete conflicts file because it doesn't know it's a target.

@Alizter
Copy link
Collaborator

Alizter commented Apr 21, 2023

@rgrinberg But in this case the output of --always-show-command-line says it used a sandbox even though --sandbox none was passed. Shouldn't this be run directly in _build/default?

@Alizter
Copy link
Collaborator

Alizter commented Apr 21, 2023

Anyway, here is a fix #7607

@emillon emillon removed this from the 3.8.0 milestone May 17, 2023
Tchou added a commit to Tchou/dune that referenced this issue Oct 27, 2023
Add the .conflicts file to targets of menhir stanzas when the
--explain flag is present and menhir is not called with --only-tokens.
Tchou added a commit to Tchou/dune that referenced this issue Oct 27, 2023
Add the .conflicts file to targets of menhir stanzas when the
--explain flag is present and menhir is not called with --only-tokens.

Signed-off-by: Kim Nguyễn <kim@nguyen.vg>
Tchou added a commit to Tchou/dune that referenced this issue Oct 28, 2023
Add the .conflicts file to targets of menhir stanzas when the
--explain flag is present and menhir is not called with --only-tokens.
It also fixes a similar bug where .cmly files where also not kept with
sandboxing enabled. Both types of files are also now correctly
recognized as targets.

Signed-off-by: Kim Nguyễn <kim@nguyen.vg>
@gasche
Copy link
Member

gasche commented Dec 14, 2023

I came up here after trying to use (flags --explain) in a Menhir-using project.

Here is my understanding of the situation:

@gasche
Copy link
Member

gasche commented Dec 14, 2023

Further comments.

  1. Both PRs rely on checking whether the --explain flags was passed to Menhir. I know little about Dune but this sounds weird to me. Why not introduce a new (explain) field in the menhir stanza, if we want to special-case the build system behavior?

  2. Menhir in general relies on options that generate new files -- and I am sure that many other metaprogramming tools follow the same UI pattern. It would be nice if Dune sandboxing could be taught to run the menhir command in a clean subdirectory (with just the parser file and possibly other dependencies specified by in the (menhir ...) stanza), and preserve the whole subdirectory somewhere in the build artifacts (for example: foo.mly.objs/). The build system may not be aware of some of the files produced, but this is fine as long as they are only outputs.

@Alizter
Copy link
Collaborator

Alizter commented Dec 14, 2023

Both PRs rely on checking whether the --explain flags was passed to Menhir. I know little about Dune but this sounds weird to me. Why not introduce a new (explain) field in the menhir stanza, if we want to special-case the build system behavior?

We essentially already have that in #9025. The issue is that we are trying to support users passing --explain from an env stanza in their dune-workspace file. If we had an (explain) field, users would have to edit their menhir stanzas directly rather than being able to tweak the environment in developer mode. It might also cause confusion if they end up passing --explain and it doesn't work.

Menhir in general relies on options that generate new files -- and I am sure that many other metaprogramming tools follow the same UI pattern. It would be nice if Dune sandboxing could be taught to run the menhir command in a clean subdirectory (with just the parser file and possibly other dependencies specified by in the (menhir ...) stanza), and preserve the whole subdirectory somewhere in the build artifacts (for example: foo.mly.objs/). The build system may not be aware of some of the files produced, but this is fine as long as they are only outputs.

This could be an option. Rather than having case-by-case rules in the menhir implementation, we could simplify it and have menhir produce a directory target for each use case. This would avoid us having to specify each target. I believe menhir rules were added before directory targets were in a usable state, but I don't see any problems having such a feature now.

We discussed doing something similar with Coq's extraction feature, which is similar in menhir in the way the rules are written.

@emillon Does having menhir rules produce directory targets sound like a viable solution?

@gasche
Copy link
Member

gasche commented Dec 14, 2023

The issue is that we are trying to support users passing --explain from an env stanza in their dune-workspace file

But apparently this makes the implementation more complex, and the end result is that we don't have the feature. I appreciate that you did all this effort, but I would rather have something simple that works.

@ejgallego
Copy link
Collaborator

Why not introduce a new (explain) field in the menhir stanza, if we want to special-case the build system behavior?

Indeed that seems a good idea, barring other more optimal (but also complex) solutions. This is a very important feature, and the (explain) field can be implemented in 30 mins.

@nojb
Copy link
Collaborator

nojb commented Dec 15, 2023

  • Why not introduce a new (explain) field in the menhir stanza, if we want to special-case the build system behavior?

I gave this one a try: #9512.

@rgrinberg rgrinberg added this to the 3.13.0 milestone Dec 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
8 participants