Repository navigation
Description
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
-lflag 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
fdncred commentedon Sep 13, 2026
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.
victorhooi commentedon Sep 13, 2026
I filed a PR
#19020
Disclaimer: Stole/borrowed the logic from GNU coreutils, and used AI to help with the rust.
fdncred commentedon Sep 13, 2026
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.
victorhooi commentedon Sep 13, 2026
@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.
fdncred commentedon Sep 13, 2026
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.
victorhooi commentedon Sep 14, 2026
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:
Give somebody only those pages, and they write a
HashSet<(u64, u64)>with aninsert. What is coreutils' own - an fts walk in C and gnulib'sdi-sethash table - isn't in this diff, which hangs ~50 lines off nushell's existingDirInforecursion. I'd genuinely welcome anyone puttingdu.cside 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.rsquoted a sentence each fromdu.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:
du.ccan 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.
fdncred commentedon Sep 14, 2026
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.
fix(du): count a hard-linked file once, not once per link (nushell#19283