Skip to content

Commit 7a6cd7a

Browse files
committed
Use shell startup scripts on non-darwin platforms too
1 parent da1ffb4 commit 7a6cd7a

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/interceptors/fresh-terminal.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,18 @@ const commandExists = (path: string): Promise<boolean> => ensureCommandExists(pa
2222
const DEFAULT_GIT_BASH_PATH = 'C:/Program Files/git/git-bash.exe';
2323
const PATH_VAR_SEPARATOR = process.platform === 'win32' ? ';' : ':';
2424
const SHELL = (process.env.SHELL || '').split('/').slice(-1)[0];
25+
2526
const OVERRIDE_BIN_PATH = path.join(__dirname, 'terminal-wrappers');
27+
// Generate POSIX paths for git-bash on Windows (or use the normal path everywhere where)
28+
const POSIX_OVERRIDE_BIN_PATH = process.platform === 'win32'
29+
? OVERRIDE_BIN_PATH.replace(/\\/g, '/').replace(/^(\w+):/, (_all, driveLetter) => `/${driveLetter.toLowerCase()}`)
30+
: OVERRIDE_BIN_PATH;
2631

2732
interface SpawnArgs {
2833
command: string;
2934
args?: string[];
3035
options?: SpawnOptions;
36+
skipStartupScripts?: true;
3137
}
3238

3339
const getTerminalCommand = _.memoize(async (): Promise<SpawnArgs | null> => {
@@ -37,7 +43,7 @@ const getTerminalCommand = _.memoize(async (): Promise<SpawnArgs | null> => {
3743
} else if (await canAccess(DEFAULT_GIT_BASH_PATH)) {
3844
return { command: DEFAULT_GIT_BASH_PATH };
3945
} else {
40-
return { command: 'start', args: ['cmd'], options: { shell: true } };
46+
return { command: 'start', args: ['cmd'], options: { shell: true }, skipStartupScripts: true };
4147
}
4248
} else if (process.platform === 'linux') {
4349
if (GSettings.isAvailable()) {
@@ -200,13 +206,13 @@ export class TerminalInterceptor implements Interceptor {
200206
const terminalSpawnArgs = await getTerminalCommand();
201207
if (!terminalSpawnArgs) throw new Error('Could not find a suitable terminal');
202208

203-
const { command, args, options } = terminalSpawnArgs;
209+
const { command, args, options, skipStartupScripts } = terminalSpawnArgs;
204210

205-
// On OSX, our PATH override below doesn't work, because path_helper always runs and prepends
206-
// the real paths over the top. To fix this, we (very carefully!) rewrite shell startup
207-
// scripts, to reset PATH there.
211+
// Our PATH override below may not work, e.g. because OSX's path_helper always prepends
212+
// the real paths over the top, and git-bash ignore env var paths overrides. To fix this,
213+
// we (very carefully!) rewrite shell startup scripts, to reset the PATH in our shell.
208214
// This gets reset on exit, and is behind a flag so it won't affect other shells anyway.
209-
if (process.platform === 'darwin') await editShellStartupScripts();
215+
if (!skipStartupScripts) await editShellStartupScripts();
210216

211217
const childProc = spawn(
212218
command,
@@ -242,8 +248,13 @@ export class TerminalInterceptor implements Interceptor {
242248
terminals[proxyPort] = (terminals[proxyPort] || []).concat(childProc);
243249

244250
const onTerminalClosed = () => {
245-
if (process.platform === 'darwin') resetShellStartupScripts();
246251
terminals[proxyPort] = _.reject(terminals[proxyPort], childProc);
252+
253+
// Delay slightly as some terminals (gnome-terminal) exit immediately,
254+
// and start the terminal elsewhere, so it may not have started yet.
255+
setTimeout(() => {
256+
if (_.every(terminals, ts => _.isEmpty(ts))) resetShellStartupScripts();
257+
}, 500);
247258
};
248259
childProc.once('exit', onTerminalClosed);
249260
childProc.once('error', (e) => {

0 commit comments

Comments
 (0)