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

Unix.open_process_args_*: look up program in PATH #10084

Merged
merged 9 commits into from Dec 21, 2020

Conversation

nojb
Copy link
Contributor

@nojb nojb commented Dec 13, 2020

Fixes #10083, see the discussion in #10057, especially #10057 (comment) and #10057 (comment)

cc @xavierleroy

@@ -879,7 +879,7 @@ val open_process_args_in : string -> string array -> in_channel
command to run, and the second argument specifies the argument array passed
to the command. This function runs the command in parallel with the program.
The standard output of the command is redirected to a pipe, which can be read
via the returned input channel.
via the returned input channel. The program is looked up in the [PATH].
Copy link
Member

@gasche gasche Dec 13, 2020

Choose a reason for hiding this comment

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

I think we would need a @since marker for the PATH lookup, or at least a clear indication in the documentation that older versions of OCaml may not do it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a clarification about this to the docstring, but without using a @since marker since it wasn't clear to me how to specify more than one such marker.

@@ -917,13 +917,13 @@ let create_process_gen usepath cmd args optenv
(if new_stderr = 2 then 2 else file_descr_not_standard new_stderr)
|] in
Fun.protect ~finally:close_after
(fun () -> spawn cmd args optenv usepath redirections)
(fun () -> spawn cmd args optenv true (* usepath *) redirections)
Copy link
Member

Choose a reason for hiding this comment

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

if spawn is not exported, we should make usepath a labelled argument instead of using comments to alleviate the problems of non-labelled boolean arguments.

(Looking at the code, I see that spawn is an external. Can external have labels too?)

Copy link
Contributor

Choose a reason for hiding this comment

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

Externals can have labeled and even optional arguments. There are examples elsewhere in this unix.ml file.

This said, spawn is not exported, and is called only twice in unix.ml, so it doesn't need to have a bulletproof interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I'm not adding a label as it seems a bit overkill in this case...

Changes Outdated
@@ -26,6 +26,11 @@ Working version

### Other libraries:

- #10084: Unix.open_process_args_* functions now look up the program in the PATH
Copy link

Choose a reason for hiding this comment

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

The glob only matches 3 of the 4 functions because of the trailing underscore 😈

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a breaking change, strictly speaking, so it should be listed with a * instead of a -.

Why is this breaking? Assume the current directory contains an executable named foo.exe, and PATH does not contain ., like it should. Unix.open_process_args "foo.exe" ... succeeds with the current implementation and fails with the new implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, I amended the Changes entry and the docstring for the function.

@nojb nojb force-pushed the unix_args_path branch 3 times, most recently from b1bb24c to 4fdb4ac Compare December 14, 2020 10:03
Copy link
Member

@gasche gasche left a comment

Choose a reason for hiding this comment

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

I am in favor of the change, which improves compatibility between the different platforms and seems more consistent with what our users actually want.

Changes Outdated
@@ -26,6 +26,12 @@ Working version

### Other libraries:

* #10084: Unix.open_process_args* functions now look up the program in the PATH.
This was already the case under Windows, but this is now also done under
Unix. Note that previusly the program was interpreted relative to the current
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Unix. Note that previusly the program was interpreted relative to the current
Unix. Note that previously the program was interpreted relative to the current

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, fixed.

@@ -879,7 +879,9 @@ val open_process_args_in : string -> string array -> in_channel
command to run, and the second argument specifies the argument array passed
to the command. This function runs the command in parallel with the program.
The standard output of the command is redirected to a pipe, which can be read
via the returned input channel.
via the returned input channel. The program is looked up in the [PATH] (this
Copy link
Member

Choose a reason for hiding this comment

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

Could you break a line before this new sentence, so that the behavior changes catches the eye?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Member

@gasche gasche left a comment

Choose a reason for hiding this comment

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

This is an approval stamp.

Before we merge, do we need to do anything specific to the fact that we are proposing a change to the sdlib that is "a bugfix" or "a breaking change" according to who you ask?

I was wondering if we needed more opinions on this; let me cc the @ocaml/caml-devel group, in case people want to chime in. If there is no negative feedback for about a week, please feel free to remind me to merge.

@nojb
Copy link
Contributor Author

nojb commented Dec 14, 2020

I was wondering if we needed more opinions on this; let me cc the @ocaml/caml-devel group, in case people want to chime in. If there is no negative feedback for about a week, please feel free to remind me to merge.

Sounds good, thanks!

Copy link
Contributor

@xavierleroy xavierleroy left a comment

Choose a reason for hiding this comment

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

I have zero problems with the change of semantics. Yes, technically it is a breaking change, so that's why it must be starred in the change log. But the previous behavior was an oversight, and the only way to do something useful with the previous behavior was to use absolute paths /bin/ls, in which case the new behavior is the same. So, I don't think we have an issue here.

Nonetheless I have hairs to split about the documentation strings, see below.

Comment on lines 884 to 885
The program is looked up in the [PATH] (this behaviour changed in 4.12;
previously the program was interpreted relative to the current directory).
Copy link
Contributor

Choose a reason for hiding this comment

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

Elsewhere in unix.mli we write "The executable file is searched in the path". (4 occurrences.)

Also, you first talk about "the command" and then "the program". For me, "the command" is a shell command line, as used in open_process_in for instance. "The program" is better.

Also, "the program was interpreted relative to the current directory" is still true: the path is not searched for programs like ./foo or foo/bar.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I pushed revised docs for the open_process_args* functions to make them more uniform with the rest of the docstrings in this file (also added a line about the environment passed to the new process, which was previously missing). Let me know what you think!

Copy link
Contributor

@xavierleroy xavierleroy left a comment

Choose a reason for hiding this comment

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

The new documentation is just fine (modulo a typo that I fixed by a direct push), thank you very much!

I'm so happy that I'll merge right now.

@xavierleroy xavierleroy merged commit b9172d4 into ocaml:trunk Dec 21, 2020
dbuenzli pushed a commit to dbuenzli/ocaml that referenced this pull request Mar 25, 2021
The `Unix.open_process_args*` used to treat their first arguments as a file name for the executable to be run, like `execv` does.

This commit ensures that the first argument is searched in the executable path, like `execvp` does.  This is a more useful behavior.

 Closes: ocaml#10083
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Usefulness of Unix.open_process_args* functions is limited by them not respecting PATH environment variable
4 participants