From 6b4de44461dbd872d12eca8721f24b8756f2ee99 Mon Sep 17 00:00:00 2001 From: Qyriad Date: Tue, 24 Feb 2026 12:12:27 +0100 Subject: [PATCH] nix-env/query: don't use RAII for closing xml tag I could instead add the right braces or manually ensure the XMLOpenElement destructor is run at the right time. Or I could just write what I actually want to happen. Fixes #1137. Change-Id: Ie1f157de1632571fb0c21e26d8e3cae56a6a6964 --- lix/legacy/nix-env.cc | 5 ++++- .../commands/test_env_query_xml.py | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 tests/functional2/commands/test_env_query_xml.py diff --git a/lix/legacy/nix-env.cc b/lix/legacy/nix-env.cc index 6bd21a348..99ad20ba8 100644 --- a/lix/legacy/nix-env.cc +++ b/lix/legacy/nix-env.cc @@ -1137,7 +1137,7 @@ static void opQuery(Globals & globals, Strings opFlags, Strings opArgs) Table table; std::ostringstream xmlStream; XMLWriter xml(true, xmlStream); - XMLOpenElement xmlRoot(xml, "items"); + xml.openElement("items"); for (auto & i : elems) { try { @@ -1366,6 +1366,9 @@ static void opQuery(Globals & globals, Strings opFlags, Strings opArgs) } } + // + xml.closeElement(); + if (!xmlOutput) { pager << formatTable(table); } else { diff --git a/tests/functional2/commands/test_env_query_xml.py b/tests/functional2/commands/test_env_query_xml.py new file mode 100644 index 000000000..e47af13c7 --- /dev/null +++ b/tests/functional2/commands/test_env_query_xml.py @@ -0,0 +1,22 @@ +from xml.etree import ElementTree + +from testlib.fixtures.file_helper import with_files +from testlib.fixtures.nix import Nix +from testlib.utils import get_global_asset_pack + + +@with_files(get_global_asset_pack("dependencies")) +def test_correct_xml(nix: Nix): + """Test that nix-env --query --xml produces valid XML.""" + query_output = ( + nix.nix_env( + ["--query", "--available", "--attr-path", "--xml", "--file", "./dependencies.nix"] + ) + .run() + .ok() + .stdout_plain + ) + + # Should parse without raising. + parsed = ElementTree.fromstring(query_output) + assert parsed.tag == "items"