Skip to content

Commit

Permalink
Harden proc macro path resolution and add integration tests. (bevyeng…
Browse files Browse the repository at this point in the history
…ine#17330)

This pr uses the `extern crate self as` trick to make proc macros behave
the same way inside and outside bevy.

# Objective

- Removes noise introduced by `crate as` in the whole bevy repo.
- Fixes bevyengine#17004.
- Hardens proc macro path resolution.

## TODO

- [x] `BevyManifest` needs cleanup.
- [x] Cleanup remaining `crate as`.
- [x] Add proper integration tests to the ci.

## Notes

- `cargo-manifest-proc-macros` is written by me and based/inspired by
the old `BevyManifest` implementation and
[`bkchr/proc-macro-crate`](https://github.com/bkchr/proc-macro-crate).
- What do you think about the new integration test machinery I added to
the `ci`?
  More and better integration tests can be added at a later stage.
The goal of these integration tests is to simulate an actual separate
crate that uses bevy. Ideally they would lightly touch all bevy crates.

## Testing

- Needs RA test
- Needs testing from other users
- Others need to run at least `cargo run -p ci integration-test` and
verify that they work.

---------

Co-authored-by: Alice Cecile <[email protected]>
  • Loading branch information
raldone01 and alice-i-cecile authored Feb 9, 2025
1 parent 669d139 commit 1b7db89
Show file tree
Hide file tree
Showing 132 changed files with 325 additions and 269 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ crates/**/target
benches/**/target
tools/**/target
**/*.rs.bk
rustc-ice-*.txt

# DX12 wgpu backend
dxcompiler.dll
Expand Down
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ members = [
# Bevy's error codes. This is a crate so we can automatically check all of the code blocks.
"errors",
]
exclude = [
# Integration tests are not part of the workspace
"tests-integration",
]

[workspace.lints.clippy]
doc_markdown = "warn"
Expand Down Expand Up @@ -494,6 +498,13 @@ serde = { version = "1", features = ["derive"] }
serde_json = "1"
bytemuck = "1.7"
bevy_render = { path = "crates/bevy_render", version = "0.16.0-dev", default-features = false }
# The following explicit dependencies are needed for proc macros to work inside of examples as they are part of the bevy crate itself.
bevy_ecs = { path = "crates/bevy_ecs", version = "0.16.0-dev", default-features = false }
bevy_state = { path = "crates/bevy_state", version = "0.16.0-dev", default-features = false }
bevy_asset = { path = "crates/bevy_asset", version = "0.16.0-dev", default-features = false }
bevy_reflect = { path = "crates/bevy_reflect", version = "0.16.0-dev", default-features = false }
bevy_image = { path = "crates/bevy_image", version = "0.16.0-dev", default-features = false }
bevy_gizmos = { path = "crates/bevy_gizmos", version = "0.16.0-dev", default-features = false }
# Needed to poll Task examples
futures-lite = "2.0.1"
async-std = "1.13"
Expand Down
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ fn none_changed_detection(criterion: &mut Criterion) {
}
}
fn insert_if_bit_enabled<const B: u16>(entity: &mut EntityWorldMut, i: u16) {
if i & 1 << B != 0 {
if i & (1 << B) != 0 {
entity.insert(Data::<B>(1.0));
}
}
Expand Down
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/components/archetype_updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn setup(system_count: usize) -> (World, Schedule) {
}

fn insert_if_bit_enabled<const B: u16>(entity: &mut EntityWorldMut, i: u16) {
if i & 1 << B != 0 {
if i & (1 << B) != 0 {
entity.insert(A::<B>(1.0));
}
}
Expand Down
30 changes: 15 additions & 15 deletions benches/benches/bevy_ecs/empty_archetypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,49 +105,49 @@ fn add_archetypes(world: &mut World, count: u16) {
e.insert(A::<10>(1.0));
e.insert(A::<11>(1.0));
e.insert(A::<12>(1.0));
if i & 1 << 1 != 0 {
if i & (1 << 1) != 0 {
e.insert(A::<13>(1.0));
}
if i & 1 << 2 != 0 {
if i & (1 << 2) != 0 {
e.insert(A::<14>(1.0));
}
if i & 1 << 3 != 0 {
if i & (1 << 3) != 0 {
e.insert(A::<15>(1.0));
}
if i & 1 << 4 != 0 {
if i & (1 << 4) != 0 {
e.insert(A::<16>(1.0));
}
if i & 1 << 5 != 0 {
if i & (1 << 5) != 0 {
e.insert(A::<18>(1.0));
}
if i & 1 << 6 != 0 {
if i & (1 << 6) != 0 {
e.insert(A::<19>(1.0));
}
if i & 1 << 7 != 0 {
if i & (1 << 7) != 0 {
e.insert(A::<20>(1.0));
}
if i & 1 << 8 != 0 {
if i & (1 << 8) != 0 {
e.insert(A::<21>(1.0));
}
if i & 1 << 9 != 0 {
if i & (1 << 9) != 0 {
e.insert(A::<22>(1.0));
}
if i & 1 << 10 != 0 {
if i & (1 << 10) != 0 {
e.insert(A::<23>(1.0));
}
if i & 1 << 11 != 0 {
if i & (1 << 11) != 0 {
e.insert(A::<24>(1.0));
}
if i & 1 << 12 != 0 {
if i & (1 << 12) != 0 {
e.insert(A::<25>(1.0));
}
if i & 1 << 13 != 0 {
if i & (1 << 13) != 0 {
e.insert(A::<26>(1.0));
}
if i & 1 << 14 != 0 {
if i & (1 << 14) != 0 {
e.insert(A::<27>(1.0));
}
if i & 1 << 15 != 0 {
if i & (1 << 15) != 0 {
e.insert(A::<28>(1.0));
}
}
Expand Down
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/iteration/par_iter_simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct Data<const X: u16>(f32);
pub struct Benchmark<'w>(World, QueryState<(&'w Velocity, &'w mut Position)>);

fn insert_if_bit_enabled<const B: u16>(entity: &mut EntityWorldMut, i: u16) {
if i & 1 << B != 0 {
if i & (1 << B) != 0 {
entity.insert(Data::<B>(1.0));
}
}
Expand Down
2 changes: 0 additions & 2 deletions crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1532,7 +1532,6 @@ mod tests {
#[test]
fn test_derive_app_label() {
use super::AppLabel;
use crate::{self as bevy_app};

#[derive(AppLabel, Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
struct UnitLabel;
Expand Down Expand Up @@ -1664,7 +1663,6 @@ mod tests {
#[test]
fn test_extract_sees_changes() {
use super::AppLabel;
use crate::{self as bevy_app};

#[derive(AppLabel, Clone, Copy, Hash, PartialEq, Eq, Debug)]
struct MySubApp;
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ extern crate std;

extern crate alloc;

// Required to make proc macros work in bevy itself.
extern crate self as bevy_app;

mod app;
mod main_schedule;
mod panic_handler;
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/asset_changed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ unsafe impl<A: AsAssetId> QueryFilter for AssetChanged<A> {

#[cfg(test)]
mod tests {
use crate::{self as bevy_asset, AssetEvents, AssetPlugin, Handle};
use crate::{AssetEvents, AssetPlugin, Handle};
use alloc::{vec, vec::Vec};
use core::num::NonZero;
use std::println;
Expand Down
5 changes: 1 addition & 4 deletions crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use crate::asset_changed::AssetChanges;
use crate::{
self as bevy_asset, Asset, AssetEvent, AssetHandleProvider, AssetId, AssetServer, Handle,
UntypedHandle,
};
use crate::{Asset, AssetEvent, AssetHandleProvider, AssetId, AssetServer, Handle, UntypedHandle};
use alloc::{sync::Arc, vec::Vec};
use bevy_ecs::{
prelude::EventWriter,
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_asset/src/folder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use alloc::vec::Vec;

use crate as bevy_asset;
use crate::{Asset, UntypedHandle};
use bevy_reflect::TypePath;

Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@
extern crate alloc;
extern crate std;

// Required to make proc macros work in bevy itself.
extern crate self as bevy_asset;

pub mod io;
pub mod meta;
pub mod processor;
Expand Down Expand Up @@ -627,7 +630,6 @@ pub struct AssetEvents;
#[cfg(test)]
mod tests {
use crate::{
self as bevy_asset,
folder::LoadedFolder,
handle::Handle,
io::{
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_asset/src/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use alloc::{
};

use crate::{
self as bevy_asset, loader::AssetLoader, processor::Process, Asset, AssetPath,
DeserializeMetaError, VisitAssetDependencies,
loader::AssetLoader, processor::Process, Asset, AssetPath, DeserializeMetaError,
VisitAssetDependencies,
};
use downcast_rs::{impl_downcast, Downcast};
use ron::ser::PrettyConfig;
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_asset/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,6 @@ mod tests {
use alloc::{string::String, vec::Vec};
use core::any::TypeId;

use crate as bevy_asset;
use crate::{Asset, AssetApp, AssetPlugin, ReflectAsset, UntypedHandle};
use bevy_app::App;
use bevy_ecs::reflect::AppTypeRegistry;
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/server/loaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ mod tests {
use bevy_reflect::TypePath;
use bevy_tasks::block_on;

use crate::{self as bevy_asset, Asset};
use crate::Asset;

use super::*;

Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ecs/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1710,7 +1710,6 @@ fn sorted_remove<T: Eq + Ord + Copy>(source: &mut Vec<T>, remove: &[T]) {

#[cfg(test)]
mod tests {
use crate as bevy_ecs;
use crate::{component::HookContext, prelude::*, world::DeferredWorld};
use alloc::vec;

Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ecs/src/change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,6 @@ mod tests {
use core::panic::Location;

use crate::{
self as bevy_ecs,
change_detection::{
Mut, NonSendMut, Ref, ResMut, TicksMut, CHECK_TICK_THRESHOLD, MAX_CHANGE_AGE,
},
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ecs/src/component.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Types for declaring and storing [`Component`]s.
use crate::{
self as bevy_ecs,
archetype::ArchetypeFlags,
bundle::BundleInfo,
change_detection::MAX_CHANGE_AGE,
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_ecs/src/entity/clone_entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,12 +816,11 @@ impl<'w> EntityClonerBuilder<'w> {
mod tests {
use super::ComponentCloneCtx;
use crate::{
self as bevy_ecs,
component::{Component, ComponentCloneBehavior, ComponentDescriptor, StorageType},
entity::{hash_map::EntityHashMap, Entity, EntityCloner},
hierarchy::{ChildOf, Children},
reflect::{AppTypeRegistry, ReflectComponent, ReflectFromWorld},
resource::Resource,
prelude::{ChildOf, Children, Resource},
reflect::AppTypeRegistry,
reflect::{ReflectComponent, ReflectFromWorld},
system::Commands,
world::{FromWorld, World},
};
Expand All @@ -835,6 +834,7 @@ mod tests {
mod reflect {
use super::*;
use crate::{
component::{Component, ComponentCloneBehavior},
entity::EntityCloner,
reflect::{AppTypeRegistry, ReflectComponent, ReflectFromWorld},
system::Commands,
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ecs/src/entity/entity_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,6 @@ mod tests {
use crate::query::{QueryState, With};
use crate::system::Query;
use crate::world::Mut;
use crate::{self as bevy_ecs};

use super::UniqueEntityIter;

Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ecs/src/entity/visit_entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ impl VisitEntitiesMut for Entity {
#[cfg(test)]
mod tests {
use crate::{
self as bevy_ecs,
entity::{hash_map::EntityHashMap, MapEntities, SceneEntityMapper},
world::World,
};
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ecs/src/entity_disabling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
//! [`World`]: crate::prelude::World
//! [`Query` performance]: crate::prelude::Query#performance
use crate as bevy_ecs;
use crate::{
component::{ComponentId, Components, StorageType},
query::FilteredAccess,
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ecs/src/event/base.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate as bevy_ecs;
use crate::component::ComponentId;
use crate::world::World;
use crate::{component::Component, traversal::Traversal};
Expand Down
3 changes: 1 addition & 2 deletions crates/bevy_ecs/src/event/collections.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate as bevy_ecs;
use alloc::vec::Vec;
use bevy_ecs::{
event::{Event, EventCursor, EventId, EventInstance},
Expand Down Expand Up @@ -398,7 +397,7 @@ impl<E: Event> ExactSizeIterator for SendBatchIds<E> {

#[cfg(test)]
mod tests {
use crate::{self as bevy_ecs, event::Events};
use crate::event::Events;
use bevy_ecs_macros::Event;

#[test]
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ecs/src/event/event_cursor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate as bevy_ecs;
use bevy_ecs::event::{
Event, EventIterator, EventIteratorWithId, EventMutIterator, EventMutIteratorWithId, Events,
};
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ecs/src/event/iterators.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate as bevy_ecs;
#[cfg(feature = "multi_threaded")]
use bevy_ecs::batching::BatchingStrategy;
use bevy_ecs::event::{Event, EventCursor, EventId, EventInstance, Events};
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ecs/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ pub use writer::EventWriter;

#[cfg(test)]
mod tests {
use crate as bevy_ecs;
use alloc::{vec, vec::Vec};
use bevy_ecs::{event::*, system::assert_is_read_only_system};
use bevy_ecs_macros::Event;
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ecs/src/event/mut_iterators.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate as bevy_ecs;
#[cfg(feature = "multi_threaded")]
use bevy_ecs::batching::BatchingStrategy;
use bevy_ecs::event::{Event, EventCursor, EventId, EventInstance, Events};
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ecs/src/event/mutator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate as bevy_ecs;
#[cfg(feature = "multi_threaded")]
use bevy_ecs::event::EventMutParIter;
use bevy_ecs::{
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ecs/src/event/reader.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate as bevy_ecs;
#[cfg(feature = "multi_threaded")]
use bevy_ecs::event::EventParIter;
use bevy_ecs::{
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ecs/src/event/registry.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate as bevy_ecs;
use alloc::vec::Vec;
use bevy_ecs::{
change_detection::{DetectChangesMut, MutUntyped},
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ecs/src/event/update.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate as bevy_ecs;
use bevy_ecs::{
change_detection::Mut,
component::Tick,
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ecs/src/event/writer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate as bevy_ecs;
use bevy_ecs::{
event::{Event, EventId, Events, SendBatchIds},
system::{ResMut, SystemParam},
Expand Down
1 change: 0 additions & 1 deletion crates/bevy_ecs/src/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#[cfg(feature = "bevy_reflect")]
use crate::reflect::{ReflectComponent, ReflectFromWorld};
use crate::{
self as bevy_ecs,
bundle::Bundle,
component::{Component, HookContext},
entity::Entity,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/identifier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ mod tests {
// and also Entity flag.
let high = 0x7FFFFFFF;
let low = 0xC;
let bits: u64 = high << u32::BITS | low;
let bits: u64 = (high << u32::BITS) | low;

let id = Identifier::try_from_bits(bits).unwrap();

Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_ecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ compile_error!("bevy_ecs cannot safely compile for a 16-bit platform.");

extern crate alloc;

// Required to make proc macros work in bevy itself.
extern crate self as bevy_ecs;

pub mod archetype;
pub mod batching;
pub mod bundle;
Expand Down Expand Up @@ -128,7 +131,6 @@ pub mod __macro_exports {

#[cfg(test)]
mod tests {
use crate as bevy_ecs;
use crate::{
bundle::Bundle,
change_detection::Ref,
Expand Down
Loading

0 comments on commit 1b7db89

Please sign in to comment.