Skip to content

pkg: minor fix is sys_poll library#11497

Merged
gridbugs merged 1 commit intoocaml:mainfrom
gridbugs:sys-poll-read-lines-fix
Mar 3, 2025
Merged

pkg: minor fix is sys_poll library#11497
gridbugs merged 1 commit intoocaml:mainfrom
gridbugs:sys-poll-read-lines-fix

Conversation

@gridbugs
Copy link
Copy Markdown
Collaborator

Io.String_path.lines_of_file raises Sys_error rather than Unix_error, so the way we were handling errors in sys_poll would not correctly handle cases where files are missing. Since Sys_error does not provide information about the nature of the error, explicitly check that the file exists and is a regular file rather than relying on exceptions.

Comment thread src/dune_pkg/sys_poll.ml Outdated
| exception Unix.Unix_error (Unix.ENOENT, _, _) -> None
if Sys.file_exists p && Sys.is_regular_file p
then Some (Io.String_path.lines_of_file p)
else None
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Wouldn't it be better to catch the Sys_error and then do the checks? That way the stat calls only happen on the exception case and don't need to be paid on every invocation of the function.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good point. I've changed it to optimistically read lines and to return None if Sys_error is raised. I'm not sure if I should keep the checks (Sys.file_exists p && Sys.is_regular_file p) in the exception case or just return None in response to all Sys_error exceptions.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sys.is_regular_file only exists starting with 5.1, you'd need to use stat instead, but for now I'd just leave it out until it proves to be a problem.

`Io.String_path.lines_of_file` raises `Sys_error` rather than
`Unix_error`.

Signed-off-by: Stephen Sherratt <stephen@sherra.tt>
@gridbugs gridbugs force-pushed the sys-poll-read-lines-fix branch from e5f87bf to 35e4460 Compare March 3, 2025 05:13
@gridbugs gridbugs merged commit 00c32ba into ocaml:main Mar 3, 2025
@gridbugs gridbugs deleted the sys-poll-read-lines-fix branch March 3, 2025 05:53
Leonidas-from-XIV pushed a commit to Leonidas-from-XIV/dune that referenced this pull request Mar 6, 2025
`Io.String_path.lines_of_file` raises `Sys_error` rather than
`Unix_error`.

Signed-off-by: Stephen Sherratt <stephen@sherra.tt>
anmonteiro pushed a commit to anmonteiro/dune that referenced this pull request Apr 22, 2025
`Io.String_path.lines_of_file` raises `Sys_error` rather than
`Unix_error`.

Signed-off-by: Stephen Sherratt <stephen@sherra.tt>
Sudha247 pushed a commit to Sudha247/dune that referenced this pull request Jul 23, 2025
`Io.String_path.lines_of_file` raises `Sys_error` rather than
`Unix_error`.

Signed-off-by: Stephen Sherratt <stephen@sherra.tt>
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.

3 participants