Improve upgrade command installation method detection robustness (#960)
* Improve upgrade command installation method detection robustness The upgrade command was failing to detect installation method for users who installed via pnpm, yarn, or bun, showing "Could not detect installation method" errors. ## Changes Made - **Added support for additional package managers**: Extended detection to include pnpm, yarn, and bun alongside existing npm, Homebrew, and Cargo support - **Implemented install-time marker system**: Modified `postinstall.js` to write a `.install-method` marker file during installation, providing reliable detection that doesn't depend on fragile path heuristics - **Enhanced path-based fallback detection**: Improved executable path analysis to better identify installation locations for all supported package managers - **Added command probing**: Implemented fallback checks that query package managers directly to verify global installations The detection now follows this robust hierarchy: 1. Read install-time marker file (most reliable) 2. Analyze executable path patterns 3. Probe package managers via subprocess calls This ensures users can successfully upgrade regardless of their chosen package manager. Fixes #954 * Add .install-method to .gitignore and note Yarn v2+ limitation - Prevent bin/.install-method marker file from being accidentally committed during development - Add comment clarifying that yarn global upgrade path only works with Yarn Classic (v1), not Yarn Berry (v2+) --------- Co-authored-by: ctate <366502+ctate@users.noreply.github.com>
This commit is contained in:
+33
-4
@@ -80,6 +80,31 @@ async function downloadFile(url, dest) {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect which package manager ran this postinstall and write a marker file
|
||||
* next to the binary so `agent-browser upgrade` can use the correct one
|
||||
* without fragile path heuristics or slow subprocess probing.
|
||||
*
|
||||
* npm_config_user_agent is set by npm/pnpm/yarn/bun during lifecycle scripts,
|
||||
* e.g. "pnpm/8.10.0 node/v20.10.0 linux x64"
|
||||
*/
|
||||
function writeInstallMethod() {
|
||||
const ua = process.env.npm_config_user_agent || '';
|
||||
let method = '';
|
||||
if (ua.startsWith('pnpm/')) method = 'pnpm';
|
||||
else if (ua.startsWith('yarn/')) method = 'yarn';
|
||||
else if (ua.startsWith('bun/')) method = 'bun';
|
||||
else if (ua.startsWith('npm/')) method = 'npm';
|
||||
|
||||
if (method) {
|
||||
try {
|
||||
writeFileSync(join(binDir, '.install-method'), method);
|
||||
} catch {
|
||||
// Non-critical — upgrade will fall back to heuristics
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
// Check if binary already exists
|
||||
if (existsSync(binaryPath)) {
|
||||
@@ -88,10 +113,12 @@ async function main() {
|
||||
chmodSync(binaryPath, 0o755);
|
||||
}
|
||||
console.log(`✓ Native binary ready: ${binaryName}`);
|
||||
|
||||
|
||||
writeInstallMethod();
|
||||
|
||||
// On global installs, fix npm's bin entry to use native binary directly
|
||||
await fixGlobalInstallBin();
|
||||
|
||||
|
||||
showInstallReminder();
|
||||
return;
|
||||
}
|
||||
@@ -106,12 +133,12 @@ async function main() {
|
||||
|
||||
try {
|
||||
await downloadFile(DOWNLOAD_URL, binaryPath);
|
||||
|
||||
|
||||
// Make executable on Unix
|
||||
if (platform() !== 'win32') {
|
||||
chmodSync(binaryPath, 0o755);
|
||||
}
|
||||
|
||||
|
||||
console.log(`✓ Downloaded native binary: ${binaryName}`);
|
||||
} catch (err) {
|
||||
console.log(`Could not download native binary: ${err.message}`);
|
||||
@@ -121,6 +148,8 @@ async function main() {
|
||||
console.log(' 2. Run: npm run build:native');
|
||||
}
|
||||
|
||||
writeInstallMethod();
|
||||
|
||||
// On global installs, fix npm's bin entry to use native binary directly
|
||||
// This avoids the /bin/sh error on Windows and provides zero-overhead execution
|
||||
await fixGlobalInstallBin();
|
||||
|
||||
Reference in New Issue
Block a user