-
Notifications
You must be signed in to change notification settings - Fork 412
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
Fix staged pps on Windows #10869
Fix staged pps on Windows #10869
Conversation
Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com>
An alternative approach is to quote the path of the executable. Also, you might want to further escape commands that are meant to run from "cmd.exe" (see the second code snippet below) Here are two functions extracted from the ocamlbuild codebase let quote_filename_for_cmd_if_needed x =
(* external commands are unfortunately called with cmd.exe (via Sys.command).
Cmd.exe has strange quoting rules. The most notorious quirk is, that
you can't use forward slashes as path separators at the first position,
unless you quote the expression explicitly.
cmd.exe will interpret the slash and everything thereafter as first
parameter. Eg. 'bin/foo -x' is treated like 'bin /foo -x'. *)
if Shell.is_simple_filename x && not (String.contains x '/')
then x
else My_std.quote_cmd ("\"" ^ x ^ "\"")
in (* See https://learn.microsoft.com/en-us/archive/blogs/twistylittlepassagesallalike/everyone-quotes-command-line-arguments-the-wrong-way *)
let quote_cmd s =
let b = Buffer.create (String.length s + 20) in
String.iter
(fun c ->
match c with
| '(' | ')' | '!' | '^' | '%' | '\"' | '<' | '>' | '&' | '|' ->
Buffer.add_char b '^'; Buffer.add_char b c
| _ ->
Buffer.add_char b c)
s;
Buffer.contents b |
It would be nice to introduce a test demonstrating the issue and its resolution. |
Indeed. I tried it but the approach seemed to be more complicated ultimately. One difficulty is that there are several layers of quoting (Dune quotes,
Any test using |
Here is the output of one of the tests in the testsuite (slightly adapted) with the current patch (the failure at the end is expected):
|
https://github.com/ocaml/dune/actions/runs/10656796066/job/29535855125?pr=10869 seems to perform some testing on windows |
Signed-off-by: nojebar <nicolas.ojeda.bar@lexifi.com>
Well spotted; I added a |
I would assume that dune does the correct escaping on Unix already. Maybe you only need to escape for "cmd.exe" ? |
Thanks for prodding me to investigate this further. It turns out that a similar issue affects the compiler: ocaml/ocaml#13415. Once that issue is fixed, one can fix the issue here simply by using |
Signed-off-by: nojebar <nicolas.ojeda.bar@lexifi.com>
Signed-off-by: nojebar <nicolas.ojeda.bar@lexifi.com>
I have pushed a revised version using quoting instead of replacing forward slashes by backslashes, which seems a more general solution. Since the compiler version of The patch itself ends up being bigger, but it makes more sense. |
Thanks so much folks, patch tested using Version as of right now still fails with:
from
|
Managed to finish compiling So indeed, no other showstopper for native Opam 2.2 support that I could see. |
Thanks @ejgallego for the confirmation that the first fix works. I was hoping the second one would work as well, but the number of nested quotations in the log makes my head hurt :) Anyway, I will try to see if I can figure out what is going on. Otherwise, maybe we will have to fall back to the first fix for now. |
Discussion upstream ocaml/ocaml#13417 (comment) also points to another advantage of the "backslash" solution: it does not increase the total command length, whose limit is which is pretty tight on Windows. Accordingly, I am planning to revise the patch to go back to the first solution. |
Signed-off-by: nojebar <nicolas.ojeda.bar@lexifi.com>
otherlibs/stdune/src/string.ml
Outdated
| prog :: args -> | ||
let prog = | ||
if Sys.win32 && contains prog '/' | ||
then concat ~sep:"\\" (split_on_char ~sep:'/' prog) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use String.map directly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, thanks.
otherlibs/stdune/src/string.ml
Outdated
then concat ~sep:"\\" (split_on_char ~sep:'/' prog) | ||
else prog | ||
in | ||
prog :: List.map ~f:quote_for_shell args |> concat ~sep:" " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
prog
is no longer quoted_for_shell. Is it expected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it is wrong: fixed in 4d27a90
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Signed-off-by: nojebar <nicolas.ojeda.bar@lexifi.com>
I'm happy with the fix in its current state. @hhugo: what do you think? @ejgallego: if you could confirm once again that the current state works for the Coq build, that would be ideal. |
Looks fine but I'm still surprised that we don't need any escaping for cmd.exe. |
You mean for the backslashes? The program path component is still quoted with |
I m referring to the escaping done by
|
This escaping would probably be needed if the command-line in question contained "enough" of these special metacharacters. In practice, they don't, so we don't see the need. We could try to implement this escaping regardless, but there are so many layers of (de)quoting (Dune ->
[Here we are using backslashes for the program path, as in this PR.] Upon seeing a double quote, Then
which is correct (the right-hand side of the "cookie" One case where this would break down would be if either the program path or the cookie value had embedded double quotes, but this is unusual. |
Can't the user pass arbitrary flags ? Can't that break the current logic? |
Yes, they will break the current logic if the flags contain things like embedded double quotes, etc. But that is somewhat orthogonal to the issue being treated here. To be clear, I am not saying that issue shouldn't be fixed. What I am saying is that this PR fixes one issue, which affects every use of staged ppxs in Dune: the presence of forward slashes in the program path. One could consider this as "quoting special characters" issue, but it has a simple solution: replace them by backward slashes. This fix is simple and localized and also, it doesn't increase the command length, which is nice. The more general issue of correctly quoting command-lines in all circumstances should be addressed separately, in my view, because: 1) it is less critical (it only triggers when flags containing certain unusual characters are used), and 2) more complicated to fix :) Makes sense? |
Sounds reasonable |
Merged, thanks for the help @hhugo @ejgallego ! |
Thanks to you! Confirmed that |
* Use backslashes when passing staged pps driver on Windows Signed-off-by: Nicolás Ojeda Bär <n.oje.bar@gmail.com>
CHANGES: ### Fixed - Show the context name for errors happening in non-default contexts. (ocaml/dune#10414, fixes ocaml/dune#10378, @jchavarri) - Correctly declare dependencies of indexes so that they are rebuilt when needed. (ocaml/dune#10623, @voodoos) - Don't depend on coq-stdlib being installed when expanding variables of the `coq.version` family (ocaml/dune#10631, fixes ocaml/dune#10629, @gares) - Error out if no files are found when using `copy_files`. (ocaml/dune#10649, @jchavarri) - Re_export dune-section private library in the dune-site library stanza, in order to avoid failure when generating and building sites modules with implicit_transitive_deps = false. (ocaml/dune#10650, fixes ocaml/dune#9661, @MA0100) - Expect test fixes: support multiple modes and fix dependencies when there is a custom runner (ocaml/dune#10671, @vouillon) - In a `(library)` stanza with `(extra_objects)` and `(foreign_stubs)`, avoid double linking the extra object files in the final executable. (ocaml/dune#10783, fixes ocaml/dune#10785, @nojb) - Map `(re_export)` library dependencies to the `exports` field in `META` files, and vice-versa. This field was proposed in to https://discuss.ocaml.org/t/proposal-a-new-exports-field-in-findlib-meta-files/13947. The field is included in Dune-generated `META` files only when the Dune lang version is >= 3.17. (ocaml/dune#10831, fixes ocaml/dune#10830, @nojb) - Fix staged pps preprocessors on Windows (which were not working at all previously) (ocaml/dune#10869, fixes ocaml/dune#10867, @nojb) - Fix `dune describe` when an executable is disabled with `enabled_if`. (ocaml/dune#10881, fixes ocaml/dune#10779, @moyodiallo) - Fix an issue where C stubs would be rebuilt whenever the stderr of Dune was redirected. (ocaml/dune#10883, fixes ocaml/dune#10882, @nojb) - Format long lists in s-expressions to fill the line instead of formatting them in a vertical way (ocaml/dune#10892, fixes ocaml/dune#10860, @nojb) - Fix the URL opened by the command `dune ocaml doc`. (ocaml/dune#10897, @gridbugs) - Fix the file referred to in the error/warning message displayed due to the dune configuration version not supporting a particular configuration stanza in use. (ocaml/dune#10923, @H-ANSEN) - Fix `enabled_if` when it uses `env` variable. (ocaml/dune#10936, fixes ocaml/dune#10905, @moyodiallo) - Fix exec -w for relative paths with --root argument (ocaml/dune#10982, @gridbugs) - Do not ignore the `(locks ..)` field in the `test` and `tests` stanza (ocaml/dune#11081, @rgrinberg) - Tolerate files without extension when generating merlin rules. (ocaml/dune#11128, @anmonteiro) ### Added - Make Merlin/OCaml-LSP aware of "hidden" dependencies used by `(implicit_transitive_deps false)` via the `-H` compiler flag. (ocaml/dune#10535, @voodoos) - Add support for the -H flag (introduced in OCaml compiler 5.2) in dune (requires lang versions 3.17). This adaptation gives the correct semantics for `(implicit_transitive_deps false)`. (ocaml/dune#10644, fixes ocaml/dune#9333, ocsigen/tyxml#274, ocaml/dune#2733, ocaml/dune#4963, @MA0100) - Add support for specifying Gitlab organization repositories in `source` stanzas (ocaml/dune#10766, fixes ocaml/dune#6723, @H-ANSEN) - New option to control jsoo sourcemap generation in env and executable stanza (ocaml/dune#10777, fixes ocaml/dune#10673, @hhugo) - One can now control jsoo compilation_mode inside an executable stanza (ocaml/dune#10777, fixes ocaml/dune#10673, @hhugo) - Add support for specifying default values of the `authors`, `maintainers`, and `license` stanzas of the `dune-project` file via the dune config file. Default values are set using the `(project_defaults)` stanza (ocaml/dune#10835, @H-ANSEN) - Add names to source tree events in performance traces (ocaml/dune#10884, @jchavarri) - Add `codeberg` as an option for defining project sources in dune-project files. For example, `(source (codeberg user/repo))`. (ocaml/dune#10904, @nlordell) - `dune runtest` can now run individual tests with `dune runtest mytest.t` (ocaml/dune#11041, @Alizter). - Wasm_of_ocaml support (ocaml/dune#11093, @vouillon) - Add a `coqdep_flags` field to the `coq` field of the `env` stanza, and to the `coq.theory` stanza, allowing to configure `coqdep` flags. (ocaml/dune#11094, @rlepigre) ### Changed - Remove all remnants of the experimental `patch-back-source-tree`. (ocaml/dune#10771, @rgrinberg) - Change the preset value for author and maintainer fields in the `dune-project` file to encourage including emails. (ocaml/dune#10848, @punchagan) - Tweak the preset value for tags in the `dune-project` file to hint at topics not having a special meaning. (ocaml/dune#10849, @punchagan) - Change some colors to improve readability in light-mode terminals (ocaml/dune#10890, @gridbugs) - Forward the linkall flag to jsoo in whole program compilation as well (ocaml/dune#10935, @hhugo) - Configurator uses `pkgconf` as pkg-config implementation when available and forwards it the `target` of `ocamlc -config`. (ocaml/dune#10937, @pirbo) - Enable Dune cache by default. Add a new Dune cache setting `enabled-except-user-rules`, which enables the Dune cache, but excludes user-written rules from it. This is a conservative choice that can avoid breaking rules whose dependencies are not correctly specified. This is the current default. (ocaml/dune#10944, ocaml/dune#10710, @nojb, @ElectreAAS) - Do not add `dune` dependency in `dune-project` when creating projects with `dune init proj`. The Dune dependency is implicitely added when generating opam files (ocaml/dune#11129, @Leonidas-from-XIV)
CHANGES: ### Fixed - Show the context name for errors happening in non-default contexts. (ocaml/dune#10414, fixes ocaml/dune#10378, @jchavarri) - Correctly declare dependencies of indexes so that they are rebuilt when needed. (ocaml/dune#10623, @voodoos) - Don't depend on coq-stdlib being installed when expanding variables of the `coq.version` family (ocaml/dune#10631, fixes ocaml/dune#10629, @gares) - Error out if no files are found when using `copy_files`. (ocaml/dune#10649, @jchavarri) - Re_export dune-section private library in the dune-site library stanza, in order to avoid failure when generating and building sites modules with implicit_transitive_deps = false. (ocaml/dune#10650, fixes ocaml/dune#9661, @MA0100) - Expect test fixes: support multiple modes and fix dependencies when there is a custom runner (ocaml/dune#10671, @vouillon) - In a `(library)` stanza with `(extra_objects)` and `(foreign_stubs)`, avoid double linking the extra object files in the final executable. (ocaml/dune#10783, fixes ocaml/dune#10785, @nojb) - Map `(re_export)` library dependencies to the `exports` field in `META` files, and vice-versa. This field was proposed in to https://discuss.ocaml.org/t/proposal-a-new-exports-field-in-findlib-meta-files/13947. The field is included in Dune-generated `META` files only when the Dune lang version is >= 3.17. (ocaml/dune#10831, fixes ocaml/dune#10830, @nojb) - Fix staged pps preprocessors on Windows (which were not working at all previously) (ocaml/dune#10869, fixes ocaml/dune#10867, @nojb) - Fix `dune describe` when an executable is disabled with `enabled_if`. (ocaml/dune#10881, fixes ocaml/dune#10779, @moyodiallo) - Fix an issue where C stubs would be rebuilt whenever the stderr of Dune was redirected. (ocaml/dune#10883, fixes ocaml/dune#10882, @nojb) - Fix the URL opened by the command `dune ocaml doc`. (ocaml/dune#10897, @gridbugs) - Fix the file referred to in the error/warning message displayed due to the dune configuration version not supporting a particular configuration stanza in use. (ocaml/dune#10923, @H-ANSEN) - Fix `enabled_if` when it uses `env` variable. (ocaml/dune#10936, fixes ocaml/dune#10905, @moyodiallo) - Fix exec -w for relative paths with --root argument (ocaml/dune#10982, @gridbugs) - Do not ignore the `(locks ..)` field in the `test` and `tests` stanza (ocaml/dune#11081, @rgrinberg) - Tolerate files without extension when generating merlin rules. (ocaml/dune#11128, @anmonteiro) ### Added - Make Merlin/OCaml-LSP aware of "hidden" dependencies used by `(implicit_transitive_deps false)` via the `-H` compiler flag. (ocaml/dune#10535, @voodoos) - Add support for the -H flag (introduced in OCaml compiler 5.2) in dune (requires lang versions 3.17). This adaptation gives the correct semantics for `(implicit_transitive_deps false)`. (ocaml/dune#10644, fixes ocaml/dune#9333, ocsigen/tyxml#274, ocaml/dune#2733, ocaml/dune#4963, @MA0100) - Add support for specifying Gitlab organization repositories in `source` stanzas (ocaml/dune#10766, fixes ocaml/dune#6723, @H-ANSEN) - New option to control jsoo sourcemap generation in env and executable stanza (ocaml/dune#10777, fixes ocaml/dune#10673, @hhugo) - One can now control jsoo compilation_mode inside an executable stanza (ocaml/dune#10777, fixes ocaml/dune#10673, @hhugo) - Add support for specifying default values of the `authors`, `maintainers`, and `license` stanzas of the `dune-project` file via the dune config file. Default values are set using the `(project_defaults)` stanza (ocaml/dune#10835, @H-ANSEN) - Add names to source tree events in performance traces (ocaml/dune#10884, @jchavarri) - Add `codeberg` as an option for defining project sources in dune-project files. For example, `(source (codeberg user/repo))`. (ocaml/dune#10904, @nlordell) - `dune runtest` can now run individual tests with `dune runtest mytest.t` (ocaml/dune#11041, @Alizter). - Wasm_of_ocaml support (ocaml/dune#11093, @vouillon) - Add a `coqdep_flags` field to the `coq` field of the `env` stanza, and to the `coq.theory` stanza, allowing to configure `coqdep` flags. (ocaml/dune#11094, @rlepigre) ### Changed - Remove all remnants of the experimental `patch-back-source-tree`. (ocaml/dune#10771, @rgrinberg) - Change the preset value for author and maintainer fields in the `dune-project` file to encourage including emails. (ocaml/dune#10848, @punchagan) - Tweak the preset value for tags in the `dune-project` file to hint at topics not having a special meaning. (ocaml/dune#10849, @punchagan) - Change some colors to improve readability in light-mode terminals (ocaml/dune#10890, @gridbugs) - Forward the linkall flag to jsoo in whole program compilation as well (ocaml/dune#10935, @hhugo) - Configurator uses `pkgconf` as pkg-config implementation when available and forwards it the `target` of `ocamlc -config`. (ocaml/dune#10937, @pirbo) - Enable Dune cache by default. Add a new Dune cache setting `enabled-except-user-rules`, which enables the Dune cache, but excludes user-written rules from it. This is a conservative choice that can avoid breaking rules whose dependencies are not correctly specified. This is the current default. (ocaml/dune#10944, ocaml/dune#10710, @nojb, @ElectreAAS) - Do not add `dune` dependency in `dune-project` when creating projects with `dune init proj`. The Dune dependency is implicitely added when generating opam files (ocaml/dune#11129, @Leonidas-from-XIV)
Fixes #10867
Dune passes a command line to the compiler (via the
-ppx
argument) which is then executed by the system shell,cmd.exe
on Windows. However, it appearscmd.exe
does not accept forward slashes as directory separator in the command part.Here we fix this by replacing forward slashes by backward slashes in the command path.