Repository: zng-ui/zng (language: rust)

Issue:
Fails to run examples
Fails to run examples on macos
Command
```bash
cargo do run animation
```

Getting error
```
thread 'main' panicked at crates/zng-app/src/running.rs:1289:9:
   cannot start App in view-process
widget path:
   <no-widget>
stack backtrace:
   0: std::backtrace_rs::backtrace::libunwind::trace
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/../../backtrace/src/backtrace/libunwind.rs:116:5
   1: std::backtrace_rs::backtrace::trace_unsynchronized
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2: std::backtrace::Backtrace::create
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/backtrace.rs:331:13
   3: zng_app::crash_handler::panic_handler
             at ./crates/zng-app/src/crash_handler.rs:1187:21
   4: core::ops::function::Fn::call
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/core/src/ops/function.rs:79:5
   5: <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/alloc/src/boxed.rs:2077:9
   6: std::panicking::rust_panic_with_hook
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panicking.rs:799:13
   7: std::panicking::begin_panic_handler::{{closure}}
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panicking.rs:656:13
   8: std::sys_common::backtrace::__rust_end_short_backtrace
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/sys_common/backtrace.rs:171:18
   9: rust_begin_unwind
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panicking.rs:652:5
  10: core::panicking::panic_fmt
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/core/src/panicking.rs:72:14
  11: zng_app::running::assert_not_view_process
             at ./crates/zng-app/src/running.rs:1289:9
  12: zng_app::APP::minimal
             at ./crates/zng-app/src/lib.rs:1175:9
  13: zng::defaults::<impl zng::APP>::defaults
             at ./crates/zng/src/lib.rs:866:21
  14: zng_example_animation::main
             at ./examples/animation/src/main.rs:19:5
  15: core::ops::function::FnOnce::call_once
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/core/src/ops/function.rs:250:5
  16: std::sys_common::backtrace::__rust_begin_short_backtrace
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/sys_common/backtrace.rs:155:18
  17: std::rt::lang_start::{{closure}}
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/rt.rs:159:18
  18: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/core/src/ops/function.rs:284:13
  19: std::panicking::try::do_call
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panicking.rs:559:40
  20: std::panicking::try
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panicking.rs:523:19
  21: std::panic::catch_unwind
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panic.rs:149:14
  22: std::rt::lang_start_internal::{{closure}}
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/rt.rs:141:48
  23: std::panicking::try::do_call
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panicking.rs:559:40
  24: std::panicking::try
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panicking.rs:523:19
  25: std::panic::catch_unwind
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panic.rs:149:14
  26: std::rt::lang_start_internal
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/rt.rs:141:20
  27: std::rt::lang_start
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/rt.rs:158:17
  28: _main

ERROR zng_view_api::app_process: view-process exit code: 101
thread 'main' panicked at crates/zng-view-api/src/app_process.rs:103:86:
   failed to spawn or connect to view-process: "timeout, did not connect in 10 seconds"
widget path:
   <no-widget>
stack backtrace:
   0: std::backtrace_rs::backtrace::libunwind::trace
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/../../backtrace/src/backtrace/libunwind.rs:116:5
   1: std::backtrace_rs::backtrace::trace_unsynchronized
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2: std::backtrace::Backtrace::create
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/backtrace.rs:331:13
   3: zng_app::crash_handler::panic_handler
             at ./crates/zng-app/src/crash_handler.rs:1187:21
   4: core::ops::function::Fn::call
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/core/src/ops/function.rs:79:5
   5: <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/alloc/src/boxed.rs:2077:9
   6: std::panicking::rust_panic_with_hook
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panicking.rs:799:13
   7: std::panicking::begin_panic_handler::{{closure}}
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panicking.rs:664:13
   8: std::sys_common::backtrace::__rust_end_short_backtrace
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/sys_common/backtrace.rs:171:18
   9: rust_begin_unwind
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panicking.rs:652:5
  10: core::panicking::panic_fmt
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/core/src/panicking.rs:72:14
  11: core::result::unwrap_failed
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/core/src/result.rs:1679:5
  12: core::result::Result<T,E>::expect
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/core/src/result.rs:1059:23
  13: zng_view_api::app_process::Controller::start_impl
             at ./crates/zng-view-api/src/app_process.rs:103:13
  14: zng_view_api::app_process::Controller::start
             at ./crates/zng-view-api/src/app_process.rs:89:9
  15: zng_app::view_process::VIEW_PROCESS::start
             at ./crates/zng-app/src/view_process.rs:312:23
  16: zng_app::running::AppIntrinsic::pre_init
             at ./crates/zng-app/src/running.rs:1236:13
  17: zng_app::running::RunningApp<E>::start
             at ./crates/zng-app/src/running.rs:98:23
  18: zng_app::AppExtended<alloc::vec::Vec<alloc::boxed::Box<dyn zng_app::AppExtensionBoxed>>>::run_dyn
             at ./crates/zng-app/src/lib.rs:1241:19
  19: zng_app::AppExtended<E>::run_impl
             at ./crates/zng-app/src/lib.rs:1289:9
  20: zng_app::AppExtended<E>::run
             at ./crates/zng-app/src/lib.rs:1383:9
  21: <zng_app::AppExtended<E> as zng_ext_window::AppRunWindowExt>::run_window
             at ./crates/zng-ext-window/src/lib.rs:171:9
  22: zng_example_animation::main
             at ./examples/animation/src/main.rs:19:5
  23: core::ops::function::FnOnce::call_once
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/core/src/ops/function.rs:250:5
  24: std::sys_common::backtrace::__rust_begin_short_backtrace
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/sys_common/backtrace.rs:155:18
  25: std::rt::lang_start::{{closure}}
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/rt.rs:159:18
  26: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/core/src/ops/function.rs:284:13
  27: std::panicking::try::do_call
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panicking.rs:559:40
  28: std::panicking::try
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panicking.rs:523:19
  29: std::panic::catch_unwind
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panic.rs:149:14
  30: std::rt::lang_start_internal::{{closure}}
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/rt.rs:141:48
  31: std::panicking::try::do_call
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panicking.rs:559:40
  32: std::panicking::try
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panicking.rs:523:19
  33: std::panic::catch_unwind
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panic.rs:149:14
  34: std::rt::lang_start_internal
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/rt.rs:141:20
  35: std::rt::lang_start
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/rt.rs:158:17
  36: _main
```


Discussion / hints:
Thanks for the report! I don't have access to a macOS right now, can you tell me if the `respawn` and `window` examples also fail? Respawn does not use prebuilt view and window runs prebuilt in the same process.
I can reproduce the issue, next release with a fix is this weekend

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0fc562c03..99d0b3f04 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,6 @@
 # Unreleased
 
+* Fix `"view_prebuilt"` linking on macOS.
 * Fix panic on old macOS (<11). Color scheme and accent is only supported >=11.
 * Fix `cargo zng fmt` of widgets with multi value property assigns.
 
diff --git a/crates/zng-env/src/process.rs b/crates/zng-env/src/process.rs
index f9c7e40b1..4acc3f5ff 100644
--- a/crates/zng-env/src/process.rs
+++ b/crates/zng-env/src/process.rs
@@ -57,7 +57,17 @@ use parking_lot::Mutex;
 ///
 /// Try to match the version used by `zng-env`.
 ///
+/// # Linker Optimizer Issues
+///
+/// The macOS system linker can "optimize" away crates that are only referenced via this macro, that is, a crate dependency
+/// that is not otherwise directly addressed by code. To workaround this issue you can add a bogus reference to the crate code, something
+/// that is not trivial to optimize away. Unfortunately this code must be added on the dependent crate, or on an intermediary dependency,
+/// if your crate is at risk of being used this way please document this issue.
+///
+/// See [`zng#437`] for an example of how to fix this issue.
+///
 /// [`wasm_bindgen`]: https://crates.io/crates/wasm-bindgen
+/// [`zng#437`]: https://github.com/zng-ui/zng/pull/437
 #[macro_export]
 macro_rules! on_process_start {
     ($closure:expr) => {
diff --git a/crates/zng/src/lib.rs b/crates/zng/src/lib.rs
index 22eb406b5..af1fd5e13 100644
--- a/crates/zng/src/lib.rs
+++ b/crates/zng/src/lib.rs
@@ -949,6 +949,15 @@ mod defaults {
                 tracing::debug!("defaults init, single_instance set");
             }
         }
+
+        fn deinit(&mut self) {
+            // ensure zng_view_prebuilt is linked, macOS system linker can "optimize" the entire
+            // crate away because it is only referenced by `linkme` in `on_process_start!`
+            #[cfg(all(view_prebuilt, any(target_os = "macos", target_os = "ios")))]
+            if std::env::var("=").is_ok() {
+                crate::view_process::prebuilt::run_same_process(|| unreachable!());
+            }
+        }
     }
 }
 


Test patch: