Repository: bevyengine/bevy
File: crates/bevy_ecs/src/system/system_registry.rs (Rust)

Code before:
            if system_id_marker.input_type_id.type_id != TypeId::of::<I>()
                || system_id_marker.output_type_id.type_id != TypeId::of::<O>()
            {
                return Err(RegisteredSystemError::IncorrectType(
                    id,
                    system_id_marker.clone(),
                ));
            }
            return Err(RegisteredSystemError::MissingRegisteredSystemComponent(id));
        };

        let mut system = registered_system
            .system
            .take()
            .ok_or(RegisteredSystemError::SystemMissing(id))?;

        // Initialize the system
        if !registered_system.initialized {
            system.initialize(self);
        }

        // refresh hotpatches for stored systems
        #[cfg(feature = "hotpatching")]
        if self
            .get_resource_ref::<HotPatchChanges>()
            .map(|r| r.last_changed())
            .unwrap_or_default()
            .is_newer_than(system.get_last_run(), self.change_tick())
        {
            system.refresh_hotpatch();
        }

        // Wait to run the commands until the system is available again.
        // This is needed so the systems can recursively run themselves.
        let result = system.run_without_applying_deferred(input, self);
        system.queue_deferred(self.into());

        // Return ownership of system trait object (if entity still exists)
        if let Ok(mut entity) = self.get_entity_mut(id.entity)
            && let Some(mut registered_system) = entity.get_mut::<RegisteredSystem<I, O>>()
        {
            registered_system.system = Some(system);
            registered_system.initialized = true;
        }

        // Run any commands enqueued by the system
        self.flush();
        Ok(result?)
    }

    /// Registers a system or returns its cached [`SystemId`].

Review comment (bug):
This is technically a change, but I think it's a bug fix.  The old code unwrapped the `Tick`, giving a tick of 0.  But comparing zero to another tick will give unpredictable results!  So I instead copied the behavior from `observer/runner.rs`, which compared first and did `.unwrap_or(true)`.  

Code after:
            if system_id_marker.input_type_id.type_id != TypeId::of::<I>()
                || system_id_marker.output_type_id.type_id != TypeId::of::<O>()
            {
                return Err(RegisteredSystemError::IncorrectType(
                    id,
                    system_id_marker.clone(),
                ));
            }
            return Err(RegisteredSystemError::MissingRegisteredSystemComponent(id));
        };

        let mut system = registered_system
            .system
            .take()
            .ok_or(RegisteredSystemError::SystemMissing(id))?;

        // Initialize the system
        if !registered_system.initialized {
            system.initialize(self);
        }

        // refresh hotpatches for stored systems
        #[cfg(feature = "hotpatching")]
        if self
            .get_resource_ref::<HotPatchChanges>()
            .is_none_or(|r| r.is_changed_after(system.get_last_run()))
        {
            system.refresh_hotpatch();
        }

        // Wait to run the commands until the system is available again.
        // This is needed so the systems can recursively run themselves.
        let result = system.run_without_applying_deferred(input, self);
        system.queue_deferred(self.into());

        // Return ownership of system trait object (if entity still exists)
        if let Ok(mut entity) = self.get_entity_mut(id.entity)
            && let Some(mut registered_system) = entity.get_mut::<RegisteredSystem<I, O>>()
        {
            registered_system.system = Some(system);
            registered_system.initialized = true;
        }

        // Run any commands enqueued by the system
        self.flush();
        Ok(result?)
    }

    /// Registers a system or returns its cached [`SystemId`].
    ///
    /// If you want to run the system immediately and you don't need its `SystemId`, see