Skip to content

du counts hard-linked files once per path, so both size columns over-report #19019

Closed

Description

@victorhooi

Bug report form

  • I have done a basic search of the issue tracker to find any existing issues that are similar.
  • I have checked that my version is at least the latest stable release available via my installation method.

Describe the bug

nushell's du sums every directory entry it walks. However, if there are multiple hard links, the file size's is reported once per link - so both apparent and physical columns in the output report more space than the tree actually contains.

This bit me earlier - compare nushell's du output for my cargo cache:

foobar in ~/.cache/cargo-target
✦ ❯ du * | to md
path apparent physical
/Users/victorhooi/.cache/cargo-target/CACHEDIR.TAG 177 B 4.0 kB
/Users/victorhooi/.cache/cargo-target/debug 178.6 GB 179.1 GB
/Users/victorhooi/.cache/cargo-target/doc 52.7 MB 57.2 MB
/Users/victorhooi/.cache/cargo-target/release 779.0 MB 788.7 MB
/Users/victorhooi/.cache/cargo-target/tmp 64 B 0 B

vs GNU du (well, technically I think it's the BSD version):

foobar in ~/.cache/cargo-target took 5s
✦ ❯ ^du -chs *
4.0K    .rustc_info.json
4.0K    .rustdoc_fingerprint.json
4.0K    CACHEDIR.TAG
 64G    debug
 55M    doc
752M    release
  0B    tmp
 65G    total

I thought I was going crazy, so I used dua to confirm:

foobar in ~/.cache/cargo-target took 2s
✦ ❯ dua
      0  B tmp
   4.10 KB .rustc_info.json
   4.10 KB .rustdoc_fingerprint.json
   4.10 KB CACHEDIR.TAG
  57.23 MB doc
 788.77 MB release
  68.86 GB debug
  69.71 GB total

In this case - rustc hard-links resued codegen units into deps/, which is what caused nushell's du to get tripped up.

AFAIK, Nushell's behaviour differs from every other du:

  • POSIX: "A file that occurs multiple times shall be counted and written for only one entry, even if the occurrences are under different file operands." (link)
  • GNU coreutils: Default behaviour is to count each hard-link once. You need to use "--count-links" to override the default. (link) (They also have a test specifically for hard links).
  • BSD/macOS: Same as GNU coreutils - default is to count files with multiple hard links only once. You can use the -l flag to override the default.

physical is also off: it is computed from allocated blocks and labelled as disk usage, but reports space that is not consumed.

Previously raised as #1317 (2020), but it was tagged as category:enhancement and delight and auto-closed by the bot. However, I'd argue this isn't a missing nicety - this is a column that actively reports the wrong number.

How to reproduce

󰀵 foobar in /tmp via 🐍 v3.14.7 via 🦀
✦ ❯ mkdir /tmp/du-links

󰀵 foobar in /tmp via 🐍 v3.14.7 via 🦀
✦ ❯ dd if=/dev/zero of=/tmp/du-links/a bs=1m count=100
100+0 records in
100+0 records out
104857600 bytes transferred in 0.065729 secs (1595301922 bytes/sec)

󰀵 foobar in /tmp via 🐍 v3.14.7 via 🦀
✦ ❯ ln /tmp/du-links/a /tmp/du-links/b

󰀵 foobar in /tmp via 🐍 v3.14.7 via 🦀
✦ ❯ ln /tmp/du-links/a /tmp/du-links/c

󰀵 foobar in /tmp via 🐍 v3.14.7 via 🦀
✦ ❯ du -sh /tmp/du-links
Error: nu::parser::unknown_flag

  × The `du` command doesn't have flag `-s`.
   ╭─[repl_entry #6:1:5]
 1 │ du -sh /tmp/du-links
   ·     ┬
   ·     ╰── unknown flag
   ╰────
  help: Use `--help` to see available flags


󰀵 foobar in /tmp via 🐍 v3.14.7 via 🦀
✦ ❯ du /tmp/du-links
╭───┬───────────────┬──────────┬──────────╮
│ # │     path      │ apparent │ physical │
├───┼───────────────┼──────────┼──────────┤
│ 0 │ /tmp/du-links │ 314.5 MB │ 314.5 MB │
╰───┴───────────────┴──────────┴──────────╯

󰀵 foobar in /tmp via 🐍 v3.14.7 via 🦀
✦ ❯ ^du /tmp/du-links
204800  /tmp/du-links

󰀵 foobar in /tmp via 🐍 v3.14.7 via 🦀

󰀵 foobar in /tmp via 🐍 v3.14.7 via 🦀
❯ ^du -chs /tmp/du-links
100M    /tmp/du-links
100M    total

Expected behavior

apparent and physical should each count a file once per invocation, regardless of how many hard links reach it - so 104.9 MB / 104.9 MB above.

Configuration

key value
version 0.115.1
os macOS 25.6.0 (aarch64-darwin)
filesystem APFS

Activity

  1. added
    status:needs-triage
    on Sep 13, 2026
  2. fdncred commented on Sep 13, 2026

    @fdncred
    Contributor

    Ya, sounds like a bug. Sounds like it was closed before because no one cared enough to make a PR. We'll see what happens this time.

  3. victorhooi commented on Sep 13, 2026

    @victorhooi
    Author

    I filed a PR

    #19020

    Disclaimer: Stole/borrowed the logic from GNU coreutils, and used AI to help with the rust.

  4. fdncred commented on Sep 13, 2026

    @fdncred
    Contributor

    I don't believe we can use the PR if since it came from GNU coreutils. They are GPL 3.0 licensed which is not compatible with our MIT license.

  5. victorhooi commented on Sep 13, 2026

    @victorhooi
    Author

    @fdncred Not sure if you saw my comment on the PR - I did post after it was closed though.

    I think there was a mixup here - my original wording may have been imprecise, and I was being self-deprecating. This is my first nushell PR. I meant I "borrowed" (as in, read) the POSIX specifications (per my link to the spec above). I also looked at GNU coreutils and BSD du, and tested them myself, to see what behaviour they implemented, to ensure nobody would think I was making up some weird behaviour that had no precedent.

    No code was copied or ported.

    The rust in the PR was written against nushell's own crates/nu-command/src/platform/dir_info.rs. What I took from coreutils was the approach - keep a set keyed on the device-inode pair, and count a file the first time its pair goes into the set - plus their vocabulary in the comments ("device-inode pair"), specifically so it'd be obvious I wasn't inventing new behaviour (insert meme of Mugatu from Zoolander...)

    I know this issue was first opened up in 2020, and nobody wanted to fix it then - and I'd be very keen to actually see it get fixed now in 2026 =).

    I also have a bad habit of not keeping an eye on things like disk-space - so having a working du in nushell would be pretty useful for me.

  6. fdncred commented on Sep 13, 2026

    @fdncred
    Contributor

    I took it to mean that you looked at the GPL3 code and implemented a version similar to it. I'm not lawyer but I still think that violates the MIT license for us to use it.

  7. victorhooi commented on Sep 14, 2026

    @victorhooi
    Author

    Thanks for sticking with this - I appreciate it, and I'm keen to see bugs like this in nushell quashed. And I do get being extra cautious.

    On the principle. I'm not a lawyer either, but this part isn't really contested: the GPL is a copyright licence, so it reaches as far as copyright does, which is the expression (the actual code), not the technique. 17 U.S.C. §102(b): "In no case does copyright protection for an original work of authorship extend to any idea, procedure, process, system, method of operation, concept, principle, or discovery". Reading GPL code and then writing your own isn't a violation; copying its expression is.

    I do understand why uutils goes further and bans reading GNU source outright - it makes "nothing was copied" provable without anyone auditing a diff. That's a project guardrail though, not the licence's reach, and nushell's own CONTRIBUTING only rules out GPL crates.

    On this PR specifically. There's nothing here that could have come from coreutils, because POSIX dictates the entire design, not just the behaviour.

    I used the 2024 spec (Issue 8):

    And the parts:

    • sys/stat.h (the 2-tuple that identifies a file) - "A file identity is uniquely determined by the combination of st_dev and st_ino."
    • du, DESCRIPTION - (the count-once behaviour) "A file that occurs multiple times shall be counted and written for only one entry, even if the occurrences are under different file operands."
    • du, RATIONALE (why every file is tracked, not just multi-link ones) - "The use of the -L option, or of multiple file operands, requires that du track all file entries encountered, even with a link count of one."
    • du, RATIONALE (it even calls out the single-operand optimisation) - "However, when -L is not used and only a single file operand is given, an implementation can optimize by only tracking files with a link count greater than one, since in that scenario, those are the only files that could be encountered more than once."

    Give somebody only those pages, and they write a HashSet<(u64, u64)> with an insert. What is coreutils' own - an fts walk in C and gnulib's di-set hash table - isn't in this diff, which hangs ~50 lines off nushell's existing DirInfo recursion. I'd genuinely welcome anyone putting du.c side by side with the diff. That's also the answer on the AI side: if anything had been reproduced, by me or by the tool, it would show.

    What I've already done. Three doc comments in dir_info.rs quoted a sentence each from du.c / di-set.c, as citations. That was the only coreutils text in the diff, and it cost nothing to remove, so I've pushed d23013f to the PR branch: it paraphrases those three and cites POSIX instead. No code changed. Every word in the diff is now mine, POSIX's, or std's.

    If you'd still rather not carry the call yourself, two options:

    1. A second opinion from Terts Diepraam (tertsdiepraam). He maintains uutils, so he lives under the strictest version of this rule, and he wrote nushell's uutils integration. I'd trust his read, and I haven't pinged him or @-ed him (is that a verb? Lol) - I wanted to check with you first.
    2. A proper clean-room. My description plus the three tests are a behavioural spec written from POSIX; anyone who hasn't opened du.c can implement from those and the POSIX page in well under an hour (I'm going to assume they're a better rust coder than me), and I'd stay out of the implementation. It's a lot of work for 50 lines, but if it's what gets this fixed after six years, I'll take it - and beers are on me if you're ever down under.

    Which of those would settle it for you? Happy to go whichever way you feel is best - I'd just rather do the thing that gets to yes and a bugfix than keep trying to push.

  8. fdncred commented on Sep 14, 2026

    @fdncred
    Contributor

    I'm sorry but we don't really want to take the chance. What I'd prefer to happen is to use the uutils/coreutils uu_du crate. What would have to happen, as with all the uutils crates, is a PR would have to be submitted to their repo and tag me. I'm friendly with the maintainers and they're always very open to support nushell by making options/settings public and changing how output is emitted.

  9. added this to the v0.117.0 milestone on Oct 18, 2026
  10. added a commit that references this issue on Oct 18, 2026
    199cf98
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status:needs-triage

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions