You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cargo run --example completions break with nested example directories #8429
$ 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
ifnottest-d ./examples
returnendfind ./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 --example2>&1|tail-n+3 | string trim
Alternately, more of a heuristic would be to modify the find command to be more robust, something like:
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.
Edit: actually, I think there are three possibilities that are allowed, re-reading the docs: explicit [[example]] in Cargo.toml, examples/single_file.rs, and examples/multi_file/main.rs.
I'm a little bit confused by the reason given in that commit,
cargo run --example no longer lists examples and never listed examples that were not built.
It appears that cargo actually gained that behavior as of this PR in 2019, rust-lang/cargo#6505 so I'm wondering if there was a regression at some point?
On my macOS machine, this is what I see:
$ cargo --version
cargo 1.56.0 (4ed5d137b 2021-10-04)
$ cargo run --example
error: "--example" takes one argument.
Available examples:
3d_scene
alien_cake_addict
android
array_texture
asset_loading
#...
The file-based heuristic is fine for many cases, I think, but it seems like the only truly correct way to enumerate examples would be to trust Cargo, or basically duplicate its features (parse Cargo.toml, look for files, etc.).
It appears that cargo actually gained that behavior as of this PR in 2019, rust-lang/cargo#6505 so I'm wondering if there was a regression at some point?
Right, looks like cargo run --example now does list all examples!
Ah I forgot about the [[examples]] from Cargo.toml (and was on rust 1.54 so bevy didn't work when I tried, fixed now with 1.56).
We could write a small Python (?) snippet that pulls the [[examples]] out of Cargo.toml.
This might prove tricky though, because we don't want to list library examples.
Maybe that's easy to fix, but it feels more stable to parse the error output of cargo run --example.
sure, PR is welcome, since it seems to be working for modern versions of cargo. I think using tail is fine but of course fish-fooding is always a good idea.
Activity
krobelus commentedon Nov 11, 2021
Commit 0d3f4db ([cargo] Use filesystem for --example completions) gives the reason why we don't parse
cargo run --exampleReading https://doc.rust-lang.org/cargo/reference/cargo-targets.html#examples
I found https://doc.rust-lang.org/cargo/guide/project-layout.html
which suggests that only top-level directories are valid as examples. It's not explicit though.
checking the cargo sources indicates that they only read
examples/*, not any deeperare you sure that
cargo run --example 3d_sceneworks?ian-h-chamberlain commentedon Nov 11, 2021
Ok, I looked a bit more, and it seems like the answer is that Cargo finds both top-level
*.rsexamples in the directory, and[[example]]s specified inCargo.toml, as of the 2018 edition: https://doc.rust-lang.org/cargo/reference/cargo-targets.html#target-auto-discoveryEdit: actually, I think there are three possibilities that are allowed, re-reading the docs: explicit
[[example]]inCargo.toml,examples/single_file.rs, andexamples/multi_file/main.rs.I'm a little bit confused by the reason given in that commit,
It appears that cargo actually gained that behavior as of this PR in 2019, rust-lang/cargo#6505 so I'm wondering if there was a regression at some point?
On my macOS machine, this is what I see:
I verified as well that actually running the examples works, since they are listed as
[[example]]in the manifest (e.g. https://github.com/bevyengine/bevy/blob/main/Cargo.toml#L133).The file-based heuristic is fine for many cases, I think, but it seems like the only truly correct way to enumerate examples would be to trust Cargo, or basically duplicate its features (parse
Cargo.toml, look for files, etc.).krobelus commentedon Nov 11, 2021
Right, looks like
cargo run --examplenow does list all examples!Ah I forgot about the
[[examples]]from Cargo.toml (and was on rust 1.54 sobevydidn't work when I tried, fixed now with 1.56).We could write a small Python (?) snippet that pulls the
[[examples]]out of Cargo.toml.This might prove tricky though, because we don't want to list library examples.
Maybe that's easy to fix, but it feels more stable to parse the error output of
cargo run --example.ian-h-chamberlain commentedon Nov 12, 2021
Sounds good to me – I have a local version that seems be working for me, would you like me to open a PR to switch to using
cargo run --example?Right now it's using
tailbut looks like it would be preferable not to for #6520 so I will see if I can fishify it before submitting a PR.krobelus commentedon Nov 12, 2021
sure, PR is welcome, since it seems to be working for modern versions of cargo. I think using
tailis fine but of course fish-fooding is always a good idea.CHANGELOG updates for 3.4.0