fix: resolve bun/pnpm global install failures on Windows (#4275)

Co-authored-by: Aiden Cline <aidenpcline@gmail.com>
This commit is contained in:
Luke Parker
2025-11-14 02:38:57 +10:00
committed by GitHub
parent 609ab069a9
commit 73443585e5
3 changed files with 36 additions and 74 deletions

View File

@@ -85,18 +85,6 @@ function prepareBinDirectory(binaryName) {
return { binDir, targetPath }
}
function copyBinary(sourcePath, binaryName) {
const { targetPath } = prepareBinDirectory(binaryName)
fs.copyFileSync(sourcePath, targetPath)
console.log(`opencode binary installed: ${targetPath}`)
// Verify the file exists after operation
if (!fs.existsSync(targetPath)) {
throw new Error(`Failed to copy binary to ${targetPath}`)
}
}
function symlinkBinary(sourcePath, binaryName) {
const { targetPath } = prepareBinDirectory(binaryName)
@@ -109,64 +97,12 @@ function symlinkBinary(sourcePath, binaryName) {
}
}
async function regenerateWindowsCmdWrappers() {
console.log("Windows + npm detected: Forcing npm to rebuild bin links")
try {
const { execSync } = require("child_process")
const pkgPath = path.join(__dirname, "..")
// npm_config_global is string | undefined
// if it exists, the value is true
const isGlobal = process.env.npm_config_global === "true" || pkgPath.includes(path.join("npm", "node_modules"))
// The npm rebuild command does 2 things - Execute lifecycle scripts and rebuild bin links
// We want to skip lifecycle scripts to avoid infinite loops, so we use --ignore-scripts
const cmd = `npm rebuild opencode-ai --ignore-scripts${isGlobal ? " -g" : ""}`
const opts = {
stdio: "inherit",
shell: true,
...(isGlobal ? {} : { cwd: path.join(pkgPath, "..", "..") }), // For local, run from project root
}
console.log(`Running: ${cmd}`)
execSync(cmd, opts)
console.log("Successfully rebuilt npm bin links")
} catch (error) {
console.error("Error rebuilding npm links:", error.message)
console.error("npm rebuild failed. You may need to manually run: npm rebuild opencode-ai --ignore-scripts")
}
}
async function main() {
try {
if (os.platform() === "win32") {
// NPM eg format - npm/11.4.2 node/v24.4.1 win32 x64
// Bun eg format - bun/1.2.19 npm/? node/v24.3.0 win32 x64
// pnpm eg format - pnpm/8.10.0 npm/? node/v20.10.0 win32 x64
const userAgent = process.env.npm_config_user_agent || ""
if (userAgent.startsWith("npm")) {
await regenerateWindowsCmdWrappers()
return
}
if (userAgent.startsWith("bun")) {
console.log("Windows + bun detected: Setting up binary")
const { binaryPath, binaryName } = findBinary()
copyBinary(binaryPath, binaryName)
return
}
if (userAgent.startsWith("pnpm")) {
console.log("Windows + pnpm detected: Setting up binary")
const { binaryPath, binaryName } = findBinary()
copyBinary(binaryPath, binaryName)
return
}
// Unknown package manager on Windows
console.log("Windows detected but unknown package manager, skipping postinstall")
// On Windows, the .exe is already included in the package and bin field points to it
// No postinstall setup needed
console.log("Windows detected: binary setup not needed (using packaged .exe)")
return
}