Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Translate local Go workspace paths for HarmonyOS deployment

Daniel2d1ed17af5c2
diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md
index 24a1634959..04c5a3d9a3 100644
--- a/.github/CONTRIBUTING.md
+++ b/.github/CONTRIBUTING.md
@@ -101,6 +101,8 @@ For ARM64 devices, run `node scripts/harmony-dev-run.mjs` from the SiYuan reposi
Use `--deveco=<path>`, `--harmony-dir=<path>`, and `--device=<connect-key>` to select tools, project, and device. `--skip-ui` reuses existing frontend output (mobile, desktop, and export are required), `--skip-kernel` copies existing `kernel/harmony/libkernel.so` and `libkernel.h`, and `--skip-hvigor` only reinstalls and launches the existing debug HAP, skipping all builds and resource copies. See `node scripts/harmony-dev-run.mjs --help` for more options. Run script tests with `node --test app/scripts/harmony-dev-run.test.js`.
+On Windows, the deployment script translates active `go.work` module paths and local replacements into WSL paths in a temporary workspace. Local dependency overrides remain active; the original workspace files are not changed. If workspace mode is disabled, the WSL build also uses `GOWORK=off`.
+
Modify Go source code:
1. go/src/runtime/vim tls_arm64.s
diff --git a/.github/CONTRIBUTING.zh-CN.md b/.github/CONTRIBUTING.zh-CN.md
index faa0bd1d92..dd8ccfc50c 100644
--- a/.github/CONTRIBUTING.zh-CN.md
+++ b/.github/CONTRIBUTING.zh-CN.md
@@ -101,6 +101,8 @@ ARM64 真机可在思源仓库根目录执行 `node scripts/harmony-dev-run.mjs`
通过 `--deveco=<路径>`、`--harmony-dir=<路径>`、`--device=<连接标识>` 指定工具、工程和设备。`--skip-ui` 复用现有前端产物(需包含 mobile、desktop、export),`--skip-kernel` 复制现有 `kernel/harmony/libkernel.so` 和 `libkernel.h`,`--skip-hvigor` 则仅重新安装并启动已有的调试 HAP,跳过所有构建及资源复制。更多参数见 `node scripts/harmony-dev-run.mjs --help`。运行脚本测试:`node --test app/scripts/harmony-dev-run.test.js`。
+Windows 下,部署脚本会在临时工作区中将当前 `go.work` 的模块路径和本地替换路径转换为 WSL 路径,保留本地依赖覆盖,不修改原始工作区文件。如果已禁用工作区模式,WSL 构建同样使用 `GOWORK=off`。
+
修改 Go 源码:
1. go/src/runtime/tls_arm64.s
diff --git a/app/scripts/harmony-dev-run.test.js b/app/scripts/harmony-dev-run.test.js
index d269b66ccb..8e243c43f8 100644
--- a/app/scripts/harmony-dev-run.test.js
+++ b/app/scripts/harmony-dev-run.test.js
@@ -87,8 +87,22 @@ test("Harmony stops on missing signed HAP, tool errors and zero-exit installatio
"install bundle successfully.\n" : "error: failed to start ability\n"}), root), /ability startup/);
});
-for (const reuse of [true, false]) {
-test(`Harmony build packages resources and builds signed debug HAP (reuse=${reuse})`, async t => {
+test("Harmony workspace translation preserves local modules, replacements and toolchain directives", async () => {
+ const {translateWorkspace} = await import("../../scripts/harmony-dev-run.mjs");
+ const directory = path.resolve("workspace");
+ const result = translateWorkspace({Go: "1.26.10", Toolchain: "go1.26.10", Godebug: [{Key: "default", Value: "go1.26"}],
+ Use: [{DiskPath: "./kernel"}, {DiskPath: path.resolve("local lute")}], Replace: [
+ {Old: {Path: "example.org/a", Version: "v1.0.0"}, New: {Path: "../local-a"}},
+ {Old: {Path: "example.org/b"}, New: {Path: "example.org/c", Version: "v1.2.0"}},
+ ]}, directory, value => "/linux/" + path.basename(value));
+ assert.equal(result, "go 1.26.10\ntoolchain go1.26.10\ngodebug default=go1.26\n" +
+ "use \"/linux/kernel\"\nuse \"/linux/local lute\"\n" +
+ "replace example.org/a v1.0.0 => \"/linux/local-a\"\nreplace example.org/b => example.org/c v1.2.0\n");
+});
+
+for (const {reuse, workspace, failKernel} of [{reuse: true, workspace: false}, {reuse: false, workspace: false},
+ {reuse: false, workspace: true}, {reuse: false, workspace: true, failKernel: true}]) {
+test(`Harmony build handles resources and workspace (reuse=${reuse}, workspace=${workspace}, failure=${!!failKernel})`, async t => {
const {main} = await import("../../scripts/harmony-dev-run.mjs");
const {root, native, write, hap, args} = fixture(t);
const deveco = path.join(root, "DevEco Studio");
@@ -111,18 +125,38 @@ test(`Harmony build packages resources and builds signed debug HAP (reuse=${reus
write("kernel/harmony/libkernel.so", header);
write("kernel/harmony/libkernel.h", "kernel header");
write(hap, "stale HAP");
+ const originalWork = write("go.work", "original workspace");
+ write("go.work.sum", "original checksums");
+ const translated = new Map();
+ let temporaryWork;
let list;
const calls = [];
- main([...args, `--deveco=${deveco}`, ...(reuse ? ["--skip-ui", "--skip-kernel"] : [])], (command, argv, settings) => {
+ const build = () => main([...args, `--deveco=${deveco}`, ...(reuse ? ["--skip-ui", "--skip-kernel"] : [])], (command, argv, settings) => {
calls.push([command, argv]);
+ assert.ok(!settings.shell);
if (argv[0] === "list") return {status: 0, stdout: "phone\n"};
+ if (command === "go") {
+ return {status: 0, stdout: argv[0] === "env" ? (workspace ? originalWork : "off") :
+ JSON.stringify({Go: "1.26.10", Use: [{DiskPath: "./kernel"}, {DiskPath: path.join(root, "local-lute")} ]})};
+ }
if (argv.includes("wslpath")) {
assert.deepEqual(settings.stdio, ["inherit", "pipe", "inherit"]);
- return {status: 0, stdout: "/mnt/c/native project/kernel/harmony\n"};
+ const value = "/linux/" + path.basename(argv.at(-1));
+ translated.set(value, argv.at(-1));
+ return {status: 0, stdout: value + "\n"};
}
if (command === "wsl.exe" && argv.includes("--cd")) {
- assert.equal(argv[argv.indexOf("--cd") + 1], "/mnt/c/native project/kernel/harmony");
+ assert.equal(argv[argv.indexOf("--cd") + 1], "/linux/harmony");
assert.ok(argv.includes("pipefail"));
+ const gowork = argv.find(value => value.startsWith("GOWORK="));
+ if (workspace) {
+ temporaryWork = translated.get(gowork.slice("GOWORK=".length));
+ assert.match(readFileSync(temporaryWork, "utf8"), /use "\/linux\/local-lute"/);
+ assert.equal(readFileSync(temporaryWork + ".sum", "utf8"), "original checksums");
+ } else {
+ assert.equal(gowork, "GOWORK=off");
+ }
+ if (failKernel) return {status: 1};
}
if (command === "tar") {
list = argv[argv.indexOf("-T") + 1];
@@ -143,12 +177,22 @@ test(`Harmony build packages resources and builds signed debug HAP (reuse=${reus
return {status: 0, stdout: argv.includes("install") && command === "test-hdc" ?
"install bundle successfully.\n" : argv.includes("start") ? "start ability successfully.\n" : ""};
}, root);
+ if (failKernel) {
+ assert.throws(build, /wsl.exe failed/);
+ assert.equal(existsSync(temporaryWork), false);
+ assert.equal(readFileSync(originalWork, "utf8"), "original workspace");
+ assert.ok(!calls.some(([, argv]) => argv.includes("assembleHap")));
+ return;
+ }
+ build();
assert.equal(existsSync(list), false);
assert.deepEqual(readFileSync(path.join(native, "entry/libs/arm64-v8a/libkernel.so")), header);
assert.equal(readFileSync(path.join(native, "entry/src/main/cpp/include/libkernel.h"), "utf8"), "kernel header");
assert.ok(calls.some(([, argv]) => argv.includes("clean")));
assert.ok(calls.some(([, argv]) => argv[0].endsWith("pm-cli.js")));
- assert.equal(calls.some(([command]) => command === "pnpm"), !reuse);
+ assert.equal(readFileSync(originalWork, "utf8"), "original workspace");
+ if (temporaryWork) assert.equal(existsSync(temporaryWork), false);
+ assert.equal(calls.some(([command, argv]) => command === "pnpm" || argv.includes("pnpm run build:mobile")), !reuse);
assert.equal(calls.some(([command]) => command === "wsl.exe" || command === "bash"), !reuse);
});
}
diff --git a/scripts/harmony-dev-run.mjs b/scripts/harmony-dev-run.mjs
index 21f03bd82a..8287625621 100644
--- a/scripts/harmony-dev-run.mjs
+++ b/scripts/harmony-dev-run.mjs
@@ -70,6 +70,20 @@ export function checkKernel(file) {
}
}
+export function translateWorkspace(workspace, directory, translate) {
+ const localPath = value => translate(path.resolve(directory, value));
+ const lines = [`go ${workspace.Go}`];
+ if (workspace.Toolchain) lines.push(`toolchain ${workspace.Toolchain}`);
+ for (const item of workspace.Godebug || []) lines.push(`godebug ${item.Key}=${item.Value}`);
+ for (const item of workspace.Use || []) lines.push(`use ${JSON.stringify(localPath(item.DiskPath))}`);
+ for (const item of workspace.Replace || []) {
+ const before = item.Old.Path + (item.Old.Version ? ` ${item.Old.Version}` : "");
+ const after = item.New.Version ? `${item.New.Path} ${item.New.Version}` : JSON.stringify(localPath(item.New.Path));
+ lines.push(`replace ${before} => ${after}`);
+ }
+ return lines.join("\n") + "\n";
+}
+
export function main(args = process.argv.slice(2), execute = spawnSync, root = repoRoot) {
const options = parseArgs(args);
if (options.help) {
@@ -89,6 +103,7 @@ Builds product=debug, buildMode=debug for ARM64 devices. Configure the debug
signing profile in DevEco Studio for your device first. Uses build.sh and its
Linux SDK / patched Go toolchain (see .github/CONTRIBUTING.md); Windows uses WSL
against this checkout. Existing OCR native libraries and models are required.
+Windows go.work paths are translated in a temporary workspace, preserving local modules.
No uninstall is attempted if installation fails.`);
return;
}
@@ -120,7 +135,11 @@ No uninstall is attempted if installation fails.`);
env.PATH = path.dirname(node) + path.delimiter + (env.PATH || "");
if (!options.skipUi) {
// 使用现有开发资源目录,不运行会清空整个 stage 的总构建命令。
- run("pnpm", ["run", "build:mobile"], {cwd: app, shell: process.platform === "win32", stdio: "inherit"});
+ if (process.platform === "win32") {
+ run(process.env.ComSpec || "cmd.exe", ["/d", "/s", "/c", "pnpm run build:mobile"], {cwd: app, stdio: "inherit"});
+ } else {
+ run("pnpm", ["run", "build:mobile"], {cwd: app, stdio: "inherit"});
+ }
}
const stageFiles = androidStageFiles(path.join(app, "stage"))
.filter(name => !name.startsWith("stage/ocr/runtime/"));
@@ -146,12 +165,27 @@ No uninstall is attempted if installation fails.`);
if (options.wslDistro) wsl.push("--distribution", options.wslDistro);
if (options.wslUser) wsl.push("--user", options.wslUser);
// WSL 的 Windows 侧诊断使用不同于 Linux 输出的编码,直接交给终端显示。
- const linuxPath = run("wsl.exe", [...wsl, "--exec", "wslpath", "-a", "-u", kernel], {
+ const translate = value => run("wsl.exe", [...wsl, "--exec", "wslpath", "-a", "-u", value], {
stdio: ["inherit", "pipe", "inherit"],
}).trim();
- // 去掉检出脚本的 CRLF,不更改工作树中的文件。
- run("wsl.exe", [...wsl, "--cd", linuxPath, "--exec", "bash", "-e", "-o", "pipefail", "-c",
- "sed 's/\\r$//' build.sh | bash -e"], {stdio: "inherit"});
+ const linuxPath = translate(kernel);
+ const workspace = run("go", ["env", "GOWORK"], {cwd: kernel}).trim();
+ const workTemp = mkdtempSync(path.join(os.tmpdir(), "siyuan-harmony-work-"));
+ try {
+ let gowork = "off";
+ if (workspace && workspace !== "off") {
+ const config = JSON.parse(run("go", ["work", "edit", "-json", workspace], {cwd: kernel}));
+ const workFile = path.join(workTemp, "go.work");
+ writeFileSync(workFile, translateWorkspace(config, path.dirname(workspace), translate), "utf8");
+ if (existsSync(workspace + ".sum")) copyFileSync(workspace + ".sum", workFile + ".sum");
+ gowork = translate(workFile);
+ }
+ // 去掉检出脚本的 CRLF,使用临时工作区保留本地依赖,不更改原始工作区文件。
+ run("wsl.exe", [...wsl, "--cd", linuxPath, "--exec", "env", `GOWORK=${gowork}`,
+ "bash", "-e", "-o", "pipefail", "-c", "sed 's/\\r$//' build.sh | bash -e"], {stdio: "inherit"});
+ } finally {
+ removeTemp(workTemp);
+ }
} else {
run("bash", ["-e", "build.sh"], {cwd: kernel, stdio: "inherit"});
}