Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 46 additions & 20 deletions src/vs/platform/externalTerminal/node/externalTerminalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ abstract class ExternalTerminalService {
return {
windows: WindowsExternalTerminalService.getDefaultTerminalWindows(),
linux: await LinuxExternalTerminalService.getDefaultTerminalLinuxReady(),
osx: 'xterm'
osx: DEFAULT_TERMINAL_OSX
};
}
}
Expand Down Expand Up @@ -189,26 +189,21 @@ export class MacExternalTerminalService extends ExternalTerminalService implemen
}
}

let stderr = '';
const osa = cp.spawn(MacExternalTerminalService.OSASCRIPT, osaArgs);
osa.on('error', err => {
reject(improveError(err));
});
osa.stderr.on('data', (data) => {
stderr += data.toString();
});
osa.on('exit', (code: number) => {
if (code === 0) { // OK
resolve(undefined);
} else {
if (stderr) {
const lines = stderr.split('\n', 1);
reject(new Error(lines[0]));
} else {
reject(new Error(nls.localize('mac.terminal.script.failed', "Script '{0}' failed with exit code {1}", script, code)));
}
}
});
setupSpawnErrorHandling(osa, resolve, reject, terminalApp);
} else if (terminalApp === 'Ghostty.app') {
// Ghostty uses CLI flags directly instead of AppleScript like Mac Terminal and iTerm
// Note: -na is required (not just -a) because we need to spawn a new instance that
// receives our --args. With just -a, if Ghostty is already running, open will
// activate the existing instance and ignore --args entirely.
const env = Object.assign({}, getSanitizedEnvironment(process), envVars);
const openArgs = ['-na', 'Ghostty.app', '--args'];
openArgs.push('--working-directory=' + dir);
openArgs.push('--wait-after-command=true');
openArgs.push('-e', ...args);

const cmd = cp.spawn('/usr/bin/open', openArgs, { env });
setupSpawnErrorHandling(cmd, resolve, reject, terminalApp);
} else {
reject(new Error(nls.localize('mac.terminal.type.not.supported', "'{0}' not supported", terminalApp)));
}
Expand Down Expand Up @@ -354,6 +349,37 @@ function improveError(err: Error & { errno?: string; path?: string }): Error {
return err;
}

/**
* Attaches error handling to a spawned child process for terminal launching.
*/
function setupSpawnErrorHandling(
cmd: cp.ChildProcess,
resolve: (value: number | PromiseLike<number | undefined> | undefined) => void,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
reject: (reason?: any) => void,
terminalApp: string
): void {
let stderr = '';
cmd.on('error', err => {
reject(improveError(err));
});
cmd.stderr?.on('data', (data) => {
stderr += data.toString();
});
cmd.on('exit', (code: number) => {
if (code === 0) {
resolve(undefined);
} else {
if (stderr) {
const lines = stderr.split('\n', 1);
reject(new Error(lines[0]));
} else {
reject(new Error(nls.localize('mac.terminal.launch.failed', "Launching '{0}' failed with exit code {1}", terminalApp, code)));
}
}
});
}

/**
* Quote args if necessary and combine into a space separated string.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,29 @@ suite('ExternalTerminalService', () => {
);
});

test(`MacTerminalService - Ghostty.app should be spawned correctly`, done => {
const testCwd = 'path/to/workspace';
const mockSpawner: any = {
spawn: (command: any, args: any, opts: any) => {
strictEqual(command, '/usr/bin/open');
strictEqual(args[0], '-a');
strictEqual(args[1], 'Ghostty.app');
strictEqual(args[2], testCwd);
strictEqual(opts.cwd, testCwd);
done();
return {
on: (evt: any) => evt
};
}
};
const testService = new MacExternalTerminalService();
testService.spawnTerminal(
mockSpawner,
{ osxExec: 'Ghostty.app' },
testCwd
);
});

test(`LinuxTerminalService - uses terminal from configuration`, done => {
const testCwd = 'path/to/workspace';
const mockSpawner: any = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import * as nls from '../../../../nls.js';
import * as paths from '../../../../base/common/path.js';
import { DEFAULT_TERMINAL_OSX, IExternalTerminalSettings } from '../../../../platform/externalTerminal/common/externalTerminal.js';
import { IExternalTerminalSettings } from '../../../../platform/externalTerminal/common/externalTerminal.js';
import { MenuId, MenuRegistry } from '../../../../platform/actions/common/actions.js';
import { KeyMod, KeyCode } from '../../../../base/common/keyCodes.js';
import { IHistoryService } from '../../../services/history/common/history.js';
Expand Down Expand Up @@ -128,7 +128,7 @@ export class ExternalTerminalContribution implements IWorkbenchContribution {
'terminal.external.osxExec': {
type: 'string',
description: nls.localize('terminal.external.osxExec', "Customizes which terminal application to run on macOS."),
default: DEFAULT_TERMINAL_OSX,
default: terminals.osx,
scope: ConfigurationScope.APPLICATION
},
'terminal.external.linuxExec': {
Expand Down
Loading