** Description changed:

+ [Impact]
+ 
+ When using tree with JSON (-J) or XML (-X) output on a nonexistent
+ directory, the generated output is malformed - missing required
+ opening tags/braces and fields. This makes the output invalid and
+ unusable for tools or scripts that parse it.
+ 
+ The fix ensures proper initialisation of markup output objects,
+ restoring valid JSON and XML generation.
+ 
+ [Test Plan]
+ 
+ 1. Start a fresh container and install `tree` and `jq`.
+ 
+ 2. Run the following command to check the JSON output:
+ 
+ ```sh
+ tree -J /nonexistentdir --noreport | jq
+ ```
+ 
+ If the bug is present, the following error message will be printed:
+ 
+ ```
+ jq: parse error: Expected value before ',' at line 2, column 1
+ ```
+ 
+ 3. Run this command to check the XML output:
+ 
+ ```sh
+ tree -X /nonexistentdir --noreport
+ ```
+ 
+ If the bug is present, the following error message will be printed:
+ 
+ ```
+ Error: XML syntax error on line 4: unexpected end element </unknown>
+ ```
+ 
+ 4. To verify that the command works when it's run against an existing
+    directory, run the following:
+ 
+ ```sh
+ mkdir -p /tmp/testdir/foo
+ [ $(tree -J /tmp/testdir/ | jq '.[0].contents[0].name') == '"foo"' ] || echo 
"test failed"
+ [ "$(tree -X /tmp/testdir/ | xq -q 'tree > directory > directory' -a name)" 
== 'foo' ] || echo "test failed"
+ ```
+ 
+ 5. Run `tree` without any option:
+ ```sh
+ tree /tmp/testdir/
+ ```
+ 
+ The successful execution of the command should produce the following
+ output:
+ 
+ ```
+ /tmp/testdir/
+ └── foo
+ 
+ 2 directories, 0 files
+ ```
+ 
+ [Where problems could occur]
+ 
+ The change affects how error cases are printed for invalid or
+ inaccessible directories. Potential regressions could include
+ incorrect JSON/XML or general command output format when running
+ `tree` against existing directories, which is addressed in the
+ testplan.
+ 
+ [Original report]
+ 
  using tree, if the target folder does not exist then the JSON or XML
  output options are broken.
  
  XML output (using input flag -X) has no open tag for the name field and
  a random close tag for 'unknown'.
  
  JSON output (using input flag -J) creates an array, the JSON object in
  the array has no open bracket and the entire 'type' key/value pair is
  missing. This makes the JSON object in the array start with a comma.
- 
  
  This issue was discovered on Ubuntu 24.04.2 LTS using tree version v2.1.1
  Functionality is correct and as expected on another machine running Ubuntu 
18.04.5 LTS using tree version v1.7.0
  
  Unfortunately I do not have another machine with anything newer than
  18.04.5 to test when the issue first appeared.
  
  ---------------------------------------------------------
  
  1.) $ lsb_release -rd
  No LSB modules are available.
  Description:    Ubuntu 24.04.2 LTS
  Release:        24.04
  
  2.) $ lsb_release -rd
  No LSB modules are available.
  Description:    Ubuntu 24.04.2 LTS
  Release:        24.04
  showhq@showhq-2025:/usr/lib/cgi-bin/tech$ tree --version
  tree v2.1.1 © 1996 - 2023 by Steve Baker, Thomas Moore, Francesc Rocher, 
Florian Sesser, Kyosuke Tokoro
  showhq@showhq-2025:/usr/lib/cgi-bin/tech$ apt-cache policy tree
  tree:
-   Installed: 2.1.1-2ubuntu3
-   Candidate: 2.1.1-2ubuntu3
-   Version table:
-  *** 2.1.1-2ubuntu3 500
-         500 http://us.archive.ubuntu.com/ubuntu noble/universe amd64 Packages
-         100 /var/lib/dpkg/status
+   Installed: 2.1.1-2ubuntu3
+   Candidate: 2.1.1-2ubuntu3
+   Version table:
+  *** 2.1.1-2ubuntu3 500
+         500 http://us.archive.ubuntu.com/ubuntu noble/universe amd64 Packages
+         100 /var/lib/dpkg/status
  
  3.) Expected: same as v1.7.0 - valid JSON with the -J flag and valid XML
  with the -X flag.
  
  4.) Reality: Invalid JSON and XML

** Changed in: tree (Ubuntu)
     Assignee: Simon Chopin (schopin) => Nadzeya Hutsko (nadzeya)

** Also affects: tree (Ubuntu Questing)
   Importance: Undecided
       Status: New

** Also affects: tree (Ubuntu Noble)
   Importance: Undecided
       Status: New

** Also affects: tree (Ubuntu Jammy)
   Importance: Undecided
       Status: New

** Also affects: tree (Ubuntu Plucky)
   Importance: Undecided
       Status: New

-- 
You received this bug notification because you are a member of Ubuntu
Bugs, which is subscribed to Ubuntu.
https://bugs.launchpad.net/bugs/2113790

Title:
  JSON and XML output broken if path invalid

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/tree/+bug/2113790/+subscriptions


-- 
ubuntu-bugs mailing list
[email protected]
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs

Reply via email to