Skip to content

Commit 63c0103

Browse files
committed
fix bug causing incomplete codegen
1 parent 891415e commit 63c0103

File tree

16 files changed

+1967
-40
lines changed

16 files changed

+1967
-40
lines changed

crates/bevy_api_gen/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ convert_case = "0.6"
5454
syn = { version = "2", features = ["parsing"], no-default-features = true }
5555
clap-verbosity-flag = "2.2"
5656
itertools = "0.12"
57+
chrono = "0.4"
5758

5859
[build-dependencies]
5960
toml = "0.8"

crates/bevy_api_gen/src/bin/driver.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
use bevy_api_gen::*;
33

44
fn main() {
5+
// initially set it to high so no logs are missed, but later when we parse the args we will set it to the correct level
6+
std::env::set_var("RUST_LOG", "trace");
57
env_logger::init();
68
rustc_plugin::driver_main(BevyAnalyzer);
79
}

crates/bevy_api_gen/src/bin/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ fn main() {
2626
}
2727
env_logger::init();
2828

29+
info!("Using RUST_LOG: {:?}", env::var("RUST_LOG"));
30+
2931
info!("Computing crate metadata");
3032
let metadata = cargo_metadata::MetadataCommand::new()
3133
.no_deps()

crates/bevy_api_gen/src/callback.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ impl rustc_driver::Callbacks for BevyAnalyzerCallbacks {
5959
// tera environment for import processor
6060
let tera = crate::configure_tera(tcx.crate_name(LOCAL_CRATE).as_str(), &templates_dir);
6161

62+
info!("Using meta directories: {:?}", meta_dirs);
6263
let mut ctxt = crate::BevyCtxt::new(
6364
tcx,
6465
&meta_dirs,

crates/bevy_api_gen/src/meta.rs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ use std::{
66
};
77

88
use cargo_metadata::camino::Utf8PathBuf;
9+
use chrono::NaiveDateTime;
910
use log::trace;
1011
use rustc_hir::def_id::DefPathHash;
11-
use serde::{Deserialize, Serialize};
12+
use serde::{Deserialize, Serialize, Serializer};
1213

1314
use crate::WorkspaceMeta;
1415

@@ -25,6 +26,27 @@ pub struct Meta {
2526
/// False if no files are going to be generated for this crate
2627
pub(crate) will_generate: bool,
2728
pub(crate) meta_version: String,
29+
#[serde(
30+
serialize_with = "serialize_timestamp",
31+
deserialize_with = "deserialize_timestamp"
32+
)]
33+
pub(crate) timestamp: NaiveDateTime,
34+
}
35+
36+
fn serialize_timestamp<S: Serializer>(
37+
timestamp: &NaiveDateTime,
38+
serializer: S,
39+
) -> Result<S::Ok, S::Error> {
40+
// format as date and time
41+
serializer.serialize_str(&timestamp.format("%Y-%m-%d %H:%M:%S").to_string())
42+
}
43+
44+
fn deserialize_timestamp<'de, D>(deserializer: D) -> Result<NaiveDateTime, D::Error>
45+
where
46+
D: serde::Deserializer<'de>,
47+
{
48+
let s = String::deserialize(deserializer)?;
49+
NaiveDateTime::parse_from_str(&s, "%Y-%m-%d %H:%M:%S").map_err(serde::de::Error::custom)
2850
}
2951

3052
impl Meta {
@@ -37,7 +59,7 @@ impl Meta {
3759
}
3860
}
3961

40-
#[derive(Serialize, Deserialize, Clone)]
62+
#[derive(Serialize, Deserialize, Clone, Debug)]
4163
pub(crate) struct ProxyMeta {
4264
pub(crate) ident: String,
4365
pub(crate) stable_crate_id: u64,
@@ -46,8 +68,8 @@ pub(crate) struct ProxyMeta {
4668

4769
/// Manages deserialisation and retrieval of meta files
4870
pub struct MetaLoader {
49-
pub(crate) meta_dirs: Vec<Utf8PathBuf>,
50-
pub(crate) workspace_meta: WorkspaceMeta,
71+
pub meta_dirs: Vec<Utf8PathBuf>,
72+
pub workspace_meta: WorkspaceMeta,
5173
cache: RefCell<HashMap<String, Meta>>,
5274
}
5375

@@ -78,7 +100,7 @@ impl MetaLoader {
78100
) -> bool {
79101
let meta = match meta_sources
80102
.iter()
81-
.filter(|s| curr_source.is_none() || curr_source.is_some_and(|cs| cs == **s))
103+
.filter(|s| curr_source.is_none() || curr_source.is_some_and(|cs| cs != **s))
82104
.find_map(|s| self.meta_for(s))
83105
{
84106
Some(meta) => meta,
@@ -100,7 +122,7 @@ impl MetaLoader {
100122

101123
if meta.is_none() {
102124
log::trace!(
103-
"Could not find meta for crate: `{}`, is_workspace_and_included: '{}'",
125+
"Could not find meta file for crate: `{}`, is_workspace_and_included: '{}'",
104126
crate_name,
105127
needs_meta
106128
)
@@ -118,11 +140,15 @@ impl MetaLoader {
118140
trace!("Loading meta from cache for: {}", crate_name);
119141
cache.get(crate_name).cloned()
120142
} else {
121-
trace!("Loading meta from filesystem for: {}", crate_name);
122143
drop(cache);
123144
let mut cache = self.cache.borrow_mut();
124-
let meta =
125-
Self::opt_load_meta(dir.join(Self::crate_name_to_meta_filename(crate_name)))?;
145+
let dir = dir.join(Self::crate_name_to_meta_filename(crate_name));
146+
trace!(
147+
"Attempting to load meta from filesystem for crate: {}, at: {}",
148+
crate_name,
149+
dir
150+
);
151+
let meta = Self::opt_load_meta(dir)?;
126152
cache.insert(crate_name.to_owned(), meta.clone());
127153
Some(meta)
128154
}
@@ -150,7 +176,7 @@ impl MetaLoader {
150176

151177
let file = File::create(&path).unwrap();
152178
let mut writer = BufWriter::new(file);
153-
serde_json::to_writer(&mut writer, meta).unwrap();
179+
serde_json::to_writer_pretty(&mut writer, meta).unwrap();
154180
writer.flush().expect("Could not flush data to meta file");
155181
}
156182

crates/bevy_api_gen/src/passes/find_methods_and_fields.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,11 +350,19 @@ fn type_is_adt_and_reflectable<'tcx>(
350350
tcx.def_path_hash(did),
351351
);
352352

353-
log::trace!(
354-
"Meta for type: `{}`, contained in meta `{}`",
355-
tcx.item_name(did),
356-
contains_hash
357-
);
353+
if contains_hash {
354+
log::info!(
355+
"Meta for type: `{}` with hash: `{:?}`, contained in the meta file",
356+
tcx.item_name(did),
357+
tcx.def_path_hash(did),
358+
);
359+
} else {
360+
log::info!(
361+
"Meta for type: `{}` with hash: `{:?}`, was not found in meta files for {crate_name} or in bevy_reflect, meaning it will not generate a proxy.",
362+
tcx.item_name(did),
363+
tcx.def_path_hash(did),
364+
);
365+
}
358366

359367
contains_hash
360368
})

crates/bevy_api_gen/src/passes/populate_template_data.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,6 @@ pub(crate) fn populate_template_data(ctxt: &mut BevyCtxt<'_>, args: &Args) -> bo
125125
}
126126

127127
trace!("Populated template context:");
128-
trace!(
129-
"{}",
130-
serde_json::to_string_pretty(&ctxt.template_context).unwrap()
131-
);
132128

133129
true
134130
}
@@ -242,11 +238,15 @@ fn ty_to_string<'tcx>(ctxt: &BevyCtxt<'tcx>, ty: Ty<'tcx>, proxy_types: bool) ->
242238
"bevy_reflect",
243239
];
244240

245-
ctxt.meta_loader.one_of_meta_files_contains(
241+
trace!("Checking ADT: `{}`.", ctxt.tcx.item_name(def_id),);
242+
243+
let contains = ctxt.meta_loader.one_of_meta_files_contains(
246244
&meta_sources,
247-
Some(&ctxt.tcx.crate_name(LOCAL_CRATE).to_ident_string()),
245+
None,
248246
def_path_hash,
249-
)
247+
);
248+
249+
contains
250250
})
251251
.is_some_and(identity)
252252
}),
@@ -338,7 +338,6 @@ impl<'a> TyPrinter<'a> {
338338
}
339339

340340
pub fn print(mut self, ty: Ty<'_>) -> String {
341-
log::trace!("Printing type: {:#?}", ty);
342341
self.print_ty(ty);
343342
self.buffer
344343
}
@@ -369,16 +368,13 @@ impl<'a> TyPrinter<'a> {
369368
}
370369

371370
fn print_adt<'tcx, I: Iterator<Item = GenericArg<'tcx>>>(&mut self, ty: AdtDef<'tcx>, args: I) {
372-
log::trace!("Printing ADT: {:#?}", ty);
373371
let did = ty.did();
374372
let import_path = (self.path_finder)(did);
375373
self.buffer.push_str(&import_path);
376374
self.print_args(args);
377375
}
378376

379377
fn print_ty(&mut self, ty: Ty<'_>) {
380-
log::trace!("Printing type: {:#?}", ty);
381-
382378
match ty.kind() {
383379
TyKind::Bool => self.print_literal("bool"),
384380
TyKind::Char => self.print_literal("char"),

crates/bevy_api_gen/src/passes/write_meta.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub(crate) fn write_meta(ctxt: &mut BevyCtxt<'_>, _args: &Args) -> bool {
2020
proxies,
2121
will_generate,
2222
meta_version: META_VERSION.to_string(),
23+
timestamp: chrono::Local::now().naive_local(),
2324
};
2425

2526
ctxt.meta_loader

crates/bevy_api_gen/src/plugin.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ impl RustcPlugin for BevyAnalyzer {
3232
compiler_args: Vec<String>,
3333
plugin_args: Self::Args,
3434
) -> rustc_interface::interface::Result<()> {
35+
log::set_max_level(plugin_args.verbose.get_log_level().to_level_filter());
36+
3537
if let Some(includes) = WorkspaceMeta::from_env().include_crates {
3638
let crate_name = compiler_args
3739
.iter()

crates/bevy_mod_scripting_functions/src/bevy_bindings/bevy_hierarchy.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ impl ::bevy::app::Plugin for BevyHierarchyScriptingPlugin {
5757
.into();
5858
output
5959
},
60+
)
61+
.register(
62+
"get",
63+
|_self: Ref<bevy::hierarchy::prelude::Parent>| {
64+
let output: Val<bevy::ecs::entity::Entity> = bevy::hierarchy::prelude::Parent::get(
65+
&_self,
66+
)
67+
.into();
68+
output
69+
},
6070
);
6171
NamespaceBuilder::<::bevy::hierarchy::HierarchyEvent>::new(world)
6272
.register(

0 commit comments

Comments
 (0)