agy-cli 0.1.38: a runtime under a non-ASCII folder still builds nc.exe (#1097)
On Windows, Bun's `build --compile` widens its own path byte by byte into UTF-16 before CopyFileW, so a magpie-bun.exe under a non-ASCII folder (a Chinese user name) fails with "failed to copy bun executable into temporary file: ENOENT". windowsNcDir then fell back to nc.cmd, which agy 1.5.x never runs, and no client tool reached the model. When process.execPath isn't ASCII, the build now runs a copy of the runtime in an ASCII-only folder (the temp dir, else %PUBLIC%, else %ProgramData%) and removes it after. nc.exe's cache key is still the real runtime's path. Run (mote, temp HOME, Bun 1.4.2): node --test test/windows-nc.mjs 7 pass; bun test in packages/agy-cli 20 pass; scripts/check.mjs agy-cli ok. Broken by hand: with main's index.mjs the new test fails. Windows 11 (xiaomi-gpu, Bun 1.4.2, real binaries): bun under a folder named with Chinese characters fails `build --compile` with the reporter's ENOENT. A non-ASCII cwd, server path or output path is fine, and the compiled exe runs from a non-ASCII folder; an 8.3 short path doesn't help. The plugin's own ensureShimDir, run by that bun: main gives nc.cmd, this change gives nc.exe in 614ms, which starts, and leaves no build dir or runtime copy behind. Not tried: agy itself against the new nc.exe on Windows, and Bun 1.4.4 (the reporter's); the code path read is the same in Bun's main source. Reported-by: Hy1an
This commit is contained in:
@@ -872,6 +872,10 @@ const WINDOWS_NC_PREFIX = "magpie-agy-bridge-nc-"
|
||||
const WINDOWS_NC_BUILD_PREFIX = "magpie-agy-bridge-ncbuild-"
|
||||
const WINDOWS_NC_BUILD_TIMEOUT_MS = 120_000
|
||||
|
||||
function asciiPath(value) {
|
||||
return /^[\x20-\x7e]*$/.test(String(value || ""))
|
||||
}
|
||||
|
||||
async function isFileWithBytes(file) {
|
||||
try {
|
||||
const stat = await fs.promises.stat(file)
|
||||
@@ -895,8 +899,24 @@ async function windowsNcDir() {
|
||||
return dir
|
||||
}
|
||||
const build = await fs.promises.mkdtemp(path.join(tmp, WINDOWS_NC_BUILD_PREFIX))
|
||||
let copy = null
|
||||
let copyBase = null
|
||||
try {
|
||||
const spec = windowsNcBuild(process.execPath, MCP_SERVER_PATH, path.join(build, "nc.exe"))
|
||||
let runtime = process.execPath
|
||||
if (!asciiPath(runtime)) {
|
||||
// Bun's compile reads its own path one byte to one UTF-16 unit, so a
|
||||
// runtime under a non-ASCII folder (a Chinese user name) fails with
|
||||
// "failed to copy bun executable into temporary file: ENOENT" (#1097).
|
||||
// A copy of it in an ASCII-only folder builds the same nc.exe.
|
||||
const base = [tmp, process.env.PUBLIC, process.env.ProgramData]
|
||||
.find(dir => dir && asciiPath(dir))
|
||||
if (!base) return null
|
||||
copyBase = base
|
||||
copy = base === tmp ? build : await fs.promises.mkdtemp(path.join(base, WINDOWS_NC_BUILD_PREFIX))
|
||||
runtime = path.join(copy, path.win32.basename(process.execPath))
|
||||
await fs.promises.copyFile(process.execPath, runtime)
|
||||
}
|
||||
const spec = windowsNcBuild(runtime, MCP_SERVER_PATH, path.join(build, "nc.exe"))
|
||||
const { err, code, timedOut } = await execOnce(spec.file, spec.args, { cwd: build, timeoutMs: WINDOWS_NC_BUILD_TIMEOUT_MS })
|
||||
if (err || timedOut || code !== 0 || !await isFileWithBytes(path.join(build, "nc.exe"))) return null
|
||||
await fs.promises.mkdir(dir, { recursive: true })
|
||||
@@ -908,8 +928,10 @@ async function windowsNcDir() {
|
||||
}
|
||||
} finally {
|
||||
fs.promises.rm(build, { recursive: true, force: true }).catch(() => {})
|
||||
if (copy && copy !== build) fs.promises.rm(copy, { recursive: true, force: true }).catch(() => {})
|
||||
}
|
||||
sweepWindowsNcDirs(tmp, path.basename(dir)).catch(() => {})
|
||||
if (copyBase && copyBase !== tmp) sweepWindowsNcDirs(copyBase, path.basename(dir)).catch(() => {})
|
||||
return dir
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-agy-cli-auth",
|
||||
"version": "0.1.37",
|
||||
"version": "0.1.38",
|
||||
"description": "Magpie/OpenCode model provider that connects your local agy CLI through the Agent Client Protocol (agy acp), so Antigravity subscription models run through the real CLI",
|
||||
"type": "module",
|
||||
"main": "./opencode.mjs",
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
// exit every agy is ended with its tree, and its workspace is removed.
|
||||
import assert from "node:assert/strict"
|
||||
import fs from "node:fs"
|
||||
import os from "node:os"
|
||||
import path from "node:path"
|
||||
import { test } from "node:test"
|
||||
import { windowsNcBuild, windowsTreeKill } from "../platform.mjs"
|
||||
@@ -102,6 +103,45 @@ test("Windows sessions dial a shared nc.exe built once, never nc.cmd", async ()
|
||||
}
|
||||
})
|
||||
|
||||
// #1097: Bun's compile can't read its own path when it holds non-ASCII
|
||||
// characters (a Chinese user name): it fails with "failed to copy bun
|
||||
// executable into temporary file: ENOENT". The build runs a copy of the
|
||||
// runtime in an ASCII-only folder, removed after.
|
||||
test("a runtime under a non-ASCII folder builds nc.exe from an ASCII copy", async () => {
|
||||
const outside = fs.mkdtempSync(path.join(os.tmpdir(), "agy-nonascii-"))
|
||||
const runtimeDir = path.join(outside, "\u6d4b\u8bd5")
|
||||
fs.mkdirSync(runtimeDir)
|
||||
const runtime = path.join(runtimeDir, "magpie-bun.exe")
|
||||
fs.writeFileSync(runtime, "MZ fake bun")
|
||||
const seen = []
|
||||
const kit = await windowsKit({
|
||||
execPath: runtime,
|
||||
onSpawn: (file, args) => {
|
||||
if (args[0] === "build") seen.push({ file, copied: fs.existsSync(file) && fs.readFileSync(file, "utf8") })
|
||||
return writeOutfile(file, args)
|
||||
},
|
||||
})
|
||||
try {
|
||||
await turn(kit, "hi")
|
||||
const builds = buildsOf(kit)
|
||||
assert.equal(builds.length, 1)
|
||||
assert.notEqual(builds[0].file, runtime)
|
||||
assert.match(builds[0].file, /^[\x20-\x7e]+$/)
|
||||
assert.equal(path.basename(builds[0].file), "magpie-bun.exe")
|
||||
assert.deepEqual(seen, [{ file: builds[0].file, copied: "MZ fake bun" }])
|
||||
const dir = agyPathDir(kit)
|
||||
assert.match(path.basename(dir), /^magpie-agy-bridge-nc-[0-9a-f]{16}$/)
|
||||
assert.deepEqual(fs.readdirSync(dir), ["nc.exe"])
|
||||
// The copy goes with the build dir.
|
||||
for (let i = 0; i < 1000 && fs.existsSync(builds[0].file); i++) await new Promise(resolve => setImmediate(resolve))
|
||||
assert.equal(fs.existsSync(builds[0].file), false)
|
||||
assert.ok(!fs.readdirSync(kit.tmp).some((name) => name.startsWith("magpie-agy-bridge-ncbuild-")))
|
||||
} finally {
|
||||
await kit.dispose()
|
||||
fs.rmSync(outside, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
test("Windows falls back to nc.cmd when the runtime can't build nc.exe", async () => {
|
||||
for (const options of [{ execPath: "C:\\opencode\\opencode.exe", versions: { ...process.versions, bun: undefined } }, { onSpawn: () => 1 }]) {
|
||||
const kit = await windowsKit(options)
|
||||
|
||||
Reference in New Issue
Block a user