Repository: conan-io/conan
Pull request: Quote build_args in conan graph build-order
Changelog: Fix: Quote `build_args` in `conan graph build-order -f=json` to avoid issues with options with spaces.
Changelog: Bugfix: `build_args` options in `graph build-order` now respect the context of the reference.
Docs: Omit



Closes https://github.com/conan-io/conan/issues/16593


Problem statement:
[bug] In build-order.json,  build_args is wrong when options have whitespaces
### Describe the bug

conan version: 2.4.1
conan profile:
```
[settings]
arch=x86_64
build_type=Release
compiler=gcc
compiler.cppstd=gnu17
compiler.libcxx=libstdc++11
compiler.version=12
os=Linux
```

Description: 

I am trying to build `boost/1.82.0` package in CI with some custom options and in particular: `boost/*:extra_b2_flags="define=BOOST_LOG_USE_STD_REGEX define=BOOST_LOG_WITHOUT_DEBUG_OUTPUT define=BOOST_LOG_WITHOUT_DEFAULT_FACTORIES define=BOOST_LOG_WITHOUT_EVENT_LOG define=BOOST_LOG_WITHOUT_IPC define=BOOST_LOG_WITHOUT_SETTINGS_PARSERS define=BOOST_LOG_WITHOUT_SYSLOG define=BOOST_LOG_WITHOUT_WCHAR_T define=BOOST_USE_WINAPI_VERSION=0x0A00"`
This option contains whitespaces.
When using `conan graph build-order --format json`, the resulting json has a `build_args` field that looks like: `--requires=boost/1.82.0 --build=boost/1.82.0 -o boost/*:extra_b2_flags=define=BOOST_LOG_USE_STD_REGEX define=BOOST_LOG_WITHOUT_DEBUG_OUTPUT define=BOOST_LOG_WITHOUT_DEFAULT_FACTORIES define=BOOST_LOG_WITHOUT_EVENT_LOG define=BOOST_LOG_WITHOUT_IPC define=BOOST_LOG_WITHOUT_SETTINGS_PARSERS define=BOOST_LOG_WITHOUT_SYSLOG define=BOOST_LOG_WITHOUT_WCHAR_T define=BOOST_USE_WINAPI_VERSION=0x0A00`
This is not valid arguments for `conan install` because `extra_b2_flags` option should be enclosed in quotes.

### How to reproduce it

With the following conanfile.txt:
```
[requires]
boost/1.82.0

[options]
boost/*:extra_b2_flags=define=BOOST_LOG_USE_STD_REGEX define=BOOST_LOG_WITHOUT_DEBUG_OUTPUT define=BOOST_LOG_WITHOUT_DEFAULT_FACTORIES define=BOOST_LOG_WITHOUT_EVENT_LOG define=BOOST_LOG_WITHOUT_IPC define=BOOST_LOG_WITHOUT_SETTINGS_PARSERS define=BOOST_LOG_WITHOUT_SYSLOG define=BOOST_LOG_WITHOUT_WCHAR_T define=BOOST_USE_WINAPI_VERSION=0x0A00
```

Run the following:
`$ conan graph build-order --order-by=configuration --build=boost/1.82.0 -pr:a default --format json .`

Inspect the resulting json and look at `build_args` field which is invalid.

Hints from issue discussion:
Hi @jeremy-coulon thanks a lot for taking the time to report the issue.
While it is true that Conan silently spliting the args is not quite expected without even a warning.

If I change
`boost/*:extra_b2_flags=define=BOOST_LOG_USE_STD_REGEX  ... define=BOOST_USE_WINAPI_VERSION=0x0A00`
with
`boost/*:extra_b2_flags="define=BOOST_LOG_USE_STD_REGEX  ... define=BOOST_USE_WINAPI_VERSION=0x0A00"`,
the json output correctly gets converted to
```
boost/*:extra_b2_flags=\"define=BOOST_LOG_USE_STD_REGEX define=BOOST_LOG_WITHOUT_DEBUG_OUTPUT ... define=BOOST_USE_WINAPI_VERSION=0x0A00\"
```

But this probably won't work within the boost recipe, as those quotes are now being passed all the way down to the compiler, which will probably complain about them when you try to create the package.

A proper solution here would be to have boost/b2 listen to the confs that conan provides for this exact reason, such as `tools.build:defines` etc. I'm talking about how we can best proceed there with @uilianries.

A question thus remains as to how Conan could help users in this case - we could try to quote them when necessary, we'll check and get back to you on that :)
I'm porposing https://github.com/conan-io/conan/pull/16594 which will quote the arguments in the build_args field to avoid issues like this. This might be just in time for the 2.5.0 release, will check with the team.

Again, thanks for taking the time to report your issue, we appreciate it!
Thanks for the pull request.

As for your suggestion to add quotes directly in my conanfile.txt, note that I already tried this workaround.
But it turned out to have an even worse problem. Now the package_id computed by `conan graph build-order` is different than the package_id computed by `conan install` with the `build_args` taken from the result of the build-order.

Commit message: Another approach?

Patch to review:
diff --git a/conans/client/graph/install_graph.py b/conans/client/graph/install_graph.py
index 65befe30de1..9acce95d924 100644
--- a/conans/client/graph/install_graph.py
+++ b/conans/client/graph/install_graph.py
@@ -1,5 +1,6 @@
 import json
 import os
+import shlex
 import textwrap
 
 from conan.api.output import ConanOutput
diff --git a/conans/model/options.py b/conans/model/options.py
index cbf6eaf83da..c18070bbe13 100644
--- a/conans/model/options.py
+++ b/conans/model/options.py
@@ -29,10 +29,14 @@ def dumps(self, scope=None):
         if self._value is None:
             return None
         important = "!" if self.important else ""
+        value = self._value
+        if " " in value:
+            value = value.replace('"', r'\"')
+            value = f'"{value}"'
         if scope:
-            return "%s:%s%s=%s" % (scope, self._name, important, self._value)
+            return "%s:%s%s=%s" % (scope, self._name, important, value)
         else:
-            return "%s%s=%s" % (self._name, important, self._value)
+            return "%s%s=%s" % (self._name, important, value)
 
     def copy_conaninfo_option(self):
         # To generate a copy without validation, for package_id info.options value
diff --git a/test/integration/command_v2/test_info_build_order.py b/test/integration/command_v2/test_info_build_order.py
index cf95afb5429..a65d5a3e658 100644
--- a/test/integration/command_v2/test_info_build_order.py
+++ b/test/integration/command_v2/test_info_build_order.py
@@ -9,7 +9,7 @@
 
 
 def test_info_build_order():
-    c = TestClient()
+    c = TestClient(light=True)
     c.save({"dep/conanfile.py": GenConanfile(),
             "pkg/conanfile.py": GenConanfile().with_requires("dep/0.1"),
             "consumer/conanfile.txt": "[requires]\npkg/0.1"})
@@ -75,7 +75,7 @@ def test_info_build_order():
 
 
 def test_info_build_order_configuration():
-    c = TestClient()
+    c = TestClient(light=True)
     c.save({"dep/conanfile.py": GenConanfile(),
             "pkg/conanfile.py": GenConanfile().with_requires("dep/0.1"),
             "consumer/conanfile.txt": "[requires]\npkg/0.1"})
@@ -129,7 +129,7 @@ def test_info_build_order_configuration():
 
 
 def test_info_build_order_configuration_text_formatter():
-    c = TestClient()
+    c = TestClient(light=True)
     c.save({"dep/conanfile.py": GenConanfile(),
             "pkg/conanfile.py": GenConanfile().with_requires("dep/0.1"),
             "consumer/conanfile.txt": "[requires]\npkg/0.1"})
@@ -144,7 +144,7 @@ def test_info_build_order_configuration_text_formatter():
 
 
 def test_info_build_order_build_require():
-    c = TestClient()
+    c = TestClient(light=True)
     c.save({"dep/conanfile.py": GenConanfile(),
             "pkg/conanfile.py": GenConanfile().with_tool_requires("dep/0.1"),
             "consumer/conanfile.txt": "[requires]\npkg/0.1"})
@@ -199,7 +199,7 @@ def test_info_build_order_build_require():
 
 
 def test_info_build_order_options():
-    c = TestClient()
+    c = TestClient(light=True)
     # The normal default_options do NOT propagate to build_requires, it is necessary to use
     # self.requires(..., options=xxx)
     c.save({"tool/conanfile.py": GenConanfile().with_option("myopt", [1, 2, 3]),
@@ -253,7 +253,7 @@ def test_info_build_order_options():
 
 
 def test_info_build_order_merge_multi_product():
-    c = TestClient()
+    c = TestClient(light=True)
     c.save({"dep/conanfile.py": GenConanfile(),
             "pkg/conanfile.py": GenConanfile().with_requires("dep/0.1"),
             "consumer1/conanfile.txt": "[requires]\npkg/0.1",
@@ -334,7 +334,7 @@ def test_info_build_order_merge_multi_product():
 
 
 def test_info_build_order_merge_multi_product_configurations():
-    c = TestClient()
+    c = TestClient(light=True)
     c.save({"dep/conanfile.py": GenConanfile(),
             "pkg/conanfile.py": GenConanfile().with_requires("dep/0.1"),
             "consumer1/conanfile.txt": "[requires]\npkg/0.1",
@@ -517,7 +517,7 @@ def test_info_build_order_lockfile_location():
     """ the lockfile should be in the caller cwd
     https://github.com/conan-io/conan/issues/13850
     """
-    c = TestClient()
+    c = TestClient(light=True)
     c.save({"dep/conanfile.py": GenConanfile("dep", "0.1"),
             "pkg/conanfile.py": GenConanfile("pkg", "0.1").with_requires("dep/0.1")})
     c.run("create dep")
@@ -529,7 +529,7 @@ def test_info_build_order_lockfile_location():
 
 def test_info_build_order_broken_recipe():
     # https://github.com/conan-io/conan/issues/14104
-    c = TestClient()
+    c = TestClient(light=True)
     dep = textwrap.dedent("""
         from conan import ConanFile
         from conan.tools.files import replace_in_file
@@ -549,7 +549,7 @@ def export(self):
 class TestBuildOrderReduce:
     @pytest.mark.parametrize("order", ["recipe", "configuration"])
     def test_build_order_reduce(self, order):
-        c = TestClient()
+        c = TestClient(light=True)
         c.save({"liba/conanfile.py": GenConanfile("liba", "0.1"),
                 "libb/conanfile.py": GenConanfile("libb", "0.1").with_requires("liba/0.1"),
                 "libc/conanfile.py": GenConanfile("libc", "0.1").with_requires("libb/0.1"),
@@ -625,7 +625,7 @@ def test_build_order_merge_reduce(self, order):
             assert level1[1]["depends"] == [liba2]
 
     def test_error_reduced(self):
-        c = TestClient()
+        c = TestClient(light=True)
         c.save({"conanfile.py": GenConanfile("liba", "0.1")})
         c.run("graph build-order . --format=json", redirect_stdout="bo1.json")
         c.run("graph build-order . --order-by=recipe --reduce --format=json",
@@ -637,7 +637,7 @@ def test_error_reduced(self):
         assert "ERROR: Reduced build-order file cannot be merged: bo2.json"
 
     def test_error_different_orders(self):
-        c = TestClient()
+        c = TestClient(light=True)
         c.save({"conanfile.py": GenConanfile("liba", "0.1")})
         c.run("graph build-order . --format=json", redirect_stdout="bo1.json")
         c.run("graph build-order . --order-by=recipe --format=json", redirect_stdout="bo2.json")
@@ -652,3 +652,25 @@ def test_error_different_orders(self):
         # different order
         c.run(f"graph build-order-merge --file=bo3.json --file=bo2.json", assert_error=True)
         assert "ERROR: Cannot merge build-orders of configuration!=recipe" in c.out
+
+
+def test_build_order_space_in_options():
+    tc = TestClient(light=True)
+    tc.save({"dep/conanfile.py": GenConanfile("dep", "1.0")
+                .with_option("flags", ["ANY", None])
+                .with_option("extras", ["ANY", None]),
+             "conanfile.txt": textwrap.dedent("""
+             [requires]
+             dep/1.0
+
+             [options]
+             dep/*:flags=define=FOO define=BAR define=BAZ
+             dep/*:extras=cxx="yes" gnuext="no"
+             """)})
+
+    tc.run("create dep")
+    tc.run("graph build-order . --order-by=configuration --build=dep/1.0 -f=json", redirect_stdout="order.json")
+    order = json.loads(tc.load("order.json"))
+    assert order["order"][0][0]["build_args"] == ''''--requires=dep/1.0 --build=dep/1.0 -o dep/*:extras="cxx=\\"yes\\" gnuext=\\"no\\"" -o dep/*:flags="define=FOO define=BAR define=BAZ"'''
+
+# '--requires=dep/1.0 --build=dep/1.0 -o dep/*:extras="\\"cxx=yes gnuext=no\\"" -o dep/*:flags="define=FOO define=BAR define=BAZ"'


Code review comments:
diff_hunk: @@ -29,10 +29,14 @@ def dumps(self, scope=None):
         if self._value is None:
             return None
         important = "!" if self.important else ""
+        value = self._value
+        if " " in value:
+            value = value.replace('"', r'\"')
+            value = f'"{value}"'
path: conans/model/options.py
text: @user1:
I think this shouldn't be done in all cases while dumping the options, it could be breaking.
I was thinking of doing this just in the generation of ``build_args`` that happens in the ``build-order``.

@author:
Yes, this is my same fear - it was just easier to test that this would work by doing it here, I should have marked this a draft to show that I'm just testing for now sorry :)

diff_hunk: @@ -652,3 +652,25 @@ def test_error_different_orders(self):
         # different order
         c.run(f"graph build-order-merge --file=bo3.json --file=bo2.json", assert_error=True)
         assert "ERROR: Cannot merge build-orders of configuration!=recipe" in c.out
+
+
+def test_build_order_space_in_options():
+    tc = TestClient(light=True)
+    tc.save({"dep/conanfile.py": GenConanfile("dep", "1.0")
+                .with_option("flags", ["ANY", None])
+                .with_option("extras", ["ANY", None]),
+             "conanfile.txt": textwrap.dedent("""
+             [requires]
+             dep/1.0
+
+             [options]
+             dep/*:flags=define=FOO define=BAR define=BAZ
+             dep/*:extras=cxx="yes" gnuext="no"
+             """)})
+
+    tc.run("create dep")
+    tc.run("graph build-order . --order-by=configuration --build=dep/1.0 -f=json", redirect_stdout="order.json")
+    order = json.loads(tc.load("order.json"))
+    assert order["order"][0][0]["build_args"] == ''''--requires=dep/1.0 --build=dep/1.0 -o dep/*:extras="cxx=\\"yes\\" gnuext=\\"no\\"" -o dep/*:flags="define=FOO define=BAR define=BAZ"'''
path: test/integration/command_v2/test_info_build_order.py
text: @author:
I think the two \\ here are a problem, the final json looks like
```
"--requires=dep/1.0 --build=dep/1.0  -o dep/*:extra=\"cxx=\\\"yes\\\"
``` 

in my terminal