|
| 1 | +# Desktop Native |
| 2 | + |
| 3 | +Desktop Native (DN) is a Rust project that represents all of the native operating system logic used |
| 4 | +in the Desktop client. |
| 5 | + |
| 6 | +The DN code is effectively a library that is exposed to Electron via |
| 7 | +[napi-rs](https://napi.rs/docs/introduction/getting-started). See [Napi](#napi) for more |
| 8 | +information. |
| 9 | + |
| 10 | +## Dependencies |
| 11 | + |
| 12 | +1. [Rust](https://www.rust-lang.org/tools/install) |
| 13 | +2. The nightly toolchain: `rustup toolchain install nightly` |
| 14 | +3. Cargo binaries for pre-commit hooks |
| 15 | + - [cargo-sort](https://crates.io/crates/cargo-sort) |
| 16 | + - [cargo-udeps](https://crates.io/crates/cargo-udeps) |
| 17 | + - [cargo-deny](https://crates.io/crates/cargo-deny) |
| 18 | + |
| 19 | +## Compiling |
| 20 | + |
| 21 | +To ensure the code will compile, a faster check than building binaries is to use `check`. |
| 22 | + |
| 23 | +``` |
| 24 | + cargo check |
| 25 | +``` |
| 26 | + |
| 27 | +The binary target can be built as well, however there is marginal gain from that since the |
| 28 | +compilation as part of running the Desktop electron app will be building the executable code. |
| 29 | + |
| 30 | +``` |
| 31 | + cargo build |
| 32 | +``` |
| 33 | + |
| 34 | +## Checks |
| 35 | + |
| 36 | +The following checks are run in CI and also as pre-commit hooks. They can also be run manually. |
| 37 | + |
| 38 | +``` |
| 39 | +cargo +nightly fmt --check |
| 40 | +``` |
| 41 | + |
| 42 | +``` |
| 43 | +cargo +nightly clippy --all-features --all-targets --tests -- -D warnings |
| 44 | +``` |
| 45 | + |
| 46 | +## Automated testing |
| 47 | + |
| 48 | +Tests are invoked in CI and can be ran manually with the below commands. |
| 49 | + |
| 50 | +### Examples |
| 51 | + |
| 52 | +Execute the unit and integration tests: |
| 53 | + |
| 54 | +``` |
| 55 | +cargo test |
| 56 | +``` |
| 57 | + |
| 58 | +Execute the only unit tests: |
| 59 | + |
| 60 | +``` |
| 61 | +cargo test --lib |
| 62 | +``` |
| 63 | + |
| 64 | +## Cross compiling |
| 65 | + |
| 66 | +Since the Desktop client supports multiple platforms, much of the code is compile-time gated with |
| 67 | +`cfg` flags. [Cross](https://crates.io/crates/cross) can be useful to check compilation of |
| 68 | +non-native OS specific code. |
| 69 | + |
| 70 | +``` |
| 71 | +cargo install cross --git https://github.com/cross-rs/cross |
| 72 | +``` |
| 73 | + |
| 74 | +### Examples |
| 75 | + |
| 76 | +Check compilation for Linux: |
| 77 | + |
| 78 | +``` |
| 79 | +cross check --target x86_64-unknown-linux-gnu |
| 80 | +``` |
| 81 | + |
| 82 | +Run `clippy` for Intel Macs: |
| 83 | + |
| 84 | +``` |
| 85 | +cross clippy --target x86_64-apple-darwin --all-features --tests |
| 86 | +``` |
| 87 | + |
| 88 | +Run unit tests for Windows: |
| 89 | + |
| 90 | +``` |
| 91 | +cross test --lib --target x86_64-pc-windows-gnu |
| 92 | +``` |
| 93 | + |
| 94 | +Run integration tests for Apple Silicon Macs: |
| 95 | + |
| 96 | +``` |
| 97 | +cross --target aarch64-apple-darwin |
| 98 | +``` |
| 99 | + |
| 100 | +## Napi |
| 101 | + |
| 102 | +`napi-rs` provides an abstraction layer by wrapping the C API that Node.js exposes, in safe Rust |
| 103 | +bindings. This enables development without having to manually write unsafe FFI. |
| 104 | + |
| 105 | +Most feature crates in the DN workspace have a Napi layer in the project's |
| 106 | +[napi](https://github.com/bitwarden/clients/tree/main/apps/desktop/desktop_native/napi/src) crate |
| 107 | +source. This Napi layer is compiled as part of the normal build processes, and `napi` is a standard |
| 108 | +crate dependency in the project's workspace. |
| 109 | + |
| 110 | +::::note |
| 111 | + |
| 112 | +In order to actually generate the bindings, |
| 113 | +[building the Desktop client](../index.mdx#build-native-module) in the parent directory is |
| 114 | +necessary. |
| 115 | + |
| 116 | +:::: |
0 commit comments