Skip to content

cargo clean --target-dir fails when target dir doesn't exist #16925

@bjorn3

Description

@bjorn3

Problem

Since #16712 trying to run cargo clean with a custom target dir fails if the target dir is non-existent rather than behaving like a nop.

Steps

  1. rm foo || true
  2. cargo clean --target-dir foo
  3. The following error is produced
error: cannot clean `/home/bjorn/cg_clif/foo`: missing or invalid `CACHEDIR.TAG` file
  |
  = note: cleaning has been aborted to prevent accidental deletion of unrelated files

or alternatively

  1. cargo check --target-dir foo
  2. cargo clean --target-dir foo passes
  3. cargo clean --target-dir foo fails

Possible Solution(s)

Do nothing when the target dir already doesn't exist.

Notes

No response

Version

cargo 1.97.0-nightly (7ecf0285e 2026-04-18)
release: 1.97.0-nightly
commit-hash: 7ecf0285ebb408d596e4a8ac76a0980d8edb7005
commit-date: 2026-04-18
host: x86_64-unknown-linux-gnu
libgit2: 1.9.2 (sys:0.20.4 vendored)
libcurl: 8.19.0-DEV (sys:0.4.87+curl-8.19.0 vendored ssl:OpenSSL/3.5.4)
ssl: OpenSSL 3.5.4 30 Sep 2025
os: Arch Linux [64-bit]

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bugCommand-cleanS-acceptedStatus: Issue or feature is accepted, and has a team member available to help mentor or review

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions