<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>docs/adr/0002-multiple-scanned-roots-share-a-virtual-top-level-dir.md - fork/gdu @ master</title>
<meta name="description" content="docs/adr/0002-multiple-scanned-roots-share-a-virtual-top-level-dir.md in fork/gdu at master.">
<meta property="og:title" content="docs/adr/0002-multiple-scanned-roots-share-a-virtual-top-level-dir.md - fork/gdu @ master">
<meta property="og:description" content="docs/adr/0002-multiple-scanned-roots-share-a-virtual-top-level-dir.md in fork/gdu at master.">
<meta property="og:type" content="website">
<meta property="og:site_name" content="git.julia.blue">
<link rel="stylesheet" href="/css/style.css?v=62">
<script type="module" src="/js/main.js?v=62"></script>
</head>
<body>
<main data-ref="master" data-tip="c636c61a7fb7f1f9a9ac9306790b41a011cbabb9">
<noscript>
  <div class="callout">
    <p>
      hey! this is a static git repo viewer. commit diffs, file history,
      older commits and other revisions won't load without javascript.
    </p>
    <p>
      you are still able to view source files on branch/tag tips,
      but the experience is degraded.
    </p>
  </div>
</noscript>
<header class="repo"><div>
<h1>fork/gdu</h1>
</div>
<pre class="clone">git clone https://git.julia.blue/fork/gdu</pre>
</header>
<p class="snapshot-commit"><span class="commit-author">Daniel Milde</span><span class="commit-message" data-commit="34f39961030d97ade7bb07e12c02e35bc6fcf314">feat: allow scanning multiple directories (#653)</span><span class="snapshot-sha sha" data-commit="34f39961030d97ade7bb07e12c02e35bc6fcf314">34f3996</span><time datetime="2026-09-02T21:19:46+02:00" title="2026-09-02 21:19:46 +0200">2026-09-02</time></p>
<header class="topbar"><details class="switcher"><summary>master</summary><div class="menu"><strong>branches</strong><a href="/fork/gdu/ref/ignore/">ignore</a><a class="current" href="/fork/gdu/ref/master/">master</a><a href="/fork/gdu/ref/subvolume/">subvolume</a><a class="all" href="/fork/gdu/refs">all refs →</a></div></details><nav class="crumbs"><a href="/fork/gdu/">gdu</a> / <a href="/fork/gdu/ref/master/tree/docs/">docs</a> / <a href="/fork/gdu/ref/master/tree/docs/adr/">adr</a> / <span class="cur">0002-multiple-scanned-roots-share-a-virtual-top-level-dir.md</span></nav>
<span class="view-stats"><span>3.6 KiB</span><span>75 lines</span></span><span class="actions"><a class="raw" href="/fork/gdu/raw/026bfe9687043a77489ac3afb06afb3b3dd5e22f/docs/adr/0002-multiple-scanned-roots-share-a-virtual-top-level-dir.md">raw</a></span></header>
<div class="markdown-view"><input type="radio" name="markdown-view" id="markdown-rendered" checked><label for="markdown-rendered">rendered</label><input type="radio" name="markdown-view" id="markdown-code"><label for="markdown-code">code</label><section class="readme rendered"><h1>2. Multiple scanned roots share a virtual top level directory</h1>
<p>Date: 2026-09-02</p>
<h2>Status</h2>
<p>Accepted</p>
<h2>Context</h2>
<p>gdu accepted exactly one directory to scan (<code>cobra.MaximumNArgs(1)</code>). Issue #423
asks for a subset of a large directory's children to be scanned together: with a
hundred subdirectories, scanning all of them takes minutes and scanning them one
at a time defeats the purpose, which is to compare them.</p>
<p>Everything downstream of the analyzer assumes a single-rooted tree. The TUI
anchors navigation on a <code>topDir</code>/<code>topDirPath</code> pair and stops the <code>/..</code> row at it;
<code>Dir.subtractStats</code> walks parent pointers to the root; the ncdu-compatible export
format holds exactly one root array. A second root has nowhere to go.</p>
<p>Two shapes were available:</p>
<ol>
<li>Keep N independent trees and teach every consumer to iterate them.</li>
<li>Give the N roots a synthetic parent, so there is still one tree.</li>
</ol>
<h2>Decision</h2>
<p>Multiple scanned roots are grouped under a <strong>virtual top level directory</strong>
(<code>analyze.CreateVirtualRootDir</code>), and the rest of gdu keeps working on the single
rooted tree it already expects.</p>
<p>The virtual root is a real <code>analyze.Dir</code> with no parent and no <code>BasePath</code>, named
<code>(multiple)</code>. Because <code>Dir.GetPath</code> prefers <code>BasePath</code> over the parent chain, the
roots below it keep reporting their true absolute path even though they now have
a parent — so rescan, deletion, shell spawning and path display need no special
handling once the user has navigated into a root.</p>
<p>Consequences of the virtual root having no path are handled by refusing the
operation rather than inventing one: <code>--change-cwd</code>, spawning a shell, browsing
to a parent (<code>--browse-parent-dirs</code>) and JSON export are unavailable while it is
displayed.</p>
<p>Rows directly under the virtual root are <strong>labelled with their absolute path</strong>
rather than their base name (<code>analyze.ItemDisplayName</code>). Two roots taken from
different parents routinely share a base name — <code>gdu /a/data /b/data</code> would
otherwise show <code>data</code> twice — and the absolute path is what the user typed.
Below the roots, ordinary base names resume.</p>
<p>Two related decisions:</p>
<ul>
<li><strong>Nested roots are rejected</strong>, not deduplicated. <code>gdu /a /a/b</code> counts <code>/a/b</code>
twice in every total gdu reports, and no silent resolution of that is
unsurprising. Exact duplicates are dropped, since they have one obvious
meaning.</li>
<li><strong>Export (<code>-o</code>) and storage (<code>--db</code>) accept one directory only.</strong> Both are
keyed by a single root, and inventing a multi-root encoding would break
compatibility with ncdu for a case nobody has asked for yet.</li>
</ul>
<h2>Consequences</h2>
<ul>
<li>Roots are scanned sequentially, not concurrently. The analyzer owns one set of
progress channels per scan and <code>ResetProgress</code> replaces them, so a second
concurrent scan on the same analyzer would close a closed channel. Progress
therefore restarts per root, and the modal reports <code>Scanning 2/3...</code>.</li>
<li><code>ResetProgress</code> also clears the analyzer's cancelled flag, so a cancel request
made between roots would be lost. The TUI tracks the request separately in
<code>scanCancelRequested</code> and stops the loop itself, keeping partial results as a
cancelled single scan does.</li>
<li>The non-interactive stdout mode gives up its memory-efficient <code>SimpleDir</code>
analyzer for multiple roots: <code>SimpleDir</code> deliberately panics on <code>SetParent</code> and
cannot be re-parented. <code>--top</code> and <code>--depth</code> already make the same trade.</li>
<li>Labelling roots by path means the directory marker (<code>/</code> in the TUI and in
stdout) has to be suppressed for them, since an absolute path already opens
with a separator. Filtering at the virtual root matches against the path shown,
not the base name, so what is typed matches what is on screen.</li>
</ul>
</section><div class="code-panel"><pre class="code src language-markdown"><span class="code-line"><a class="ln" id="L1" href="#L1">1</a><span class="code-text"><a-p>#</a-p> <a-tt>2. Multiple scanned roots share a virtual top level directory</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L2" href="#L2">2</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L3" href="#L3">3</a><span class="code-text">Date: 2026-09-02</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L4" href="#L4">4</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L5" href="#L5">5</a><span class="code-text"><a-p>##</a-p> <a-tt>Status</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L6" href="#L6">6</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L7" href="#L7">7</a><span class="code-text">Accepted</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L8" href="#L8">8</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L9" href="#L9">9</a><span class="code-text"><a-p>##</a-p> <a-tt>Context</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L10" href="#L10">10</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L11" href="#L11">11</a><span class="code-text">gdu accepted exactly one directory to scan (`cobra.MaximumNArgs(1)`). Issue #423</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L12" href="#L12">12</a><span class="code-text">asks for a subset of a large directory&#39;s children to be scanned together: with a</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L13" href="#L13">13</a><span class="code-text">hundred subdirectories, scanning all of them takes minutes and scanning them one</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L14" href="#L14">14</a><span class="code-text">at a time defeats the purpose, which is to compare them.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L15" href="#L15">15</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L16" href="#L16">16</a><span class="code-text">Everything downstream of the analyzer assumes a single-rooted tree. The TUI</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L17" href="#L17">17</a><span class="code-text">anchors navigation on a `topDir`/`topDirPath` pair and stops the `/..` row at it;</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L18" href="#L18">18</a><span class="code-text">`Dir.subtractStats` walks parent pointers to the root; the ncdu-compatible export</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L19" href="#L19">19</a><span class="code-text">format holds exactly one root array. A second root has nowhere to go.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L20" href="#L20">20</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L21" href="#L21">21</a><span class="code-text">Two shapes were available:</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L22" href="#L22">22</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L23" href="#L23">23</a><span class="code-text"><a-p>1. </a-p>Keep N independent trees and teach every consumer to iterate them.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L24" href="#L24">24</a><span class="code-text"><a-p>2. </a-p>Give the N roots a synthetic parent, so there is still one tree.</span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L25" href="#L25">25</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L26" href="#L26">26</a><span class="code-text"><a-p>## </a-p><a-tt>Decision</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L27" href="#L27">27</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L28" href="#L28">28</a><span class="code-text"><a-p>Multiple scanned roots are grouped under a **virtual top level directory**</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L29" href="#L29">29</a><span class="code-text"><a-p>(`analyze.CreateVirtualRootDir`), and the rest of gdu keeps working on the single</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L30" href="#L30">30</a><span class="code-text"><a-p>rooted tree it already expects.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L31" href="#L31">31</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L32" href="#L32">32</a><span class="code-text"><a-p>The virtual root is a real `analyze.Dir` with no parent and no `BasePath`, named</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L33" href="#L33">33</a><span class="code-text"><a-p>`(multiple)`. Because `Dir.GetPath` prefers `BasePath` over the parent chain, the</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L34" href="#L34">34</a><span class="code-text"><a-p>roots below it keep reporting their true absolute path even though they now have</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L35" href="#L35">35</a><span class="code-text"><a-p>a parent — so rescan, deletion, shell spawning and path display need no special</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L36" href="#L36">36</a><span class="code-text"><a-p>handling once the user has navigated into a root.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L37" href="#L37">37</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L38" href="#L38">38</a><span class="code-text"><a-p>Consequences of the virtual root having no path are handled by refusing the</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L39" href="#L39">39</a><span class="code-text"><a-p>operation rather than inventing one: `--change-cwd`, spawning a shell, browsing</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L40" href="#L40">40</a><span class="code-text"><a-p>to a parent (`--browse-parent-dirs`) and JSON export are unavailable while it is</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L41" href="#L41">41</a><span class="code-text"><a-p>displayed.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L42" href="#L42">42</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L43" href="#L43">43</a><span class="code-text"><a-p>Rows directly under the virtual root are **labelled with their absolute path**</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L44" href="#L44">44</a><span class="code-text"><a-p>rather than their base name (`analyze.ItemDisplayName`). Two roots taken from</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L45" href="#L45">45</a><span class="code-text"><a-p>different parents routinely share a base name — `gdu /a/data /b/data` would</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L46" href="#L46">46</a><span class="code-text"><a-p>otherwise show `data` twice — and the absolute path is what the user typed.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L47" href="#L47">47</a><span class="code-text"><a-p>Below the roots, ordinary base names resume.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L48" href="#L48">48</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L49" href="#L49">49</a><span class="code-text"><a-p>Two related decisions:</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L50" href="#L50">50</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L51" href="#L51">51</a><span class="code-text"><a-p>- **Nested roots are rejected**, not deduplicated. `gdu /a /a/b` counts `/a/b`</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L52" href="#L52">52</a><span class="code-text"><a-p>  twice in every total gdu reports, and no silent resolution of that is</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L53" href="#L53">53</a><span class="code-text"><a-p>  unsurprising. Exact duplicates are dropped, since they have one obvious</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L54" href="#L54">54</a><span class="code-text"><a-p>  meaning.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L55" href="#L55">55</a><span class="code-text"><a-p>- **Export (`-o`) and storage (`--db`) accept one directory only.** Both are</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L56" href="#L56">56</a><span class="code-text"><a-p>  keyed by a single root, and inventing a multi-root encoding would break</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L57" href="#L57">57</a><span class="code-text"><a-p>  compatibility with ncdu for a case nobody has asked for yet.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L58" href="#L58">58</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L59" href="#L59">59</a><span class="code-text"><a-p>## </a-p><a-tt>Consequences</a-tt></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L60" href="#L60">60</a><span class="code-text"></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L61" href="#L61">61</a><span class="code-text"><a-p>- Roots are scanned sequentially, not concurrently. The analyzer owns one set of</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L62" href="#L62">62</a><span class="code-text"><a-p>  progress channels per scan and `ResetProgress` replaces them, so a second</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L63" href="#L63">63</a><span class="code-text"><a-p>  concurrent scan on the same analyzer would close a closed channel. Progress</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L64" href="#L64">64</a><span class="code-text"><a-p>  therefore restarts per root, and the modal reports `Scanning 2/3...`.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L65" href="#L65">65</a><span class="code-text"><a-p>- `ResetProgress` also clears the analyzer&#39;s cancelled flag, so a cancel request</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L66" href="#L66">66</a><span class="code-text"><a-p>  made between roots would be lost. The TUI tracks the request separately in</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L67" href="#L67">67</a><span class="code-text"><a-p>  `scanCancelRequested` and stops the loop itself, keeping partial results as a</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L68" href="#L68">68</a><span class="code-text"><a-p>  cancelled single scan does.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L69" href="#L69">69</a><span class="code-text"><a-p>- The non-interactive stdout mode gives up its memory-efficient `SimpleDir`</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L70" href="#L70">70</a><span class="code-text"><a-p>  analyzer for multiple roots: `SimpleDir` deliberately panics on `SetParent` and</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L71" href="#L71">71</a><span class="code-text"><a-p>  cannot be re-parented. `--top` and `--depth` already make the same trade.</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L72" href="#L72">72</a><span class="code-text"><a-p>- Labelling roots by path means the directory marker (`/` in the TUI and in</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L73" href="#L73">73</a><span class="code-text"><a-p>  stdout) has to be suppressed for them, since an absolute path already opens</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L74" href="#L74">74</a><span class="code-text"><a-p>  with a separator. Filtering at the virtual root matches against the path shown,</a-p></span><span class="code-break">
</span></span><span class="code-line"><a class="ln" id="L75" href="#L75">75</a><span class="code-text"><a-p>  </a-p>not the base name, so what is typed matches what is on screen.</span></span></pre></div></div>
</main>
</body>
</html>
