-
Notifications
You must be signed in to change notification settings - Fork 392
Expand file tree
/
Copy pathcommonUtils.ts
More file actions
598 lines (553 loc) · 16.8 KB
/
commonUtils.ts
File metadata and controls
598 lines (553 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Writable, Readable } from 'stream';
import * as path from 'path';
import * as os from 'os';
import * as fs from 'fs';
import * as cp from 'child_process';
import * as ptyType from 'node-pty';
import { StringDecoder } from 'string_decoder';
import { toErrorText } from './errors';
import { Disposable, Event, NodeEventEmitter } from '../spec-utils/event';
import { isLocalFile } from '../spec-utils/pfs';
import { escapeRegExCharacters } from '../spec-utils/strings';
import { Log, nullLog } from '../spec-utils/log';
import { ShellServer } from './shellServer';
export { CLIHost, getCLIHost } from './cliHost';
export interface Exec {
stdin: Writable;
stdout: Readable;
stderr: Readable;
exit: Promise<{ code: number | null; signal: string | null }>;
terminate(): Promise<void>;
}
export interface ExecParameters {
env?: NodeJS.ProcessEnv;
cwd?: string;
cmd: string;
args?: string[];
stdio?: [cp.StdioNull | cp.StdioPipe, cp.StdioNull | cp.StdioPipe, cp.StdioNull | cp.StdioPipe];
output: Log;
}
export interface ExecFunction {
(params: ExecParameters): Promise<Exec>;
}
export type GoOS = { [OS in NodeJS.Platform]: OS extends 'win32' ? 'windows' : OS; }[NodeJS.Platform];
export type GoARCH = { [ARCH in NodeJS.Architecture]: ARCH extends 'x64' ? 'amd64' : ARCH; }[NodeJS.Architecture];
export interface PlatformInfo {
os: GoOS;
arch: GoARCH;
variant?: string;
}
export interface PtyExec {
onData: Event<string>;
write?(data: string): void;
resize(cols: number, rows: number): void;
exit: Promise<{ code: number | undefined; signal: number | undefined }>;
terminate(): Promise<void>;
}
export interface PtyExecParameters {
env?: NodeJS.ProcessEnv;
cwd?: string;
cmd: string;
args?: string[];
cols?: number;
rows?: number;
output: Log;
}
export interface PtyExecFunction {
(params: PtyExecParameters): Promise<PtyExec>;
}
export function equalPaths(platform: NodeJS.Platform, a: string, b: string) {
if (platform === 'linux') {
return a === b;
}
return a.toLowerCase() === b.toLowerCase();
}
export async function runCommandNoPty(options: {
exec: ExecFunction;
cmd: string;
args?: string[];
cwd?: string;
env?: NodeJS.ProcessEnv;
stdin?: Buffer | fs.ReadStream | Event<string>;
output: Log;
print?: boolean | 'continuous' | 'onerror';
}) {
const { exec, cmd, args, cwd, env, stdin, output, print } = options;
const p = await exec({
cmd,
args,
cwd,
env,
output,
});
return new Promise<{ stdout: Buffer; stderr: Buffer }>((resolve, reject) => {
const stdout: Buffer[] = [];
const stderr: Buffer[] = [];
const stdoutDecoder = print === 'continuous' ? new StringDecoder() : undefined;
p.stdout.on('data', (chunk: Buffer) => {
stdout.push(chunk);
if (print === 'continuous') {
output.write(stdoutDecoder!.write(chunk));
}
});
p.stdout.on('error', (err: any) => {
// ENOTCONN seen with missing executable in addition to ENOENT on child_process.
if (err?.code !== 'ENOTCONN') {
throw err;
}
});
const stderrDecoder = print === 'continuous' ? new StringDecoder() : undefined;
p.stderr.on('data', (chunk: Buffer) => {
stderr.push(chunk);
if (print === 'continuous') {
output.write(toErrorText(stderrDecoder!.write(chunk)));
}
});
p.stderr.on('error', (err: any) => {
// ENOTCONN seen with missing executable in addition to ENOENT on child_process.
if (err?.code !== 'ENOTCONN') {
throw err;
}
});
const subs: Disposable[] = [];
p.exit.then(({ code, signal }) => {
try {
const failed = !!code || !!signal;
subs.forEach(sub => sub.dispose());
const stdoutBuf = Buffer.concat(stdout);
const stderrBuf = Buffer.concat(stderr);
if (print === true || (failed && print === 'onerror')) {
output.write(stdoutBuf.toString().replace(/\r?\n/g, '\r\n'));
output.write(toErrorText(stderrBuf.toString()));
}
if (print && code) {
output.write(`Exit code ${code}`);
}
if (print && signal) {
output.write(`Process signal ${signal}`);
}
if (failed) {
reject({
message: `Command failed: ${cmd} ${(args || []).join(' ')}`,
stdout: stdoutBuf,
stderr: stderrBuf,
code,
signal,
});
} else {
resolve({
stdout: stdoutBuf,
stderr: stderrBuf,
});
}
} catch (e) {
reject(e);
}
}, reject);
if (stdin instanceof Buffer) {
p.stdin.write(stdin, err => {
if (err) {
reject(err);
}
});
p.stdin.end();
} else if (stdin instanceof fs.ReadStream) {
stdin.pipe(p.stdin);
} else if (typeof stdin === 'function') {
subs.push(stdin(buf => p.stdin.write(buf)));
}
});
}
export async function runCommand(options: {
ptyExec: PtyExecFunction;
cmd: string;
args?: string[];
cwd?: string;
env?: NodeJS.ProcessEnv;
output: Log;
resolveOn?: RegExp;
onDidInput?: Event<string>;
stdin?: string;
print?: 'off' | 'continuous' | 'end';
}) {
const { ptyExec, cmd, args, cwd, env, output, resolveOn, onDidInput, stdin } = options;
const print = options.print || 'continuous';
const p = await ptyExec({
cmd,
args,
cwd,
env,
output: output,
});
return new Promise<{ cmdOutput: string }>((resolve, reject) => {
let cmdOutput = '';
const subs: Disposable[] = [];
if (p.write) {
if (stdin) {
p.write(stdin);
}
if (onDidInput) {
subs.push(onDidInput(data => p.write!(data)));
}
}
p.onData(chunk => {
cmdOutput += chunk;
if (print === 'continuous') {
output.raw(chunk);
}
if (resolveOn && resolveOn.exec(cmdOutput)) {
resolve({ cmdOutput });
}
});
p.exit.then(({ code, signal }) => {
try {
if (print === 'end') {
output.raw(cmdOutput);
}
subs.forEach(sub => sub?.dispose());
if (code || signal) {
reject({
message: `Command failed: ${cmd} ${(args || []).join(' ')}`,
cmdOutput,
code,
signal,
});
} else {
resolve({ cmdOutput });
}
} catch (e) {
reject(e);
}
}, e => {
subs.forEach(sub => sub?.dispose());
reject(e);
});
});
}
// From https://man7.org/linux/man-pages/man7/signal.7.html:
export const processSignals: Record<string, number | undefined> = {
SIGHUP: 1,
SIGINT: 2,
SIGQUIT: 3,
SIGILL: 4,
SIGTRAP: 5,
SIGABRT: 6,
SIGIOT: 6,
SIGBUS: 7,
SIGEMT: undefined,
SIGFPE: 8,
SIGKILL: 9,
SIGUSR1: 10,
SIGSEGV: 11,
SIGUSR2: 12,
SIGPIPE: 13,
SIGALRM: 14,
SIGTERM: 15,
SIGSTKFLT: 16,
SIGCHLD: 17,
SIGCLD: undefined,
SIGCONT: 18,
SIGSTOP: 19,
SIGTSTP: 20,
SIGTTIN: 21,
SIGTTOU: 22,
SIGURG: 23,
SIGXCPU: 24,
SIGXFSZ: 25,
SIGVTALRM: 26,
SIGPROF: 27,
SIGWINCH: 28,
SIGIO: 29,
SIGPOLL: 29,
SIGPWR: 30,
SIGINFO: undefined,
SIGLOST: undefined,
SIGSYS: 31,
SIGUNUSED: 31,
};
export function plainExec(defaultCwd: string | undefined): ExecFunction {
return async function (params: ExecParameters): Promise<Exec> {
const { cmd, args, stdio, output } = params;
const text = `Run: ${cmd} ${(args || []).join(' ').replace(/\n.*/g, '')}`;
const start = output.start(text);
const cwd = params.cwd || defaultCwd;
const env = params.env ? { ...process.env, ...params.env } : process.env;
const exec = await findLocalWindowsExecutable(cmd, cwd, env, output);
const p = cp.spawn(exec, args ?? [], { cwd, env, stdio: stdio as any, windowsHide: true });
return {
stdin: p.stdin,
stdout: p.stdout,
stderr: p.stderr,
exit: new Promise((resolve, reject) => {
p.once('error', err => {
output.stop(text, start);
reject(err);
});
p.once('close', (code, signal) => {
output.stop(text, start);
resolve({ code, signal });
});
}),
async terminate() {
p.kill('SIGKILL');
}
};
};
}
export async function plainPtyExec(defaultCwd: string | undefined, loadNativeModule: <T>(moduleName: string) => Promise<T | undefined>, allowInheritTTY: boolean): Promise<PtyExecFunction> {
const pty = await loadNativeModule<typeof ptyType>('node-pty');
if (!pty) {
const plain = plainExec(defaultCwd);
return plainExecAsPtyExec(plain, allowInheritTTY);
}
return async function (params: PtyExecParameters): Promise<PtyExec> {
const { cmd, args, output } = params;
const text = `Run: ${cmd} ${(args || []).join(' ').replace(/\n.*/g, '')}`;
const start = output.start(text);
const useConpty = false; // TODO: Investigate using a shell with ConPTY. https://github.com/Microsoft/vscode-remote/issues/1234#issuecomment-485501275
const cwd = params.cwd || defaultCwd;
const env = params.env ? { ...process.env, ...params.env } : process.env;
const exec = await findLocalWindowsExecutable(cmd, cwd, env, output);
const p = pty.spawn(exec, args || [], {
cwd,
env: env as any,
cols: output.dimensions?.columns,
rows: output.dimensions?.rows,
useConpty,
});
const subs = [
output.onDidChangeDimensions && output.onDidChangeDimensions(e => p.resize(e.columns, e.rows))
];
return {
onData: p.onData.bind(p),
write: p.write.bind(p),
resize: p.resize.bind(p),
exit: new Promise(resolve => {
p.onExit(({ exitCode, signal }) => {
subs.forEach(sub => sub?.dispose());
output.stop(text, start);
resolve({ code: exitCode, signal });
if (process.platform === 'win32') {
try {
// In some cases the process hasn't cleanly exited on Windows and the winpty-agent gets left around
// https://github.com/microsoft/node-pty/issues/333
p.kill();
} catch {
}
}
});
}),
async terminate() {
p.kill('SIGKILL');
}
};
};
}
export function plainExecAsPtyExec(plain: ExecFunction, allowInheritTTY: boolean): PtyExecFunction {
return async function (params: PtyExecParameters): Promise<PtyExec> {
const p = await plain({
...params,
stdio: allowInheritTTY && params.output !== nullLog ? [
process.stdin.isTTY ? 'inherit' : 'pipe',
process.stdout.isTTY ? 'inherit' : 'pipe',
process.stderr.isTTY ? 'inherit' : 'pipe',
] : undefined,
});
const onDataEmitter = new NodeEventEmitter<string>();
if (p.stdout) {
const stdoutDecoder = new StringDecoder();
p.stdout.on('data', data => onDataEmitter.fire(stdoutDecoder.write(data)));
p.stdout.on('close', () => {
const end = stdoutDecoder.end();
if (end) {
onDataEmitter.fire(end);
}
});
}
if (p.stderr) {
const stderrDecoder = new StringDecoder();
p.stderr.on('data', data => onDataEmitter.fire(stderrDecoder.write(data)));
p.stderr.on('close', () => {
const end = stderrDecoder.end();
if (end) {
onDataEmitter.fire(end);
}
});
}
return {
onData: onDataEmitter.event,
write: p.stdin ? p.stdin.write.bind(p.stdin) : undefined,
resize: () => {},
exit: p.exit.then(({ code, signal }) => ({
code: typeof code === 'number' ? code : undefined,
signal: typeof signal === 'string' ? processSignals[signal] : undefined,
})),
terminate: p.terminate.bind(p),
};
};
}
async function findLocalWindowsExecutable(command: string, cwd = process.cwd(), env: Record<string, string | undefined>, output: Log): Promise<string> {
if (process.platform !== 'win32') {
return command;
}
// From terminalTaskSystem.ts.
// If we have an absolute path then we take it.
if (path.isAbsolute(command)) {
return await findLocalWindowsExecutableWithExtension(command) || command;
}
if (/[/\\]/.test(command)) {
// We have a directory and the directory is relative (see above). Make the path absolute
// to the current working directory.
const fullPath = path.join(cwd, command);
return await findLocalWindowsExecutableWithExtension(fullPath) || fullPath;
}
let pathValue: string | undefined = undefined;
let paths: string[] | undefined = undefined;
// The options can override the PATH. So consider that PATH if present.
if (env) {
// Path can be named in many different ways and for the execution it doesn't matter
for (let key of Object.keys(env)) {
if (key.toLowerCase() === 'path') {
const value = env[key];
if (typeof value === 'string') {
pathValue = value;
paths = value.split(path.delimiter)
.filter(Boolean);
paths.push(path.join(env.ProgramW6432 || 'C:\\Program Files', 'Docker\\Docker\\resources\\bin')); // Fall back when newly installed.
}
break;
}
}
}
// No PATH environment. Bail out.
if (paths === void 0 || paths.length === 0) {
output.write(`findLocalWindowsExecutable: No PATH to look up executable '${command}'.`);
const err = new Error(`No PATH to look up executable '${command}'.`);
(err as any).code = 'ENOENT';
throw err;
}
// We have a simple file name. We get the path variable from the env
// and try to find the executable on the path.
for (let pathEntry of paths) {
// The path entry is absolute.
let fullPath: string;
if (path.isAbsolute(pathEntry)) {
fullPath = path.join(pathEntry, command);
} else {
fullPath = path.join(cwd, pathEntry, command);
}
const withExtension = await findLocalWindowsExecutableWithExtension(fullPath);
if (withExtension) {
return withExtension;
}
}
// Not found in PATH. Bail out.
output.write(`findLocalWindowsExecutable: Exectuable '${command}' not found on PATH '${pathValue}'.`);
const err = new Error(`Exectuable '${command}' not found on PATH '${pathValue}'.`);
(err as any).code = 'ENOENT';
throw err;
}
const pathext = process.env.PATHEXT;
const executableExtensions = pathext ? pathext.toLowerCase().split(';') : ['.com', '.exe', '.bat', '.cmd'];
async function findLocalWindowsExecutableWithExtension(fullPath: string) {
if (executableExtensions.indexOf(path.extname(fullPath)) !== -1) {
return await isLocalFile(fullPath) ? fullPath : undefined;
}
for (const ext of executableExtensions) {
const withExtension = fullPath + ext;
if (await isLocalFile(withExtension)) {
return withExtension;
}
}
return undefined;
}
export function parseVersion(str: string) {
const m = /^'?v?(\d+(\.\d+)*)/.exec(str);
if (!m) {
return undefined;
}
return m[1].split('.')
.map(i => parseInt(i, 10));
}
export function isEarlierVersion(left: number[], right: number[]) {
for (let i = 0, n = Math.max(left.length, right.length); i < n; i++) {
const l = left[i] || 0;
const r = right[i] || 0;
if (l !== r) {
return l < r;
}
}
return false; // Equal.
}
export async function loadNativeModule<T>(moduleName: string): Promise<T | undefined> {
// Check NODE_PATH for Electron. Do this first to avoid loading a binary-incompatible version from the local node_modules during development.
if (process.env.NODE_PATH) {
for (const nodePath of process.env.NODE_PATH.split(path.delimiter)) {
if (nodePath) {
try {
return require(`${nodePath}/${moduleName}`);
} catch (err) {
// Not available.
}
}
}
}
try {
return require(moduleName);
} catch (err) {
// Not available.
}
return undefined;
}
export type PlatformSwitch<T> = T | { posix: T; win32: T };
export function platformDispatch<T>(platform: NodeJS.Platform, platformSwitch: PlatformSwitch<T>) {
if (platformSwitch && typeof platformSwitch === 'object' && 'win32' in platformSwitch) {
return platform === 'win32' ? platformSwitch.win32 : platformSwitch.posix;
}
return platformSwitch;
}
export async function isFile(shellServer: ShellServer, location: string) {
return platformDispatch(shellServer.platform, {
posix: async () => {
try {
await shellServer.exec(`test -f '${location}'`);
return true;
} catch (err) {
return false;
}
},
win32: async () => {
return (await shellServer.exec(`Test-Path '${location}' -PathType Leaf`))
.stdout.trim() === 'True';
}
})();
}
let localUsername: Promise<string>;
export async function getLocalUsername() {
if (localUsername === undefined) {
localUsername = (async () => {
try {
return os.userInfo().username;
} catch (err) {
if (process.platform !== 'linux') {
throw err;
}
// os.userInfo() fails with VS Code snap install: https://github.com/microsoft/vscode-remote-release/issues/6913
const result = await runCommandNoPty({ exec: plainExec(undefined), cmd: 'id', args: ['-u', '-n'], output: nullLog });
return result.stdout.toString().trim();
}
})();
}
return localUsername;
}
export function getEntPasswdShellCommand(userNameOrId: string) {
const escapedForShell = userNameOrId.replace(/['\\]/g, '\\$&');
const escapedForRexExp = escapeRegExCharacters(userNameOrId)
.replaceAll('\'', '\\\'');
// Leading space makes sure we don't concatenate to arithmetic expansion (https://tldp.org/LDP/abs/html/dblparens.html).
return ` (command -v getent >/dev/null 2>&1 && getent passwd '${escapedForShell}' || grep -E '^${escapedForRexExp}|^[^:]*:[^:]*:${escapedForRexExp}:' /etc/passwd || true)`;
}