diff --git a/install b/install index b17ccfde3b5..feb66da1f52 100755 --- a/install +++ b/install @@ -100,9 +100,17 @@ else fi fi + # Git Bash and MSYS2 run as emulated x64 on Windows ARM64, so `uname -m` reports x86_64. + # The host CPU still shows in the `uname -s` suffix and in PROCESSOR_IDENTIFIER. + if [ "$os" = "windows" ] && [ "$arch" = "x64" ]; then + if [[ "$raw_os" == *-ARM64 || "${PROCESSOR_IDENTIFIER:-}" == ARM* ]]; then + arch="arm64" + fi + fi + combo="$os-$arch" case "$combo" in - linux-x64|linux-arm64|darwin-x64|darwin-arm64|windows-x64) + linux-x64|linux-arm64|darwin-x64|darwin-arm64|windows-x64|windows-arm64) ;; *) echo -e "${RED}Unsupported OS/Arch: $os/$arch${NC}" diff --git a/packages/cli/script/postinstall.mjs b/packages/cli/script/postinstall.mjs index 551511eead5..9635660284b 100644 --- a/packages/cli/script/postinstall.mjs +++ b/packages/cli/script/postinstall.mjs @@ -15,7 +15,14 @@ if (!command) throw new Error("OpenCode package does not declare a binary") const sourceCommand = packageJson.opencodeSourceBinary ?? command const platform = { darwin: "darwin", linux: "linux", win32: "windows" }[os.platform()] ?? os.platform() -const arch = { x64: "x64", arm64: "arm64", arm: "arm" }[os.arch()] ?? os.arch() +const nodeArch = { x64: "x64", arm64: "arm64", arm: "arm" }[os.arch()] ?? os.arch() +// x64 Node runs emulated on Windows ARM64, but PROCESSOR_IDENTIFIER still names the host CPU. +const arch = + platform === "windows" && nodeArch === "x64" && process.env.PROCESSOR_IDENTIFIER?.startsWith("ARM") + ? "arm64" + : nodeArch +// Windows cannot spawn npm's .cmd shim directly, so reuse the npm CLI that runs this script. +const npm = process.env.npm_execpath?.endsWith("npm-cli.js") ? [process.execPath, process.env.npm_execpath] : ["npm"] const sourceBinary = platform === "windows" ? `${sourceCommand}.exe` : sourceCommand const targetBinary = path.resolve(directory, packageJson.bin[command]) const dependencies = packageJson.optionalDependencies ?? {} @@ -95,7 +102,10 @@ function packageNames() { ? [`${base}-baseline`, base] : [base, `${base}-baseline`] : [base] - return names.filter((name) => dependencies[name]) + // Windows 11 on ARM64 can still run the x64 binary under emulation when the native one fails. + const x64 = base.replace(/-arm64$/, "-x64") + const emulated = platform === "windows" && arch === "arm64" ? [`${x64}-baseline`, x64] : [] + return [...names, ...emulated].filter((name) => dependencies[name]) } function copyBinary(source) { @@ -127,12 +137,15 @@ function installPackage(name) { const temp = fs.mkdtempSync(path.join(os.tmpdir(), "opencode-install-")) try { const result = childProcess.spawnSync( - "npm", + npm[0], [ + ...npm.slice(1), "install", "--ignore-scripts", "--no-save", "--loglevel=error", + // npm rejects a package built for another CPU, such as arm64 under emulated x64 Node, unless forced. + ...(name.includes(`-${platform}-${nodeArch}`) ? [] : ["--force"]), "--prefix", temp, `${name}@${dependencies[name]}`,