Skip to content

cargo run --example completions break with nested example directories #8429

@ian-h-chamberlain

Description

@ian-h-chamberlain
$ fish --version 
fish, version 3.3.1

$ echo $version 
3.3.1

Cargo completions for --example seem to be a little off when there are subdirectories in the examples directory of a cargo project.

Example, running in a local checkout of this repository: https://github.com/bevyengine/bevy

$ cargo run --example # <tab>
2d       asset        game         reflection  ui    
3d       audio        hello_world  scene       wasm  
android  diagnostics  input        shader      window
app      ecs          ios          tools      

Expected result would include z_sort_debug, 3d_scene, and many more.
Directory names do not necessarily correlate to actual runnable example names, and file names do not necessarily correspond to example names, although they usually do by convention.

The problem seems to be in this function in cargo.fish:

function __list_cargo_examples
    if not test -d ./examples
        return
    end

    find ./examples -mindepth 1 -maxdepth 1 -type f -name "*.rs" -or -type d \
        | string replace -r './examples/(.*?)(?:.rs)?$' '$1'
end

I'm not sure exactly why the original implementation had -maxdepth 1 or -type d.

There are two options as far as I can tell, one would be to run cargo itself to list the examples, as a quick hack something like this seems to work pretty well:

cargo run --example 2>&1 | tail -n+3 | string trim

Alternately, more of a heuristic would be to modify the find command to be more robust, something like:

find ./examples -mindepth 1 -type f -name "*.rs" -exec basename '{}' \; | string replace '.rs' ''

I can open a PR for one or the other approach, depending on what seems preferable. The first approach is probably more likely to be correct, but could break depending on implementation changes in cargo, the second may be incorrect depending on a given project's structure, but probably matches convention fairly well.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions