-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bevy_reflect: Contextual serialization error messages #13888
bevy_reflect: Contextual serialization error messages #13888
Conversation
26a00a8
to
d85de9b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great, I love the idea of trying to provide better error messages to users when possible. I have only a couple of non-blocking comments that I thought I'd mention, otherwise LGTM!
crates/bevy_reflect/src/serde/ser.rs
Outdated
pub fn new(value: &'a dyn Reflect, registry: &'a TypeRegistry) -> Self { | ||
#[cfg(feature = "debug_stack")] | ||
TYPE_INFO_STACK.set(crate::type_info_stack::TypeInfoStack::new()); | ||
|
||
TypedReflectSerializer { value, registry } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since TypedReflectSerializer
is responsible for creating/clearing then TypeInfoStack
, I feel like it should be possible to store the stack within this serializer, and pass references to it around like the TypeRegistry
, perhaps wrapping it all in a ReflectContext
.
I don't see any problems with the thread_local
implementation as-is beyond a general ick around global state; you're justification is perfectly valid. Just making a note here for a possible future PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't fully remember unfortunately.
I believe the reason was that the serializer would need to hold onto a &mut TypeInfoStack
but we don't want to require the user to define this themselves.
We might be able to get around this by creating an InternalReflectSerializer
. Then ReflectSerializer
can create and own the stack, and then pass a mutable reference to it to the internal serializer.
That would be a breaking change though. I'll try to look into it and see if it will actually work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just looked into this and while I believe it's possible to do this with an internal serializer, it wouldn't really work properly across threads.
This is because a Serialize
impl only requires a &self
reference. This means that two references to the same serializer could be sent to different threads and be serialized at the same time (maybe to send multiple network packets, save to separate files, etc.). Doing so would cause the stack to receive entries from both, throwing it completely off.
So for now, I think using a thread-local is probably the safest option.
Gentle reminder to fix merge conflicts here. |
d85de9b
to
e051c41
Compare
b931cc8
to
995999a
Compare
CI failures look real: you seem to have forgotten to feature flag an import. |
Used in serialization and deserialization
995999a
to
75076c0
Compare
Objective
Reflection serialization can be difficult to debug. A lot of times a type fails to be serialized and the user is left wondering where that type came from.
This is most often encountered with Bevy's scenes. Attempting to serialize all resources in the world will fail because some resources can't be serialized.
For example, users will often get complaints about
bevy_utils::Instant
not registeringReflectSerialize
. Well,Instant
can't be serialized, so the only other option is to exclude the resource that contains it. But what resource contains it? This is where reflection serialization can get a little tricky (it'sTime<Real>
btw).Solution
Add the
debug_stack
feature tobevy_reflect
. When enabled, the reflection serializers and deserializers will keep track of the current type stack. And this stack will be used in error messages to help with debugging.Now, if we unknowingly try to serialize
Time<Real>
, we'll get the following error:Implementation
This makes use of
thread_local!
to manage an internalTypeInfoStack
which holds a stack of&'static TypeInfo
. We push to the stack before a type is (de)serialized and pop from the stack afterwards.Using a thread-local should be fine since we know two (de)serializers can't be running at the same time (and if they're running on separate threads, then we're still good).
The only potential issue would be if a user went through one of the sub-serializers, like
StructSerializer
. However, I don't think many users are going through these types (I don't even know if we necessarily want to keep those public either, but we'll save that for a different PR). Additionally, this is just a debug feature that only affects error messages, so it wouldn't have any drastically negative effect. It would just result in the stack not being cleared properly if there were any errors.Lastly, this is not the most performant implementation since we now fetch the
TypeInfo
an extra time. But I figured that for a debug tool, it wouldn't matter too much.Feature
This also adds a
debug
feature, which enables thedebug_stack
feature.I added it because I think we may want to potentially add more debug tools in the future, and this gives us a good framework for adding those. Users who want all debug features, present and future, can just set
debug
. If they only want this feature, then they can just usedebug_stack
.I also made the
debug
feature default to help capture the widest audience (i.e. the users who want this feature but don't know they do). However, if we think it's better as a non-default feature, I can change it!And if there's any bikeshedding around the name
debug_stack
, let me know!Testing
Run the following command:
Changelog
debug
anddebug_stack
features tobevy_reflect
debug_stack
ordebug
feature is enabled