Skip to main content

Crate echoes

Crate echoes 

Source
Expand description

§cargo-echoes

CI codecov crates.io docs.rs

By chance two separate functions meet, and I am you and what I see is me.

cargo echoes finds duplicated Rust doctests, and optionally duplicated functions and code blocks, across a workspace or across every crate of a GitHub organization. The wiki explains each check, the options and the Action in depth.

A pull request review by echoes, with an inline comment on a rewritten copy of a function and a suggestion that deletes it

§Usage

cargo install echoes
cargo echoes --functions --context-blocks
cargo echoes --organization --functions
CheckEnableDefault minimum sizeSet the size
DoctestsOn by default0 tokens--min-tokens
Functions--functions30 weighted body tokens--fn-min-tokens
Contexts--context-blocks30 canonical weighted tokens--context-min-tokens

Options can also live in .echoes.toml at the workspace root, and cargo echoes --help lists them all. Leave out whole paths, such as benchmarks, with gitignore-style exclude globs or --exclude, and keep an intentional copy with a rust,echoes fence or a // echoes: allow line above the function. The wiki lists what counts as the same code.

--organization scans the default branch of every repository of a GitHub organization or user that the GH_TOKEN or GITHUB_TOKEN credential can read, private ones included, and compares functions across crates and modules. Without a name it scans the owner of the current repository, from GITHUB_REPOSITORY_OWNER or the origin remote. Repeat it to join several owners, and pass --repository OWNER/REPO once per repository to add repositories of any owner, alone or beside the organizations. Downloaded repositories never configure the scan or run code, so pass --config for options, and the exit status is 2 whenever a repository could not be analyzed, even with --no-fail.

§GitHub Action

on: pull_request
jobs:
  echoes:
    runs-on: ubuntu-26.04
    permissions:
      contents: write
      pull-requests: write
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: LucaCappelletti94/cargo-echoes@v1
        with:
          pr-number: ${{ github.event.pull_request.number }}
          functions: true

The action reviews the pull request and suggests removing each copy it introduces, with links to report a wrong review and support echoes. On every new commit it dismisses its earlier requests for changes, updates its earlier comment-only summaries and resolves the threads of copies that are gone, which needs contents: write. action.yml lists every input, and these two workflows review pull requests from forks.

§Library

let report = echoes::Echoes::default()
    .functions()
    .context_blocks()
    .run(".")
    .unwrap();
println!("{}", echoes::human(&report, false));

Use default-features = false, features = ["std"] to drop the CLI. Coding agents get the same guidance from the echoes skill.

npx skills add LucaCappelletti94/cargo-echoes

Modules§

extract
Doc comment walking and doctest block extraction.

Structs§

ContextGroup
Approximate lexical contexts sharing an exact canonical form.
ContextSite
One approximate lexical context match at its original source range.
DependencyCandidate
An upstream lexical context matching selected local code.
DependencyCoverage
Coverage and failures of a requested dependency search.
DependencySearch
Parameters for dependency context discovery.
DocTest
A duplicated site, a doctest block or a function.
Echoes
Scan parameters. In run, unset values fall back to .echoes.toml, then to defaults.
ExistingIssue
An existing tagged upstream extraction request.
Group
Duplicated sites sharing one canonical form.
OrganizationCoverage
Completeness of the accessible organization corpus.
OrganizationPackage
A Cargo package in an organization snapshot.
OrganizationReport
Organization source provenance and analysis coverage.
OrganizationRepository
A repository and its pinned default-branch revision.
Report
Outcome of a scan.
ReuseGroup
Dependency reuse alternatives for one selected local context.
ReuseReport
Grouped dependency findings and requested-analysis coverage.
SourceFile
One file of a target.
TargetScan
One resolved target.

Enums§

ContextAccess
Source-established accessibility of a lexical context.
ContextKind
The kind of lexical scope a context candidate covers.
ContextScope
A selection over the workspace context corpus.
Error
Failure of a scan.
Kind
What a group’s sites are.
OwnerError
Why the current repository’s GitHub owner could not be determined.
Remedy
What to do with the copies of a function group, the first matching row of the rule table winning.
ReuseAction
The reuse path suggested by source evidence.

Functions§

annotations
Render the report as GitHub workflow annotations, one per copy to remove plus one per approximate context site. They need no token, so a fork pull request still shows them anchored in the diff.
exit_code
Exit status for a finished scan.
group
Group doctests by canonical form, dropping allowed sites, blocks under min_tokens, and groups under threshold.
human
Render the report for humans.
json
Render the report as JSON.
repository_owner
The GitHub owner of the repository at dir, from GITHUB_REPOSITORY_OWNER when set and non-empty, else from its origin remote.

Type Aliases§

Result
A Result with Error as its default error type.