Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
67ac233
Refactor macOS CLI installation to use ~/.local/bin
ivan-ottinger Mar 27, 2026
314afbd
Respect user preference when CLI is explicitly disabled
ivan-ottinger Mar 27, 2026
d03e18f
Fix test paths for cross-platform CI compatibility
ivan-ottinger Mar 27, 2026
8b7186a
Skip macOS-specific tests on Windows CI
ivan-ottinger Mar 27, 2026
20e9dae
Remove legacy symlink cleanup
ivan-ottinger Mar 27, 2026
9f5f263
Skip CLI auto-install in development mode
ivan-ottinger Mar 27, 2026
e78e351
Merge remote-tracking branch 'origin/trunk' into stu-1363-auto-instal…
ivan-ottinger Mar 27, 2026
270df7f
Remove stale legacy cleanup comment from test
ivan-ottinger Mar 27, 2026
bf34da1
Fix uninstall ENOENT and narrow test skip to Windows only
ivan-ottinger Mar 27, 2026
514943d
Remove redundant isCliInstalled check from autoInstallIfNeeded
ivan-ottinger Apr 1, 2026
5eb12ec
Check PATH in isCliInstalled and normalize PATH entries
ivan-ottinger Apr 1, 2026
816d532
Use process.env.SHELL for shell profile detection
ivan-ottinger Apr 1, 2026
1f5f619
Merge remote-tracking branch 'origin/trunk' into stu-1363-auto-instal…
ivan-ottinger Apr 1, 2026
54f7759
Merge remote-tracking branch 'origin/trunk' into stu-1363-auto-instal…
ivan-ottinger Apr 2, 2026
5a0bc63
Check profile file instead of runtime PATH for CLI status
ivan-ottinger Apr 2, 2026
ec95c5a
Merge remote-tracking branch 'origin/trunk' into stu-1363-auto-instal…
ivan-ottinger Apr 2, 2026
be0d28a
Remove stale PATH setup from test beforeEach
ivan-ottinger Apr 2, 2026
3f4fdb3
Simplify test mocks for readFile
ivan-ottinger Apr 2, 2026
74cb728
Merge remote-tracking branch 'origin/trunk' into stu-1363-auto-instal…
ivan-ottinger Apr 2, 2026
7e5f09e
Only treat ENOENT as empty profile, rethrow other errors
ivan-ottinger Apr 2, 2026
645ba1f
Only set cliAutoInstalled flag after verifying install succeeded
ivan-ottinger Apr 2, 2026
1ee857d
Revert isCliInstalled check after install on macOS
ivan-ottinger Apr 2, 2026
bf746d8
Merge branch 'trunk' into stu-1363-auto-install-cli-macos
fredrikekelund Apr 8, 2026
520af96
Tweaks
fredrikekelund Apr 8, 2026
681f640
Clean up legacy CLI symlink when uninstalling, if needed
fredrikekelund Apr 8, 2026
820334c
Simplification
fredrikekelund Apr 8, 2026
5fc4568
Move functions to methods
fredrikekelund Apr 8, 2026
9339dfa
`os.userInfo()` and shell basenames instead of paths
fredrikekelund Apr 8, 2026
1270007
Minor tweak
fredrikekelund Apr 8, 2026
fb6f9dc
Remove "CLI Installed" modal
fredrikekelund Apr 8, 2026
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions apps/studio/bin/install-studio-cli.sh

This file was deleted.

2 changes: 2 additions & 0 deletions apps/studio/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
stopCliEventsSubscriber,
} from 'src/modules/cli/lib/cli-events-subscriber';
import { isStudioCliInstalled } from 'src/modules/cli/lib/ipc-handlers';
import { autoInstallMacOSCliIfNeeded } from 'src/modules/cli/lib/macos-installation-manager';
import { updateWindowsCliVersionedPathIfNeeded } from 'src/modules/cli/lib/windows-installation-manager';
import { getRunningSiteCount, SiteServer, stopAllServers } from 'src/site-server';
import {
Expand Down Expand Up @@ -340,6 +341,7 @@ async function appBoot() {
).catch( ( err ) => Sentry.captureException( err ) );

await updateWindowsCliVersionedPathIfNeeded();
await autoInstallMacOSCliIfNeeded();

finishedInitialization = true;
} );
Expand Down
184 changes: 129 additions & 55 deletions apps/studio/src/modules/cli/lib/macos-installation-manager.ts
Comment thread
ivan-ottinger marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { dialog } from 'electron';
import { mkdir, readlink, symlink, unlink, lstat } from 'node:fs/promises';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import * as Sentry from '@sentry/electron/main';
import { isErrnoException } from '@studio/common/lib/is-errno-exception';
Expand All @@ -8,18 +9,26 @@ import { sudoExec } from 'src/lib/sudo-exec';
import { getMainWindow } from 'src/main-window';
import { StudioCliInstallationManager } from 'src/modules/cli/lib/ipc-handlers';
import { getResourcesPath } from 'src/storage/paths';
import { loadUserData, updateAppdata } from 'src/storage/user-data';
import packageJson from '../../../../package.json';

const cliSymlinkPath = '/usr/local/bin/studio';
const legacyCliSymlinkPath = '/usr/local/bin/studio';
const cliSymlinkPath = path.join( os.homedir(), '.local', 'bin', 'studio' );

const binPath = path.join( getResourcesPath(), 'bin' );
const cliPackagedPath = path.join( binPath, 'studio-cli.sh' );
const installScriptPath = path.join( binPath, 'install-studio-cli.sh' );
const uninstallScriptPath = path.join( binPath, 'uninstall-studio-cli.sh' );

const SUPPORTED_SHELLS = [ 'bash', 'zsh' ] as const;
const SHELL_PROFILE_MAP: Record< ( typeof SUPPORTED_SHELLS )[ number ], string > = {
bash: '.bash_profile',
zsh: '.zshrc',
};
const DEFAULT_PROFILE = SHELL_PROFILE_MAP[ 'zsh' ];
const PATH_DEFINITION = '$HOME/.local/bin';
const PATH_EXPORT_LINE = `export PATH="${ PATH_DEFINITION }:$PATH"`;

const ERROR_FILE_ALREADY_EXISTS = 'Studio CLI symlink path already occupied by non-symlink';
// Defined in @vscode/sudo-prompt
const ERROR_PERMISSION = 'User did not grant permission.';

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we don't run sudo, the error message is no longer needed.


export class MacOSCliInstallationManager implements StudioCliInstallationManager {
constructor() {
Expand All @@ -29,36 +38,18 @@ export class MacOSCliInstallationManager implements StudioCliInstallationManager
}

async isCliInstalled(): Promise< boolean > {
const currentSymlinkDestination = await this.getCurrentSymlinkDestination();

// Return true if we are running the development version of the app and the production CLI is installed
if ( process.env.NODE_ENV !== 'production' ) {
const prodCliPackagedPath = path.join(
path.sep,
'Applications',
'Studio.app',
'Contents',
'Resources',
'bin',
'studio-cli.sh'
);
if ( currentSymlinkDestination === prodCliPackagedPath ) {
return true;
}
const existingContent = await this.readShellProfileContent();

if ( ! existingContent.includes( PATH_DEFINITION ) ) {
return false;
}

return currentSymlinkDestination === cliPackagedPath;
return await this.doesSymlinkLeadToPackagedCli( cliSymlinkPath );
}

async installCliWithConfirmation(): Promise< void > {
try {
await this.installCli();
const mainWindow = await getMainWindow();
await dialog.showMessageBox( mainWindow, {
type: 'info',
title: __( 'CLI Installed' ),
message: __( 'The CLI has been installed successfully.' ),
} );
} catch ( error ) {
console.error( 'Failed to install CLI', error );

Expand All @@ -76,8 +67,6 @@ export class MacOSCliInstallationManager implements StudioCliInstallationManager
),
cliSymlinkPath
);
} else if ( error.message === ERROR_PERMISSION ) {
message = __( 'Please ensure you grant Studio admin permissions when prompted.' );
} else {
// Only report unexpected errors to Sentry
Sentry.captureException( error );
Expand All @@ -98,6 +87,7 @@ export class MacOSCliInstallationManager implements StudioCliInstallationManager
async uninstallCliWithConfirmation(): Promise< void > {
try {
await this.uninstallCli();
await this.uninstallLegacyCliIfNeeded();
const mainWindow = await getMainWindow();
await dialog.showMessageBox( mainWindow, {
type: 'info',
Expand Down Expand Up @@ -125,9 +115,21 @@ export class MacOSCliInstallationManager implements StudioCliInstallationManager
}
}

async autoInstallIfNeeded(): Promise< void > {
// Only auto-install on first launch. If the flag is already set but the CLI isn't
// installed, the user must have explicitly disabled it — respect their choice.
const userData = await loadUserData();
if ( userData.cliAutoInstalled ) {
return;
}

await this.installCli();
await updateAppdata( { cliAutoInstalled: true } );
}
Comment thread
ivan-ottinger marked this conversation as resolved.

private async installCli(): Promise< void > {
try {
const stats = await lstat( cliSymlinkPath );
const stats = await fs.promises.lstat( cliSymlinkPath );

if ( ! stats.isSymbolicLink() ) {
throw new Error( ERROR_FILE_ALREADY_EXISTS );
Expand All @@ -144,59 +146,131 @@ export class MacOSCliInstallationManager implements StudioCliInstallationManager
return;
}

try {
const directoryPath = path.dirname( cliSymlinkPath );
const directoryPath = path.dirname( cliSymlinkPath );

await unlink( cliSymlinkPath );
await mkdir( directoryPath, { recursive: true } );
await symlink( cliPackagedPath, cliSymlinkPath );
} catch ( e ) {
// `/usr/local/bin` is not typically writable by non-root users, so in most cases, we run
// this install script with admin privileges to create the symlink.
await sudoExec( `/bin/sh "${ installScriptPath }"`, {
name: packageJson.productName,
env: {
CLI_SYMLINK_PATH: cliSymlinkPath,
CLI_PACKAGED_PATH: cliPackagedPath,
},
} );
try {
await fs.promises.unlink( cliSymlinkPath );
} catch ( error ) {
if ( ! isErrnoException( error ) || error.code !== 'ENOENT' ) {
throw error;
}
}

await fs.promises.mkdir( directoryPath, { recursive: true } );
await fs.promises.symlink( cliPackagedPath, cliSymlinkPath );
await this.ensurePathInProfile();
}

private async uninstallCli(): Promise< void > {
try {
const stats = await lstat( cliSymlinkPath );
const stats = await fs.promises.lstat( cliSymlinkPath );

if ( ! stats.isSymbolicLink() ) {
throw new Error( ERROR_FILE_ALREADY_EXISTS );
}
} catch ( error ) {
if ( isErrnoException( error ) && error.code === 'ENOENT' ) {
// File does not exist, which means we can proceed
} else {
throw error;
// File does not exist, nothing to uninstall.
return;
}
throw error;
}

await fs.promises.unlink( cliSymlinkPath );
}
Comment thread
ivan-ottinger marked this conversation as resolved.

private async uninstallLegacyCliIfNeeded(): Promise< void > {
const legacyCliExists = await this.doesSymlinkLeadToPackagedCli( legacyCliSymlinkPath );
if ( ! legacyCliExists ) {
return;
}

try {
await unlink( cliSymlinkPath );
await fs.promises.unlink( legacyCliSymlinkPath );
} catch ( error ) {
// `/usr/local/bin` is not typically writable by non-root users, so in most cases, we run
// this uninstall script with admin privileges to remove the symlink.
await sudoExec( `/bin/sh "${ uninstallScriptPath }"`, {
name: packageJson.productName,
env: {
CLI_SYMLINK_PATH: cliSymlinkPath,
CLI_SYMLINK_PATH: legacyCliSymlinkPath,
},
} );
}
}

private async getCurrentSymlinkDestination(): Promise< string | null > {
private async ensurePathInProfile(): Promise< void > {
const existingContent = await this.readShellProfileContent();

if ( existingContent.includes( PATH_DEFINITION ) ) {
return;
}

const profilePath = this.getShellProfilePath();

const lineToAppend =
existingContent.endsWith( '\n' ) || existingContent === ''
? `${ PATH_EXPORT_LINE }\n`
: `\n${ PATH_EXPORT_LINE }\n`;

await fs.promises.writeFile( profilePath, existingContent + lineToAppend, 'utf-8' );
}

private getShellProfilePath(): string {
const shell = path.basename( os.userInfo().shell ?? process.env.SHELL ?? '' );
const supportedShell = SUPPORTED_SHELLS.find( ( candidate ) => candidate === shell );
const profileFile = supportedShell ? SHELL_PROFILE_MAP[ supportedShell ] : DEFAULT_PROFILE;
return path.join( os.homedir(), profileFile );
}

private async readShellProfileContent(): Promise< string > {
try {
return await readlink( cliSymlinkPath );
return await fs.promises.readFile( this.getShellProfilePath(), 'utf-8' );
} catch ( error ) {
if ( isErrnoException( error ) && error.code === 'ENOENT' ) {
return '';
}
throw error;
}
}

private async doesSymlinkLeadToPackagedCli( symlinkPath: string ): Promise< boolean > {
try {
const symlinkDestination = await fs.promises.readlink( symlinkPath );

if ( process.env.NODE_ENV !== 'production' ) {
const prodCliPackagedPath = path.join(
path.sep,
'Applications',
'Studio.app',
'Contents',
'Resources',
'bin',
'studio-cli.sh'
);

if ( symlinkDestination === prodCliPackagedPath ) {
return true;
}
}

return symlinkDestination === cliPackagedPath;
} catch {
return null;
return false;
}
}
}

export async function autoInstallMacOSCliIfNeeded(): Promise< void > {
if ( process.platform !== 'darwin' || process.env.NODE_ENV !== 'production' ) {
return;
}

try {
const manager = new MacOSCliInstallationManager();
await manager.autoInstallIfNeeded();
} catch ( error ) {
console.error( 'Failed to auto-install macOS CLI', error );
Sentry.captureException( error );
}
}
1 change: 1 addition & 0 deletions apps/studio/src/storage/storage-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface UserData {
colorScheme?: 'system' | 'light' | 'dark';
betaFeatures?: BetaFeatures;
stopSitesOnQuit?: boolean;
cliAutoInstalled?: boolean;
}

export interface PromptWindowsSpeedUpResult {
Expand Down
3 changes: 2 additions & 1 deletion apps/studio/src/storage/user-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ type UserDataSafeKeys =
| 'preferredTerminal'
| 'preferredEditor'
| 'betaFeatures'
| 'colorScheme';
| 'colorScheme'
| 'cliAutoInstalled';

type PartialUserDataWithSafeKeysToUpdate = Partial< Pick< UserData, UserDataSafeKeys > >;

Expand Down
Loading