Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [`getrandom`](cli/web/getrandom.md)
- [Default `index.html`](cli/web/default-index-html.md)
- [Linter](cli/linter.md)
- [Running Examples](cli/examples.md)
- [Configuration](cli/configuration.md)
- [Configuration Reference](cli/configuration/reference.md)
- [Troubleshooting](cli/troubleshooting.md)
Expand Down
24 changes: 24 additions & 0 deletions docs/src/cli/examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Examples

The CLI makes it easy to build, run and lint examples by automatically enabling required features of the example.

## Build and run a specific example

```sh
# Run the `web_asset` example from https://github.com/bevyengine/bevy that requires the feature `https`.
bevy run --example web_asset
info: enabling required_features: ["https"], for example: web_asset

# Build the `web_asset` example in the web.
bevy build --example web_asset web
info: enabling required_features: ["https"], for example: web_asset
```
Comment on lines +7 to +15
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
```sh
# Run the `web_asset` example from https://github.com/bevyengine/bevy that requires the feature `https`.
bevy run --example web_asset
info: enabling required_features: ["https"], for example: web_asset
# Build the `web_asset` example in the web.
bevy build --example web_asset web
info: enabling required_features: ["https"], for example: web_asset
```
Take Bevy's `web_asset` example, for instance. It requires the `https` feature in `Cargo.toml`:
```toml
[[example]]
name = "web_asset"
path = "examples/asset/web_asset.rs"
required-features = ["https"]
```
Running `cargo build --example web_asset` will fail with Cargo complaining that the `https` feature was not enabled. The Bevy CLI differs by automatically enabling the feature for you:
```sh
# The CLI will automatically add `--feature https`, as that feature is required to run the example.
bevy run --example web_asset
# It also works when building for the web.
bevy build --example web_asset web
```

I think it would be good to show how required features are specified in Cargo.toml :)


## Build all examples

The CLI supports building all examples only for the native target, for the web this is not supported yet.
When building all examples, the CLI will build with `--all-features`.

```sh
bevy build --examples
```
1 change: 1 addition & 0 deletions docs/src/cli/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The CLI is a prototype tool intended to streamline common tasks when working on
- [**Project Scaffolding**](scaffolding.md)
- [**Build and running Bevy Web Apps**](web.md)
- [**Linter Integration**](linter.md)
- [**Enabling required features for examples**](examples.md)
Comment on lines 6 to +8
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
- [**Build and running Bevy Web Apps**](web.md)
- [**Linter Integration**](linter.md)
- [**Enabling required features for examples**](examples.md)
- [**Build and Running Bevy Web Apps**](web.md)
- [**Linter Integration**](linter.md)
- [**Enabling Required Features for Examples**](examples.md)

(Nit) Let's make all of these title case


[initial scope document]: https://hackmd.io/cCHAfbtaSviU_MDnbNHKxg
[original issue]: https://github.com/bevyengine/bevy/issues/436
Expand Down
35 changes: 35 additions & 0 deletions src/commands/build/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Provides functionalities to build a Bevy app targeting either native or web platforms.

pub use args::*;
use cargo_metadata::TargetKind;
use tracing::info;

#[cfg(feature = "web")]
use crate::web::build::build_web;
Expand Down Expand Up @@ -39,6 +41,39 @@ pub fn build(args: &mut BuildArgs) -> anyhow::Result<()> {

args.apply_config(&config);

// If a specific example was passed, extend the already present features with the
// required_features from this example.
if let Some(example) = &args.cargo_args.target_args.example
// Search in the current workspace packages for an `example` target that matches the given
// example name.
&& let Some(example_target) = metadata
.workspace_packages()
.iter()
.flat_map(|p| p.targets.clone())
.find(|t| t.name.as_str() == example && t.kind.contains(&TargetKind::Example))
{
let required_features = example_target.required_features;

info!(
"enabling required_features: {:?}, for example: {example}",
required_features
);

args.cargo_args
.feature_args
.features
.extend(required_features);
}
// build `--examples` with all features enabled.
else if args.cargo_args.target_args.is_examples {
args.cargo_args
.feature_args
.features
.push("--all-features".to_owned());

info!("automatically added `--all-features` to build examples with all features enabled");
Comment on lines +69 to +74
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure we should always enable --all-features when --examples is passed. When I run cargo build --examples --all-features in Bevy's repo, it fails with a compile error. That's not a great user experience, especially when the primary use of this feature will be by Bevy devs.

In @alice-i-cecile's original comment when I asked how to deal with --examples and --all-targets, she said:

Based on the design proposed in the linked issue, I would build these with --all-features (uh, except where mutually incompatible features exist) and diverge from cargo. Or at least warn users that they probably want to enable all the features and try again.

I think we either need to be more intelligent with how we handle mutually incompatible features, or just cut this for now. The alternative is Bevy devs being unable to compile the Bevy workspace with bevy build --examples, having to switch to cargo build --examples, which I don't think is acceptable.

Copy link
Member

Choose a reason for hiding this comment

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

If we update this, make sure to also update the section in examples.md and lint/mod.rs.

}

#[cfg(feature = "web")]
if args.is_web() {
build_web(args, &metadata)?;
Expand Down
35 changes: 34 additions & 1 deletion src/commands/lint/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub use args::*;
use tracing::error;
use tracing::{error, info};

#[cfg(feature = "rustup")]
use crate::commands::lint::install::install_linter;
Expand Down Expand Up @@ -145,6 +145,39 @@ fn build_lint_cmd(args: &mut LintArgs) -> anyhow::Result<CommandExt> {
}
}

// If a specific example was passed, extend the already present features with the
// required_features from this example.
if let Some(example) = &args.cargo_args.target_args.example
// Search in the current workspace packages for an `example` target that matches the given
// example name.
&& let Some(example_target) = metadata
.workspace_packages()
.iter()
.flat_map(|p| p.targets.clone())
.find(|t| t.name.as_str() == example && t.kind.contains(&cargo_metadata::TargetKind::Example))
{
let required_features = example_target.required_features;

info!(
"enabling required_features: {:?}, for example: {example}",
required_features
);

args.cargo_args
.feature_args
.features
.extend(required_features);
}
// build `--examples` with all features enabled.
else if args.cargo_args.target_args.is_examples {
args.cargo_args
.feature_args
.features
.push("--all-features".to_owned());

info!("automatically added `--all-features` to check examples with all features enabled");
}

let cargo_args = args.cargo_args_builder();

cmd.args(cargo_args)
Expand Down
25 changes: 25 additions & 0 deletions src/commands/run/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Provides functionalities to run a Bevy app targeting either native or web platforms.

use tracing::info;

pub use self::args::*;
#[cfg(feature = "web")]
use crate::web::run::run_web;
Expand Down Expand Up @@ -37,6 +39,29 @@ pub fn run(args: &mut RunArgs) -> anyhow::Result<()> {

args.apply_config(&config);

// Extend the already present features with the required_features from this example.
if let Some(example) = &args.cargo_args.target_args.example
// Search in the current workspace packages for an `example` target that matches the given
// example name.
&& let Some(example_target) = metadata
.workspace_packages()
.iter()
.flat_map(|p| p.targets.clone())
.find(|t| t.name.as_str() == example && t.kind.contains(&cargo_metadata::TargetKind::Example))
{
let required_features = example_target.required_features;

info!(
"enabling required_features: {:?}, for example: {example}",
required_features
);

args.cargo_args
.feature_args
.features
.extend(required_features);
}

#[cfg(feature = "web")]
if args.is_web() {
return run_web(args, &metadata);
Expand Down