Skip to content

feat(cp): add -l/--link flag to create hard links - #19583

Merged
fdncred merged 5 commits into
nushell:mainfrom
Ariakage:cp-link-flag
Nov 20, 2026
Merged

fdncred merged 5 commits into
nushell:mainfrom
Ariakage:cp-link-flag

Conversation

@Ariakage

@Ariakage Ariakage commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Description

Implements #14306: cp gains a -l/--link flag matching GNU cp -l,
which creates hard links to regular files instead of copying them.
Combined with -r it can hard-link a whole directory tree
(cp -lr source dest), the use case in the issue.

The flag just exposes uu_cp's existing CopyMode::Link.

  • -l cannot be combined with -u: uu_cp ignores UpdateMode in
    CopyMode::Link, so the combination is rejected with
    IncompatibleParameters rather than silently misbehaving.
  • When linking, symlinks are followed and their targets are linked,
    matching uutils' resolve_dereference (!recursive || is_link).
    -P/--no-dereference links the links themselves, as noted in the
    flag's help text.

Fresh take on #14359, which went stale.

User-facing changes (Release notes)

cp now supports -l/--link to create hard links to regular files instead of copying, e.g. cp -l a.txt b.txt, or cp -lr dir_a dir_b to recursively link a directory tree (symlinks inside the tree are followed unless -P is given). -l cannot be combined with -u.

Additional notes

Match GNU `cp -l`: regular files are created as hard links to the
source instead of copied. Combined with `-r` this recursively links
a directory hierarchy, e.g. `cp -lr source dest`.

Implemented by exposing `uu_cp`'s existing `CopyMode::Link` through
a new switch; no dependency changes needed. `UpdateMode` and
`CopyMode` are now resolved independently so `cp -lu` behaves like
upstream `cp`.

Closes nushell#14306.

@fdncred fdncred left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review overview

🟡 Changes recommended

cp -l and cp -lr make hard links on macOS, and clippy, fmt and the ucp tests pass. The blocker is upstream: uu_cp 0.12.0 chmods the destination after linking, which changes the source file's mode, and that fix isn't released yet. The new test helper also won't compile on Windows.

Findings: 1 High severity · 3 Medium severity · 1 Low severity

Open (5)

Notes from my clanker, checked on a05ab4a. I built the branch and ran each case on it, on 0.117.1 (which has no -l), and on uutils cp 0.12.0, the version nushell links. cargo test -p nu-command --test tests -- commands::ucp gave 79 passed and 6 ignored, and clippy with CI's flags, fmt and typos were clean, all on macOS. The Windows finding comes from compile-checking the test helper in a separate crate. Nothing ran on Windows or Linux, and I had no GNU cp here to compare against.

Comment thread crates/nu-command/src/filesystem/ucp.rs Resolved
Comment thread crates/nu-command/tests/commands/ucp.rs Resolved
Comment thread crates/nu-command/src/filesystem/ucp.rs Resolved
Comment thread crates/nu-command/tests/commands/ucp.rs Resolved
Comment thread crates/nu-command/src/filesystem/ucp.rs Resolved
- Reject `-l` together with `-u`: uu_cp 0.12.0 ignores UpdateMode in
  Link copy mode and IfOlder disables overwrite verification, so the
  combination silently did the wrong thing. Return
  IncompatibleParameters instead.
- Follow symlinks when linking recursively, matching uutils'
  resolve_dereference (`!recursive || is_link`); `-P` keeps linking
  the links themselves. Documented in the flag's help text.
- Replace inode/file-index assertions with a portable write-through
  hard-link check: std's Windows MetadataExt::file_index is unstable
  and would break the tests target on Windows.
- Replace the directory-error test, which never exercised `-l`, with
  an existing-destination case, a `-lu` rejection test, and a
  recursive test covering a symlink in the tree. Add an ignored
  unix-only regression test for the source-mode chmod bug
  (uutils/coreutils#15087); unignore once a uu_cp release containing
  uutils/coreutils#15088 is available.
@Ariakage

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review — all five findings addressed in 9f32029:

  1. Source-mode chmod: confirmed — cp -lf on a 755 source with a 600 destination leaves the source at 600. Since the fix (cp: don't change the source's mode with --link uutils/coreutils#15088) isn't in a uu_cp release yet, I added a unix-only regression test (linking_does_not_change_source_mode, exactly the scenario you suggested: 755 source, 600 dest, cp -lf) but marked it #[ignore]d so it goes green once the uutils bump lands. Happy to also gate the flag itself if you'd rather hold the whole PR for that release — let me know which you prefer.
  2. Windows file_index: replaced file_id with your write-through assert_hard_linked — no cfg, no unstable APIs, and it also catches clonefile-style copies.
  3. -l + -u: now rejected with IncompatibleParameters using get_flag_span for both flags, same pattern as find. Covered by errors_on_link_and_update_combination. PR description updated (dropped the cp -lu claim).
  4. -lr symlinks: went with the uutils behavior — dereference is now (!recursive || link) && !no_dereference, so cp -lr follows symlinks and links their targets while -P links the links themselves. Documented in the -l help text and exercised by a symlink inside the recursive test's tree.
  5. Weak directory test: replaced with errors_linking_over_existing_file — cp -l over an existing destination reports "File exists" (and leaves the destination untouched), and cp -lf replaces it with a link verified via assert_hard_linked.

cargo fmt, nuclippy-flagged clippy on nu-command, typos, and cargo test -p nu-command --test tests -- commands::ucp all pass (80 tests; the only failure is the pre-existing macOS test_cp_debug_default reflink failure, which also fails on main).

@fdncred

fdncred commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

We may need to wait for the next uutils release

@Ariakage

Copy link
Copy Markdown
Contributor Author

Sounds good — I'll keep an eye on the next uutils release. Once uu_cp with uutils/coreutils#15088 is out, I'll bump the uutils crates in this PR, un-ignore linking_does_not_change_source_mode, and re-verify.

@fdncred

fdncred commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

i'll ping one of the maintainers to see if they have an outlook for the next release.

@fdncred

fdncred commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

that maintainer said they'll have a release in the next few days.

@Ariakage

Copy link
Copy Markdown
Contributor Author

Thanks for checking! I'll watch for the release.

'File exists' is the POSIX errno suffix; on Windows the same failure
reads differently. The 'cannot create hard link' prefix comes from
uutils' fluent strings and renders the same on every platform.
@fdncred

fdncred commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

i created #19620 to bump uutils to 0.13.0

@Ariakage

Copy link
Copy Markdown
Contributor Author

linking_does_not_change_source_mode passes locally with uu_cp 0.13.0, so the upstream fix works (local bump reverted). Once #19620 merges I'll rebase, drop the #[ignore], and ping you.

@fdncred

fdncred commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

it's merged now

uu_cp 0.13.0 is on main now and contains the fix for the chmod after
hard link, so linking_does_not_change_source_mode runs for real.
The link tests also move to the injected-Playground API that main
switched to.
@Ariakage

Copy link
Copy Markdown
Contributor Author

Merged main (uu_cp 0.13.0). linking_does_not_change_source_mode is un-ignored and passes in both cases; the link tests were also migrated to the new injected-Playground API. Ready for another look.

@fdncred

fdncred commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Just need to update the description of this PR and i think we're good to go.

@Ariakage

Copy link
Copy Markdown
Contributor Author

done

@fdncred fdncred added notes:ready Indicates Ready for Release notes notes:additions Noted in "Additions" section labels Nov 20, 2026
@fdncred
fdncred merged commit 0b6b84f into nushell:main Nov 20, 2026
14 checks passed
@fdncred

fdncred commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Thanks

@github-actions github-actions Bot added this to the v0.118.0 milestone Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:additions Noted in "Additions" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement cp -l flag to create hardlinks in a file heirarchy

2 participants