Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 166 additions & 0 deletions .github/workflows/rust-core.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
name: Rust Core

# Builds, lints and tests only the Rust Core crates a pull request can affect:
# the crates whose files changed and every crate that depends on them, as
# chosen by scripts/rust-core/affected-crates.mjs. Linux checks every selected
# crate. macOS and Windows run the tests of the selected crates that declare
# those hosts, once the pull request is ready for review and Linux passed.
#
# There is no path filter: a change that affects no crate still reports the
# "Rust Core gate" check, which branch rules can require.
on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review]
workflow_dispatch:

permissions:
contents: read

concurrency:
group: rust-core-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

env:
CARGO_TERM_COLOR: always
# CI compiles each crate once: incremental state and full debug information
# would only cost time and disk space.
CARGO_INCREMENTAL: 0
CARGO_PROFILE_DEV_DEBUG: line-tables-only

jobs:
select:
name: Select crates
runs-on: ubuntu-latest
timeout-minutes: 10
outputs:
rust: ${{ steps.select.outputs.rust }}
packages: ${{ steps.select.outputs.packages }}
test-args: ${{ steps.select.outputs.test-args }}
crate-directories: ${{ steps.select.outputs.crate-directories }}
hosts: ${{ steps.select.outputs.hosts }}
steps:
- uses: actions/checkout@v7
with:
# A pull request checks out its merge commit, whose first parent is
# the branch it merges into.
fetch-depth: 2
- uses: actions/setup-node@v6
with:
node-version: 22
- uses: dtolnay/rust-toolchain@stable

- name: Test the CI scripts
run: node --test scripts/rust-core/*.test.mjs
- name: Check that every test runs on a host CI tests its crate on
run: node scripts/rust-core/verify-platform-tests.mjs
- name: Select crates
id: select
env:
# A manual run checks every crate.
SCOPE: ${{ github.event_name == 'pull_request' && '--base HEAD^1' || '--all' }}
run: |
read -ra scope <<< "$SCOPE"
node scripts/rust-core/affected-crates.mjs --github "${scope[@]}"

linux:
name: Linux
needs: select
if: needs.select.outputs.rust == 'true'
runs-on: ubuntu-latest
timeout-minutes: 60
env:
# One build directory for the lints, Rustdoc and the tests.
CARGO_TARGET_DIR: ${{ github.workspace }}/rust/target
PACKAGES: ${{ needs.select.outputs.packages }}
TEST_ARGS: ${{ needs.select.outputs.test-args }}
CRATE_DIRECTORIES: ${{ needs.select.outputs.crate-directories }}
defaults:
run:
shell: bash
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v6
with:
node-version: 22
- uses: dtolnay/rust-toolchain@stable
with:
components: clippy, rustfmt
targets: x86_64-pc-windows-msvc
# Only downloaded crates are cached: build output would not fit the
# repository cache quota next to the SDK end-to-end cache.
- uses: actions/cache@v6
with:
path: |
~/.cargo/registry/index
~/.cargo/registry/cache
~/.cargo/git/db
key: rust-core-cargo-${{ hashFiles('rust/Cargo.lock', 'scripts/rust-core/comment-lexer/Cargo.lock') }}
restore-keys: rust-core-cargo-

- name: Check formatting
run: cargo fmt --manifest-path rust/Cargo.toml --all -- --check
- name: Check the log event catalog
run: node scripts/logging/generate-log-events.mjs --check
- name: Check comments
run: |
read -ra directories <<< "$CRATE_DIRECTORIES"
scripts/rust-core/verify-rust-core-comments.sh "${directories[@]}"
- name: Lint
run: |
read -ra packages <<< "$PACKAGES"
cargo clippy --locked --manifest-path rust/Cargo.toml "${packages[@]}" --all-targets --all-features -- -D warnings
# Code behind cfg(windows) compiles only for Windows. Checking it here
# finds its errors without a Windows runner; default features only,
# because the remote server feature targets Unix hosts.
- name: Lint for Windows
run: |
read -ra packages <<< "$PACKAGES"
cargo clippy --locked --manifest-path rust/Cargo.toml "${packages[@]}" --all-targets --target x86_64-pc-windows-msvc -- -D warnings
- name: Test
run: |
read -ra arguments <<< "$TEST_ARGS"
cargo test --locked --manifest-path rust/Cargo.toml "${arguments[@]}"

hosts:
name: ${{ matrix.name }}
needs: [select, linux]
# Draft pull requests run Linux only, like the desktop repository.
if: needs.select.outputs.hosts != '[]' && (github.event_name != 'pull_request' || !github.event.pull_request.draft)
strategy:
fail-fast: false
matrix:
include: ${{ fromJSON(needs.select.outputs.hosts) }}
runs-on: ${{ matrix.runner }}
timeout-minutes: 45
defaults:
run:
shell: bash
env:
TEST_ARGS: ${{ matrix.test-args }}
steps:
- uses: actions/checkout@v7
- uses: dtolnay/rust-toolchain@stable

- name: Test
run: |
read -ra arguments <<< "$TEST_ARGS"
cargo test --locked --manifest-path rust/Cargo.toml "${arguments[@]}"

gate:
name: Rust Core gate
needs: [select, linux, hosts]
if: always()
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
# A job that had nothing to check is skipped; any other result fails.
- name: Require every job to pass or be skipped
env:
NEEDS: ${{ toJSON(needs) }}
run: |
jq -r 'to_entries[] | "\(.key): \(.value.result)"' <<< "$NEEDS"
failed="$(jq -r '[to_entries[] | select(.value.result != "success" and .value.result != "skipped") | .key] | join(", ")' <<< "$NEEDS")"
if [[ -n "$failed" ]]; then
echo "::error::Rust Core jobs did not pass: $failed"
exit 1
fi
115 changes: 115 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# 参与 Rust Core 开发

本页规定平台相关代码怎么写、平台测试放在哪里,以及 CI 怎样只运行受影响的 crate。插件契约和 SDK 的规则见 [`shared/plugins/README.md`](shared/plugins/README.md)。

## 平台差异写成数据

- 路径比较、Windows 保留文件名、路径斜杠、Unix 权限位、batch 参数转义这类平台规则放在 [`lithe-platform`](rust/crates/platform),以 `Platform { Unix, Windows }` 参数表达。生产代码用 `Platform::HOST` 取本机规则。
- `cfg` 只用在必须调用操作系统接口的地方,例如 Windows job object、Unix 进程组、权限位系统调用。
- 规则写成数据后,任何主机都能分别传入 `Platform::Unix` 和 `Platform::Windows` 测试两套规则,不需要真的有那台系统。

## 平台测试放在哪里

按下面的顺序判断,命中第一条就停:

1. **能写成 `Platform` 参数的规则**:在规则所在的 crate 里对两个平台各测一次,不加 `cfg`。这类测试在所有主机上都运行。
2. **结果取决于真实操作系统的行为**(进程树、job object、权限位、batch 启动、可执行文件查找等):放进 [`rust/crates/platform-tests/tests/platform/`](rust/crates/platform-tests/tests/platform) 里对应主题的模块。需要被启动的程序时,给 `lithe-platform-fixture` 增加一种模式。不要把这类测试写在被测 crate 里,也不要在其他 crate 里加 `#[cfg(windows)]`、`#[cfg(target_os = "macos")]` 之类的测试。
3. **只借用 Unix 才有的夹具**(符号链接、权限位)来验证本 crate 自己的逻辑,并且 Linux 上会运行的 `#[cfg(unix)]` 测试:可以留在本 crate。如果 Windows 上应有另一种结果,那个结果按第 2 条写进 `lithe-platform-tests`。
4. **测试里的 `cfg!`** 只能把夹具改成本机格式,例如 Windows 绝对路径要带盘符;不能用它为不同系统选择不同的预期结果。预期随系统变化的断言属于第 2 条。
5. **例外**:crate 的集成测试必须启动本包的可执行文件时,因为 `CARGO_BIN_EXE_*` 只对同一个包可见,测试无法搬走。这种 crate 在自己的 `Cargo.toml` 声明主机。目前只有 [`lithe-remote`](rust/crates/remote):它的服务端运行在 Linux 和 macOS 主机上。新增例外要在 PR 里说明为什么不能放进 `lithe-platform-tests`。

[`scripts/rust-core/verify-platform-tests.mjs`](scripts/rust-core/verify-platform-tests.mjs) 检查的是:有没有测试在所属 crate 的每台 CI 主机上都被 `cfg` 编译掉,或者被 `cfg_attr(…, ignore)` 忽略。这样的测试在 CI 里永远不会运行,检查会失败。CI 的 `Select crates` 作业每次都运行这项检查。加 `--list` 会列出所有只在部分主机上运行的测试。第 3、4 条要看测试的意图,脚本判断不了,由评审把关。

## 声明 CI 主机

```toml
[package.metadata.lithe-ci]
hosts = ["macos", "windows"] # 除 Linux 外,还要在这些主机上运行测试
test-features = ["server"] # 测试需要的 feature,在每台主机上都启用
```

`hosts` 只接受 `macos` 和 `windows`;每个 crate 都会在 Linux 上测试,不必声明。目前声明主机的 crate 有 `lithe-platform`、`lithe-platform-tests` 和 `lithe-remote`。声明主机后,每个影响到该 crate 的 PR 都会多占用 macOS 或 Windows runner,所以只能用于上一节允许的情况。

## CI 怎样只运行受影响的 crate

[`.github/workflows/rust-core.yml`](.github/workflows/rust-core.yml) 由四个作业组成:

- **Select crates**:先运行 CI 脚本的测试和上面的放置检查,再由 [`affected-crates.mjs`](scripts/rust-core/affected-crates.mjs) 根据 PR 改动的文件选出 crate。选中的是文件所在的 crate,加上所有直接或间接依赖它的 crate(各种依赖都算,包括 dev-dependency)。另外几类文件的处理是:
- 会选中全部 crate:`rust/` 下不属于某个 crate 的文件(workspace 清单、`Cargo.lock`、`fixtures/`、`contracts/`、`logging/`)、`shared/plugins/contract/`、`scripts/rust-core/`、`scripts/logging/`,以及工作流文件本身。
- `shared/plugins/sdk/e2e/` 只选中 `lithe-core`。
- 文档、SDK 等其他文件不选中任何 crate。
- **Linux**:对选中的 crate 依次运行格式检查(全部 crate,耗时很短)、日志事件目录检查、注释规范(文本检查覆盖全部 crate,Rustdoc 只构建选中的 crate)、启用全部 feature 的 clippy、针对 `x86_64-pc-windows-msvc` 的 clippy(只用默认 feature),最后是启用 `test-features` 的测试。
- **macOS / Windows**:只测试选中、并且声明了对应主机的 crate。Draft PR 不运行这一步;PR 标记为 ready 并且 Linux 通过之后才运行。
- **Rust Core gate**:汇总上面三个作业。作业被跳过算通过,其余结果都算失败。工作流不设路径过滤,所以每个 PR 都会报告这个检查,分支规则可以把它设为必过。

手动运行工作流(workflow_dispatch)会检查全部 crate。CI 只缓存下载的依赖,不缓存构建产物。

## 本地命令

```sh
node scripts/rust-core/affected-crates.mjs # 相对 origin/main 的改动,CI 会运行哪些 crate
node scripts/rust-core/verify-platform-tests.mjs --list # 放置检查,并列出只在部分主机运行的测试
node --test scripts/rust-core/*.test.mjs # CI 脚本自己的测试
cargo test --manifest-path rust/Cargo.toml -p lithe-platform -p lithe-platform-tests # 在 macOS 或 Windows 上运行平台测试
```

手边没有 Windows 时,至少运行一次 `cargo clippy --manifest-path rust/Cargo.toml --workspace --all-targets --target x86_64-pc-windows-msvc -- -D warnings`,确认 Windows 代码能编译。

---

# Contributing to Rust Core (English)

This page sets how platform code is written, where platform tests go, and how CI runs only the crates a change affects. The plugin contract and SDK rules live in [`shared/plugins/README.md`](shared/plugins/README.md).

## Platform differences are data

- Platform rules, such as path comparison, reserved Windows file names, path separators, Unix permission bits and batch argument quoting, live in [`lithe-platform`](rust/crates/platform) and take a `Platform { Unix, Windows }` argument. Production code passes `Platform::HOST`.
- `cfg` is only for calls into the operating system, such as Windows job objects, Unix process groups and permission system calls.
- Because the rules are data, any host tests both sets by passing `Platform::Unix` and `Platform::Windows`.

## Where platform tests go

Take the first rule that applies:

1. **A rule that takes a `Platform`**: test it for both platforms in the crate that owns it, without `cfg`. Every host runs these tests.
2. **A result that depends on the real operating system** (process trees, job objects, permission bits, batch launches, executable lookup and the like): add it to the module for that subject under [`rust/crates/platform-tests/tests/platform/`](rust/crates/platform-tests/tests/platform). When a test needs a program to start, add a mode to `lithe-platform-fixture`. Do not write such tests in the crate under test, and do not add `#[cfg(windows)]`, `#[cfg(target_os = "macos")]` or similar tests to any other crate.
3. **A `#[cfg(unix)]` test that only borrows a Unix fixture** (a symbolic link, permission bits) to check the crate's own logic, and that runs on Linux: it may stay in its crate. If Windows should behave differently, that result goes into `lithe-platform-tests` under rule 2.
4. **`cfg!` in a test** may only convert a fixture to the host's form, for example give a Windows absolute path its drive. It must not choose a different expected result per system; such assertions fall under rule 2.
5. **Exception**: when a crate's integration tests must start the crate's own executable, they cannot move, because `CARGO_BIN_EXE_*` is visible only within the package. Such a crate declares its hosts in its own `Cargo.toml`. Today only [`lithe-remote`](rust/crates/remote) does, because its server runs on Linux and macOS hosts. A new exception explains in its pull request why the tests cannot go into `lithe-platform-tests`.

[`scripts/rust-core/verify-platform-tests.mjs`](scripts/rust-core/verify-platform-tests.mjs) fails when a test is compiled out by `cfg`, or ignored by `cfg_attr(…, ignore)`, on every CI host of its crate, because CI would never run it. The `Select crates` job runs it on every change. `--list` also prints every test that runs on only some hosts. Rules 3 and 4 are about a test's intent, which the script cannot judge; review enforces them.

## Declaring CI hosts

```toml
[package.metadata.lithe-ci]
hosts = ["macos", "windows"] # hosts that also run the crate's tests, besides Linux
test-features = ["server"] # features the tests need, enabled on every host
```

`hosts` accepts `macos` and `windows`; every crate is tested on Linux without declaring it. Today `lithe-platform`, `lithe-platform-tests` and `lithe-remote` declare hosts. A declaration makes every pull request that affects the crate use a macOS or Windows runner, so it is only for the cases the previous section allows.

## How CI runs only the affected crates

[`.github/workflows/rust-core.yml`](.github/workflows/rust-core.yml) has four jobs:

- **Select crates** tests the CI scripts, runs the placement check above, and lets [`affected-crates.mjs`](scripts/rust-core/affected-crates.mjs) choose crates from the files the pull request changes: the crate each file belongs to, plus every crate that depends on it directly or indirectly, through any kind of dependency, dev-dependencies included. Other files are handled as follows:
- These select every crate: files under `rust/` outside any crate (the workspace manifest, `Cargo.lock`, `fixtures/`, `contracts/`, `logging/`), `shared/plugins/contract/`, `scripts/rust-core/`, `scripts/logging/` and the workflow itself.
- `shared/plugins/sdk/e2e/` selects `lithe-core` only.
- Documentation, the SDKs and other files select no crate.
- **Linux** runs, for the selected crates: the formatting check (every crate; it is quick), the log event catalog check, the comment rules (the textual checks cover every crate, Rustdoc builds the selected ones), clippy with every feature, clippy for `x86_64-pc-windows-msvc` with default features, and the tests with their `test-features`.
- **macOS / Windows** test the selected crates that declare that host. Draft pull requests skip them; they run once the pull request is ready for review and Linux passed.
- **Rust Core gate** sums up the three jobs above. A skipped job passes; any other result fails. The workflow has no path filter, so every pull request reports this check and branch rules can require it.

A manual run (workflow_dispatch) checks every crate. CI caches downloaded dependencies only, not build output.

## Local commands

```sh
node scripts/rust-core/affected-crates.mjs # which crates CI runs for the changes since origin/main
node scripts/rust-core/verify-platform-tests.mjs --list # the placement check, listing tests that run on some hosts only
node --test scripts/rust-core/*.test.mjs # the CI scripts' own tests
cargo test --manifest-path rust/Cargo.toml -p lithe-platform -p lithe-platform-tests # platform tests on macOS or Windows
```

Without a Windows host, at least run `cargo clippy --manifest-path rust/Cargo.toml --workspace --all-targets --target x86_64-pc-windows-msvc -- -D warnings` to confirm the Windows code compiles.
Loading
Loading