Skip to content

Conversation

@wangl-cc
Copy link
Member

@wangl-cc wangl-cc commented Jan 4, 2026

Summary by Sourcery

为 maa-cli 引入基于 xtask 的构建命令,并将其接入发布工作流,以统一构建和打包流程。

新功能:

  • 添加一个 xtask build 子命令,用于构建带可选 vendored 依赖和 tar 打包的 maa-cli 可执行文件。

增强改进:

  • 将构建和打包逻辑重构到独立的 xtask::build 模块中,由 CI 复用。
  • 将工作区根目录和主机三元组暴露为 xtask 命令共享的常量,并将相关逻辑从测试模块中移出。

CI:

  • 更新发布用的 GitHub Actions 工作流,使用 cargo x build 来构建和打包制品,并从 xtask 的输出中推导制品名称。
Original summary in English

Summary by Sourcery

Introduce an xtask-based build command for maa-cli and wire it into the release workflow to standardize building and packaging.

New Features:

  • Add an xtask build subcommand to build the maa-cli binary with optional vendored dependencies and tar packaging.

Enhancements:

  • Refactor build and packaging logic into a dedicated xtask::build module reused by CI.
  • Expose workspace root and host triplet as shared constants for xtask commands, moving this logic out of the test module.

CI:

  • Update the release GitHub Actions workflow to use cargo x build for building and packaging artifacts, and derive artifact names from xtask outputs.

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - 我发现了 1 个问题,并给出了一些高层次的反馈:

  • workspace_root 的实现通过对 CARGO_MANIFEST_DIR 使用硬编码长度的切片来获取工作空间根目录,这种方式比较脆弱;建议使用 Path::new(CARGO_MANIFEST_DIR).parent() 来推导工作空间根目录,从而避免对目录名做出假设。
  • xtask/build.rs 中,std::env::var("TARGET").unwrap() 会在 TARGET 未设置时触发 panic;使用带有更清晰错误信息的 expect 或者提供一个回退值,会让故障更容易诊断。
  • xtask/src/build.rs 中构造 target_dirbinary 路径时,建议使用 PathBuf.join() 而不是字符串拼接,以避免在不同平台上出现一些微妙的路径问题。
给 AI Agent 的提示
Please address the comments from this code review:

## Overall Comments
- `workspace_root` 的实现通过对 `CARGO_MANIFEST_DIR` 使用硬编码长度的切片来获取工作空间根目录,这种方式比较脆弱;建议使用 `Path::new(CARGO_MANIFEST_DIR).parent()` 来推导工作空间根目录,从而避免对目录名做出假设。
-`xtask/build.rs` 中,`std::env::var("TARGET").unwrap()` 会在 `TARGET` 未设置时触发 panic;使用带有更清晰错误信息的 `expect` 或者提供一个回退值,会让故障更容易诊断。
-`xtask/src/build.rs` 中构造 `target_dir``binary` 路径时,建议使用 `PathBuf``.join()` 而不是字符串拼接,以避免在不同平台上出现一些微妙的路径问题。

## Individual Comments

### Comment 1
<location> `xtask/src/main.rs:14-15` </location>
<code_context>
+const CARGO_MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR");
+const HOST_TRIPLET: &str = env!("TARGET");
+
+fn workspace_root() -> &'static str {
+    &CARGO_MANIFEST_DIR[..CARGO_MANIFEST_DIR.len() - 6]
+}
+
</code_context>

<issue_to_address>
**suggestion (bug_risk):** 避免通过硬编码的 `[..len-6]` 切片来推导工作空间根目录。

这种做法依赖目录名严格为 `/xtask`(包括分隔符在内共 6 个字节),因此一旦 crate 被重命名或移动就会出错。更好的方式是使用路径相关的 API,例如通过 `Path::new(CARGO_MANIFEST_DIR).parent().unwrap()`(如果需要的话可以再调用一次 `.parent()`)来以一种与布局和平台无关的方式推导工作空间根目录。

建议的实现:

```rust
mod build;
mod cmd;
mod env;
mod github;
mod release;
mod test;

use std::path::Path;

const CARGO_MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR");
const HOST_TRIPLET: &str = env!("TARGET");

```

```rust
fn workspace_root() -> &'static str {
    Path::new(CARGO_MANIFEST_DIR)
        .parent()
        .expect("CARGO_MANIFEST_DIR should have a parent directory")
        .to_str()
        .expect("workspace root path should be valid UTF-8")
}

```

如果 `workspace_root()` 在多个地方被调用时依赖于结尾的斜杠或某个特定子目录,请检查这些调用点,确保在采用更通用、基于路径的实现之后,它们仍然能按预期工作。另外,如果该文件当前存在 `mod` / `const` 重复声明(如示例所示),你可能还希望将其去重以保持整洁,不过这不是本次修改能够正常工作的必需条件。
</issue_to_address>

Sourcery 对开源项目是免费的——如果你觉得这些评审有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点击 👍 或 👎,我会根据你的反馈改进后续的评审。
Original comment in English

Hey - I've found 1 issue, and left some high level feedback:

  • The workspace_root implementation relies on slicing CARGO_MANIFEST_DIR by a hardcoded length, which is brittle; consider deriving the workspace root using Path::new(CARGO_MANIFEST_DIR).parent() to avoid assumptions about the directory name.
  • In xtask/build.rs, std::env::var("TARGET").unwrap() will panic if TARGET is not set; using expect with a clearer message or a fallback would make failures easier to diagnose.
  • When constructing target_dir and binary paths in xtask/src/build.rs, consider using PathBuf and .join() instead of string concatenation to avoid subtle path issues across platforms.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `workspace_root` implementation relies on slicing `CARGO_MANIFEST_DIR` by a hardcoded length, which is brittle; consider deriving the workspace root using `Path::new(CARGO_MANIFEST_DIR).parent()` to avoid assumptions about the directory name.
- In `xtask/build.rs`, `std::env::var("TARGET").unwrap()` will panic if `TARGET` is not set; using `expect` with a clearer message or a fallback would make failures easier to diagnose.
- When constructing `target_dir` and `binary` paths in `xtask/src/build.rs`, consider using `PathBuf` and `.join()` instead of string concatenation to avoid subtle path issues across platforms.

## Individual Comments

### Comment 1
<location> `xtask/src/main.rs:14-15` </location>
<code_context>
+const CARGO_MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR");
+const HOST_TRIPLET: &str = env!("TARGET");
+
+fn workspace_root() -> &'static str {
+    &CARGO_MANIFEST_DIR[..CARGO_MANIFEST_DIR.len() - 6]
+}
+
</code_context>

<issue_to_address>
**suggestion (bug_risk):** Avoid hard-coding the `[..len-6]` slice for deriving the workspace root.

This relies on the directory being exactly `/xtask` (6 bytes including the separator), so it will break if the crate is renamed or moved. Prefer using path APIs, e.g. `Path::new(CARGO_MANIFEST_DIR).parent().unwrap()` (and `.parent()` again if needed) to derive the workspace root in a layout- and platform-agnostic way.

Suggested implementation:

```rust
mod build;
mod cmd;
mod env;
mod github;
mod release;
mod test;

use std::path::Path;

const CARGO_MANIFEST_DIR: &str = env!("CARGO_MANIFEST_DIR");
const HOST_TRIPLET: &str = env!("TARGET");

```

```rust
fn workspace_root() -> &'static str {
    Path::new(CARGO_MANIFEST_DIR)
        .parent()
        .expect("CARGO_MANIFEST_DIR should have a parent directory")
        .to_str()
        .expect("workspace root path should be valid UTF-8")
}

```

If `workspace_root()` is used in multiple places assuming a trailing slash or specific subdirectory, review those call sites to ensure they still behave correctly with the more general, path-based implementation. Also, if this file currently has duplicated `mod` / `const` declarations (as in the snippet), you may want to deduplicate them for cleanliness, though that is not required for this change to work.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@codecov
Copy link

codecov bot commented Jan 4, 2026

Codecov Report

❌ Patch coverage is 33.33333% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.34%. Comparing base (b2a6107) to head (6f2ac2d).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
crates/maa-cli/src/config/init.rs 50.00% 0 Missing and 1 partial ⚠️
crates/maa-cli/src/main.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main     #469       +/-   ##
===========================================
+ Coverage   59.06%   69.34%   +10.28%     
===========================================
  Files          56       56               
  Lines        5560     5598       +38     
  Branches     5560     5598       +38     
===========================================
+ Hits         3284     3882      +598     
+ Misses       2018     1425      -593     
- Partials      258      291       +33     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

- Replace `--with-core` with `--install-core` and add `--no-core-tests` flag
- Remove redundant `MAA_EXTRA_SHARE_NAME` environment variable
- Simplify test command arguments in CI workflows
- Update `config init` to accept `&Path` instead of `PathBuf`
- Add debug logging to xtask command execution
@wangl-cc wangl-cc changed the title ci: add xtask for build ci: add xtask for build and better test Jan 4, 2026
@wangl-cc wangl-cc merged commit 32267cc into main Jan 4, 2026
29 of 30 checks passed
@wangl-cc wangl-cc deleted the ci/xtask-build branch January 4, 2026 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants