本篇回擼一把《Rust企業級應用最佳實踐》,講者分享了Rust應用的“最后一公里”中所解決的問題和有效實踐,非常接地氣。
Speaker: Liao Yiming (廖意明)
1. 面向CI的Cargo工具
stages:
- build
build_release:
stage: build
script:
- ...
- cargo fmt
- cargo fix
- cargo fix
- RUSTFLAGS="-D warnings" cargo clippy
- cargo build --release
本章節分享的最佳實踐:在做人工的Code Review之前,盡可能的利用自動化檢查工具進行預審查,并展示了一個構件腳本。
這個腳本要求開發者在提交時,要在本地做好cargo fmt
、fix
、clippy
,否則CI流水線是無法通過的。
腳本中有兩處cargo fix
,這是一個trick:如果在第一處cargo fix修改了代碼,就會導致第二個cargo fix
因為code dirty而無法通過。
接下來的cargo clippy
的 -D warnings
參數,表示構建不接受warning,開發者可以在代碼中添加#![deny(warnings)]
,或者在本地運行cargo clippy -- -D warnings
來檢查是否滿足該要求。
2. SemVer
本章節介紹了定義依賴時的語義化版本的概念,如上圖。
下面是一些自定義升級策略的例子。其中"^0.2.3"之所以不能自動升級到“1.0.0”是因為在語義化版本中,第一位Major位為0,表示不穩定,所以升級幅度會有限制。
[dependencies]
kov = "=1.2.3" # 可用版本:1.2.
kov = "^1.2.3" # 可用版本:>= 1.2.3 且 < 2.0.0
kov = "^1.2" # 可用版本:>= 1.2.0 且 < 2.0.0
kov = "^1" # 可用版本:>= 1.0.0 且 < 2.0.0
kov = "^0.2.3" # 可用版本:>= 0.2.3 且 < 0.3.0
kov = "^0.2" # 可用版本:>= 0.2.0 且 < 0.3.0
kov = "^0.0.3" # 可用版本:>= 0.0.3 且 < 0.0.4
kov = "^0.0" # 可用版本:>= 0.0.0 且 < 0.1.0
kov = "^0" # 可用版本:>= 0.0.0 且 < 1.0.0
kov = "*" # 可用版本:>= 0.0.0
kov = "1.*" # 可用版本:>= 1.0.0 且 < 2.0.0
kov = "1.2.*" # 可用版本:>= 1.0.0 且 < 2.0.0
kov = ">1.2.3" # 可用版本:> 1.2.3
kov = ">1.2.3 <1.2.17" # 可用版本:> 1.2.3 且 < 1.2.17
kov = "<=1.2.3" # 可用版本:<= 1.2.3
如果大家想去試更多的case,可以試下這個在線計算器semver calculator。
講者在本章節分享了自己遇到的幾次“飯后編譯失敗”的經歷。造成的原因是:語義化版本的兼容性,是由開發者人為保證的,所以有可能出錯。如果出現了因為Cargo Update
導致的編譯失敗,可以通過前面的kov = "=1.2.3"
強制鎖定版本來解決。
本章關于語義化版本的最佳實踐:
- 不要使用通配符
*
; - 盡可能明確版本“x.y.z”,并通過
cargo update -p cratename
來指定升級,而不要cargo update
進行大面積升級; - 對于crate提供者,一旦出現兼容性問題,馬上進行
cargo yank
,可以阻止還沒用過問題版本的用戶看到此版本。
3. 私庫依賴
Cargo.toml
中的依賴,除了指定語義化版本之外,在私有代碼場景中,可以用git依賴的方式,比如下面列舉的默認分支、指定分支、commit id、tag等等。
rand = {git="https://github.com/rust-lang-nursey/rand"}
rand = {git="https://github.com/rust-lang-nursey/rand", branch="next"}
rand = {git="https://github.com/rust-lang-nursey/rand", rev="39a7x2"}
rand = {git="https://github.com/rust-lang-nursey/rand",tag="0.3.1"}
但是,這會帶來“多模塊依賴問題”的問題。如下圖所示:
<figcaption style="margin-top: 0.66667em; padding: 0px 1em; font-size: 0.9em; line-height: 1.5; text-align: center; color: rgb(153, 153, 153);">來源:講者PPT</figcaption>
Error: perhaps two different version of crate 'x' are being used?
講者分享了他發現的一個解決方案:在Rust 1.34.0引入的Alternate Register,可以向私有庫進行語義化版本的發布:
- ~/.cargo/config
[registries]
my-registry={index="https://my-intranet:8080/git/index"}
- cargo login --registry=my-registry
- cargo publish --registry=my-registry
- Cargo.toml
[dependencies]
other-crate={version="1.0",registry="my-registry"}
本章的建議:避免由開發者在本地進行隨意的發布,應該在CI流水線在合適的時機進行自動化發布。
4. 構建腳本
本章分享了Rust的構建腳本,在Cargo.toml
中的package
中添加build項,如下圖所示。其中build.rs
文件目錄同Cargo.toml
即可。
[package]
name = "demo"
version = "1.0.0"
edition = "2018"
build = "build.rs"
構建腳本,可以把很多額外的信息動態加入到編譯后的可執行文件中,包括可執行文件的當前版本、編譯環境、系統版本等,方便追溯。
為了更快捷的創建構建腳本,講者開源了一個構建腳本工具shadow-rs:shadow-rs allows you to recall properties of the build process and environment at runtime, including:
- Cargo.toml project version
- Dependency information
- The Git commit that produced the build artifact (binary)
- What version of the rust toolchain was used in compilation
- The build variant, e.g. debug or release
- (And more)
來加顆star吧。