Skip to content

ocamldep: add -strict (error if input doesnt exist)#11593

Closed
emillon wants to merge 1 commit intoocaml:trunkfrom
emillon:ocamldep-strict
Closed

ocamldep: add -strict (error if input doesnt exist)#11593
emillon wants to merge 1 commit intoocaml:trunkfrom
emillon:ocamldep-strict

Conversation

@emillon
Copy link
Copy Markdown
Contributor

@emillon emillon commented Oct 3, 2022

Closes #8625

See oxcaml/oxcaml#892

When a nonexistent input file is passed to ocamldep, it is ignored and the error is not reported. This PR adds a -strict flag that prevents this behavior.

Consider the following situation:

  • on one side, ocamldep is being invoked as ocamldep -modules a.ml
  • on the other side, a.ml is unlinked and created again.

(this corresponds to what happens when dune is running and hg or git is updating its tree in the meantime - updates are not always atomic if hardlinks are involved for example)

We want to make sure that when ocamldep exits with 0, its output corresponds to either the old or the new version of the file. This is enabled by -strict.

@emillon
Copy link
Copy Markdown
Contributor Author

emillon commented Oct 3, 2022

(as for the PR itself, I tried to add some tests to cover this but let me know if there's a better way to do this or if this should be expanded to cover more cases - in our use case, -modules is the thing that matters.)

@emillon
Copy link
Copy Markdown
Contributor Author

emillon commented Oct 3, 2022

cc @snowleopard

Copy link
Copy Markdown
Contributor

@nojb nojb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM generally; the manual needs to be updated though.

@emillon
Copy link
Copy Markdown
Contributor Author

emillon commented Oct 3, 2022

@nojb thanks. I amended ocamldep.1 and ocamldep.etex (and also reworded to "exit with nonzero exit code")

@shindere
Copy link
Copy Markdown
Contributor

shindere commented Oct 3, 2022 via email

@shindere
Copy link
Copy Markdown
Contributor

shindere commented Oct 3, 2022 via email

@shindere
Copy link
Copy Markdown
Contributor

shindere commented Oct 3, 2022 via email

@@ -0,0 +1,8 @@
(* TEST
modules = "dep.ml a.ml"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is not necessary to test both with, and without -modules, since the behaviour being changed is orthogonal to this flag. I would leave only the one without -modules and remove the one with -modules.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the ones with -modules. I'd like to implement @shindere 's suggestion to group things in a single test, but I'm not sure to do it. This is my first time with ocamltest. How can I specify that the same test should be run with different options and different expected outcomes?

* setup-ocamlc.byte-build-env
** ocamlc.byte
commandline = "-depend a.ml doesnotexist.ml"
*** check-ocamlc.byte-output
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we want to test the output of ocamldep in this test; the feature being added seems unrelated. Perhaps let's simplify the patch by removing this test step and the "reference" file?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. There's one gotcha in the current implementation though: if we pass as input A B and C, and B does not exist, the output will still contain dependency information for A and C (but with a nonzero exit code). That test kind of characterizes that behaviour so that's why I included it. But yes it can be argued that the output of a failing command is not to be relied upon, so I'm fine with removing the check on the output.

@shindere
Copy link
Copy Markdown
Contributor

shindere commented Oct 3, 2022 via email

@emillon emillon force-pushed the ocamldep-strict branch 2 times, most recently from cf185c2 to a0b1c62 Compare October 7, 2022 09:16
@shindere
Copy link
Copy Markdown
Contributor

shindere commented Oct 9, 2022 via email

@shindere
Copy link
Copy Markdown
Contributor

shindere commented Oct 9, 2022 via email

@shindere
Copy link
Copy Markdown
Contributor

shindere commented Oct 11, 2022 via email

@mshinwell
Copy link
Copy Markdown
Contributor

@emillon I wonder if you might consider a slightly different fix, as per this PR: oxcaml/oxcaml#892

This removes the file-exists test entirely in strict mode, relying on the existing exception handler around the "open" call (which might be in a ppx subprocess). This should avoid a race where the file is deleted between the file-exists test and the open.

@nojb
Copy link
Copy Markdown
Contributor

nojb commented Dec 30, 2022

Friendly ping.

@mshinwell made a suggestion about a different implementation which sounds slightly superior (as it avoids a race condition). @emillon: do you think you could give it a shot?

@nojb nojb self-assigned this Feb 23, 2023
@nojb
Copy link
Copy Markdown
Contributor

nojb commented Feb 23, 2023

Friendly ping. @emillon: are you still interested in working on this issue?

@snowleopard
Copy link
Copy Markdown

I'd like to add that at Jane Street we've been happily using @mshinwell's patch in our build system for a while.

@emillon
Copy link
Copy Markdown
Contributor Author

emillon commented Feb 24, 2023

Sorry for the long silence. I wasn't sure what was the situation at JS. I'll update this with @mshinwell's patch.

@nojb
Copy link
Copy Markdown
Contributor

nojb commented Feb 24, 2023

Sorry for the long silence. I wasn't sure what was the situation at JS. I'll update this with @mshinwell's patch.

Thanks!

@nojb
Copy link
Copy Markdown
Contributor

nojb commented Mar 11, 2023

Friendly ping. Any news?

@emillon
Copy link
Copy Markdown
Contributor Author

emillon commented Mar 13, 2023

I updated the PR and the test suite output. Let me know what you think. Sorry for leaving that aside.

Closes ocaml#8625
See oxcaml/oxcaml#892

When a nonexistent input file is passed to ocamldep, it is ignored and
the error is not reported. This PR adds a `-strict` flag that prevents
this behavior.

Consider the following situation:
- on one side, ocamldep is being invoked as `ocamldep -modules a.ml`
- on the other side, `a.ml` is unlinked and created again.

(this corresponds to what happens when `dune` is running and `hg` or
`git` is updating its tree in the meantime - updates are not always
atomic if hardlinks are involved for example)

We want to make sure that when ocamldep exits with 0, its output
corresponds to either the old or the new version of the file. This is
enabled by `-strict`.
Copy link
Copy Markdown
Contributor

@nojb nojb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks generally good. I'm only wondering about the exact semantics of the new flag. Given the placement of the test in a try catch clause, it looks like "strict" mode will apply to any error during the processing of the file (eg syntax error), not just in case the file does not exist.

This seems fine to me; but if that's the case, I would like the documentation appearing in the help text of the tool and the manual/man page to be adapted accordingly.

with x -> begin
print_exception x;
if not !allow_approximation then begin
if !strict || not !allow_approximation then begin
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I understand correctly that in -strict mode the tool will exit with a non-zero code also if the file contains a syntax error or some other is raised when processing it (not just if the file does not exist)? If yes, the documentation should be adapted accordingly.

@nojb
Copy link
Copy Markdown
Contributor

nojb commented May 14, 2025

PR open for two years without any activity. As assignee, I am moving to close it; to be reopened as needed.

@nojb nojb closed this May 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ocamldep: add a robust mode

5 participants