toolrunner.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. const os = require("os");
  13. const events = require("events");
  14. const child = require("child_process");
  15. const path = require("path");
  16. const io = require("@actions/io");
  17. const ioUtil = require("@actions/io/lib/io-util");
  18. /* eslint-disable @typescript-eslint/unbound-method */
  19. const IS_WINDOWS = process.platform === 'win32';
  20. /*
  21. * Class for running command line tools. Handles quoting and arg parsing in a platform agnostic way.
  22. */
  23. class ToolRunner extends events.EventEmitter {
  24. constructor(toolPath, args, options) {
  25. super();
  26. if (!toolPath) {
  27. throw new Error("Parameter 'toolPath' cannot be null or empty.");
  28. }
  29. this.toolPath = toolPath;
  30. this.args = args || [];
  31. this.options = options || {};
  32. }
  33. _debug(message) {
  34. if (this.options.listeners && this.options.listeners.debug) {
  35. this.options.listeners.debug(message);
  36. }
  37. }
  38. _getCommandString(options, noPrefix) {
  39. const toolPath = this._getSpawnFileName();
  40. const args = this._getSpawnArgs(options);
  41. let cmd = noPrefix ? '' : '[command]'; // omit prefix when piped to a second tool
  42. if (IS_WINDOWS) {
  43. // Windows + cmd file
  44. if (this._isCmdFile()) {
  45. cmd += toolPath;
  46. for (const a of args) {
  47. cmd += ` ${a}`;
  48. }
  49. }
  50. // Windows + verbatim
  51. else if (options.windowsVerbatimArguments) {
  52. cmd += `"${toolPath}"`;
  53. for (const a of args) {
  54. cmd += ` ${a}`;
  55. }
  56. }
  57. // Windows (regular)
  58. else {
  59. cmd += this._windowsQuoteCmdArg(toolPath);
  60. for (const a of args) {
  61. cmd += ` ${this._windowsQuoteCmdArg(a)}`;
  62. }
  63. }
  64. }
  65. else {
  66. // OSX/Linux - this can likely be improved with some form of quoting.
  67. // creating processes on Unix is fundamentally different than Windows.
  68. // on Unix, execvp() takes an arg array.
  69. cmd += toolPath;
  70. for (const a of args) {
  71. cmd += ` ${a}`;
  72. }
  73. }
  74. return cmd;
  75. }
  76. _processLineBuffer(data, strBuffer, onLine) {
  77. try {
  78. let s = strBuffer + data.toString();
  79. let n = s.indexOf(os.EOL);
  80. while (n > -1) {
  81. const line = s.substring(0, n);
  82. onLine(line);
  83. // the rest of the string ...
  84. s = s.substring(n + os.EOL.length);
  85. n = s.indexOf(os.EOL);
  86. }
  87. strBuffer = s;
  88. }
  89. catch (err) {
  90. // streaming lines to console is best effort. Don't fail a build.
  91. this._debug(`error processing line. Failed with error ${err}`);
  92. }
  93. }
  94. _getSpawnFileName() {
  95. if (IS_WINDOWS) {
  96. if (this._isCmdFile()) {
  97. return process.env['COMSPEC'] || 'cmd.exe';
  98. }
  99. }
  100. return this.toolPath;
  101. }
  102. _getSpawnArgs(options) {
  103. if (IS_WINDOWS) {
  104. if (this._isCmdFile()) {
  105. let argline = `/D /S /C "${this._windowsQuoteCmdArg(this.toolPath)}`;
  106. for (const a of this.args) {
  107. argline += ' ';
  108. argline += options.windowsVerbatimArguments
  109. ? a
  110. : this._windowsQuoteCmdArg(a);
  111. }
  112. argline += '"';
  113. return [argline];
  114. }
  115. }
  116. return this.args;
  117. }
  118. _endsWith(str, end) {
  119. return str.endsWith(end);
  120. }
  121. _isCmdFile() {
  122. const upperToolPath = this.toolPath.toUpperCase();
  123. return (this._endsWith(upperToolPath, '.CMD') ||
  124. this._endsWith(upperToolPath, '.BAT'));
  125. }
  126. _windowsQuoteCmdArg(arg) {
  127. // for .exe, apply the normal quoting rules that libuv applies
  128. if (!this._isCmdFile()) {
  129. return this._uvQuoteCmdArg(arg);
  130. }
  131. // otherwise apply quoting rules specific to the cmd.exe command line parser.
  132. // the libuv rules are generic and are not designed specifically for cmd.exe
  133. // command line parser.
  134. //
  135. // for a detailed description of the cmd.exe command line parser, refer to
  136. // http://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts/7970912#7970912
  137. // need quotes for empty arg
  138. if (!arg) {
  139. return '""';
  140. }
  141. // determine whether the arg needs to be quoted
  142. const cmdSpecialChars = [
  143. ' ',
  144. '\t',
  145. '&',
  146. '(',
  147. ')',
  148. '[',
  149. ']',
  150. '{',
  151. '}',
  152. '^',
  153. '=',
  154. ';',
  155. '!',
  156. "'",
  157. '+',
  158. ',',
  159. '`',
  160. '~',
  161. '|',
  162. '<',
  163. '>',
  164. '"'
  165. ];
  166. let needsQuotes = false;
  167. for (const char of arg) {
  168. if (cmdSpecialChars.some(x => x === char)) {
  169. needsQuotes = true;
  170. break;
  171. }
  172. }
  173. // short-circuit if quotes not needed
  174. if (!needsQuotes) {
  175. return arg;
  176. }
  177. // the following quoting rules are very similar to the rules that by libuv applies.
  178. //
  179. // 1) wrap the string in quotes
  180. //
  181. // 2) double-up quotes - i.e. " => ""
  182. //
  183. // this is different from the libuv quoting rules. libuv replaces " with \", which unfortunately
  184. // doesn't work well with a cmd.exe command line.
  185. //
  186. // note, replacing " with "" also works well if the arg is passed to a downstream .NET console app.
  187. // for example, the command line:
  188. // foo.exe "myarg:""my val"""
  189. // is parsed by a .NET console app into an arg array:
  190. // [ "myarg:\"my val\"" ]
  191. // which is the same end result when applying libuv quoting rules. although the actual
  192. // command line from libuv quoting rules would look like:
  193. // foo.exe "myarg:\"my val\""
  194. //
  195. // 3) double-up slashes that precede a quote,
  196. // e.g. hello \world => "hello \world"
  197. // hello\"world => "hello\\""world"
  198. // hello\\"world => "hello\\\\""world"
  199. // hello world\ => "hello world\\"
  200. //
  201. // technically this is not required for a cmd.exe command line, or the batch argument parser.
  202. // the reasons for including this as a .cmd quoting rule are:
  203. //
  204. // a) this is optimized for the scenario where the argument is passed from the .cmd file to an
  205. // external program. many programs (e.g. .NET console apps) rely on the slash-doubling rule.
  206. //
  207. // b) it's what we've been doing previously (by deferring to node default behavior) and we
  208. // haven't heard any complaints about that aspect.
  209. //
  210. // note, a weakness of the quoting rules chosen here, is that % is not escaped. in fact, % cannot be
  211. // escaped when used on the command line directly - even though within a .cmd file % can be escaped
  212. // by using %%.
  213. //
  214. // the saving grace is, on the command line, %var% is left as-is if var is not defined. this contrasts
  215. // the line parsing rules within a .cmd file, where if var is not defined it is replaced with nothing.
  216. //
  217. // one option that was explored was replacing % with ^% - i.e. %var% => ^%var^%. this hack would
  218. // often work, since it is unlikely that var^ would exist, and the ^ character is removed when the
  219. // variable is used. the problem, however, is that ^ is not removed when %* is used to pass the args
  220. // to an external program.
  221. //
  222. // an unexplored potential solution for the % escaping problem, is to create a wrapper .cmd file.
  223. // % can be escaped within a .cmd file.
  224. let reverse = '"';
  225. let quoteHit = true;
  226. for (let i = arg.length; i > 0; i--) {
  227. // walk the string in reverse
  228. reverse += arg[i - 1];
  229. if (quoteHit && arg[i - 1] === '\\') {
  230. reverse += '\\'; // double the slash
  231. }
  232. else if (arg[i - 1] === '"') {
  233. quoteHit = true;
  234. reverse += '"'; // double the quote
  235. }
  236. else {
  237. quoteHit = false;
  238. }
  239. }
  240. reverse += '"';
  241. return reverse
  242. .split('')
  243. .reverse()
  244. .join('');
  245. }
  246. _uvQuoteCmdArg(arg) {
  247. // Tool runner wraps child_process.spawn() and needs to apply the same quoting as
  248. // Node in certain cases where the undocumented spawn option windowsVerbatimArguments
  249. // is used.
  250. //
  251. // Since this function is a port of quote_cmd_arg from Node 4.x (technically, lib UV,
  252. // see https://github.com/nodejs/node/blob/v4.x/deps/uv/src/win/process.c for details),
  253. // pasting copyright notice from Node within this function:
  254. //
  255. // Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  256. //
  257. // Permission is hereby granted, free of charge, to any person obtaining a copy
  258. // of this software and associated documentation files (the "Software"), to
  259. // deal in the Software without restriction, including without limitation the
  260. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  261. // sell copies of the Software, and to permit persons to whom the Software is
  262. // furnished to do so, subject to the following conditions:
  263. //
  264. // The above copyright notice and this permission notice shall be included in
  265. // all copies or substantial portions of the Software.
  266. //
  267. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  268. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  269. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  270. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  271. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  272. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  273. // IN THE SOFTWARE.
  274. if (!arg) {
  275. // Need double quotation for empty argument
  276. return '""';
  277. }
  278. if (!arg.includes(' ') && !arg.includes('\t') && !arg.includes('"')) {
  279. // No quotation needed
  280. return arg;
  281. }
  282. if (!arg.includes('"') && !arg.includes('\\')) {
  283. // No embedded double quotes or backslashes, so I can just wrap
  284. // quote marks around the whole thing.
  285. return `"${arg}"`;
  286. }
  287. // Expected input/output:
  288. // input : hello"world
  289. // output: "hello\"world"
  290. // input : hello""world
  291. // output: "hello\"\"world"
  292. // input : hello\world
  293. // output: hello\world
  294. // input : hello\\world
  295. // output: hello\\world
  296. // input : hello\"world
  297. // output: "hello\\\"world"
  298. // input : hello\\"world
  299. // output: "hello\\\\\"world"
  300. // input : hello world\
  301. // output: "hello world\\" - note the comment in libuv actually reads "hello world\"
  302. // but it appears the comment is wrong, it should be "hello world\\"
  303. let reverse = '"';
  304. let quoteHit = true;
  305. for (let i = arg.length; i > 0; i--) {
  306. // walk the string in reverse
  307. reverse += arg[i - 1];
  308. if (quoteHit && arg[i - 1] === '\\') {
  309. reverse += '\\';
  310. }
  311. else if (arg[i - 1] === '"') {
  312. quoteHit = true;
  313. reverse += '\\';
  314. }
  315. else {
  316. quoteHit = false;
  317. }
  318. }
  319. reverse += '"';
  320. return reverse
  321. .split('')
  322. .reverse()
  323. .join('');
  324. }
  325. _cloneExecOptions(options) {
  326. options = options || {};
  327. const result = {
  328. cwd: options.cwd || process.cwd(),
  329. env: options.env || process.env,
  330. silent: options.silent || false,
  331. windowsVerbatimArguments: options.windowsVerbatimArguments || false,
  332. failOnStdErr: options.failOnStdErr || false,
  333. ignoreReturnCode: options.ignoreReturnCode || false,
  334. delay: options.delay || 10000
  335. };
  336. result.outStream = options.outStream || process.stdout;
  337. result.errStream = options.errStream || process.stderr;
  338. return result;
  339. }
  340. _getSpawnOptions(options, toolPath) {
  341. options = options || {};
  342. const result = {};
  343. result.cwd = options.cwd;
  344. result.env = options.env;
  345. result['windowsVerbatimArguments'] =
  346. options.windowsVerbatimArguments || this._isCmdFile();
  347. if (options.windowsVerbatimArguments) {
  348. result.argv0 = `"${toolPath}"`;
  349. }
  350. return result;
  351. }
  352. /**
  353. * Exec a tool.
  354. * Output will be streamed to the live console.
  355. * Returns promise with return code
  356. *
  357. * @param tool path to tool to exec
  358. * @param options optional exec options. See ExecOptions
  359. * @returns number
  360. */
  361. exec() {
  362. return __awaiter(this, void 0, void 0, function* () {
  363. // root the tool path if it is unrooted and contains relative pathing
  364. if (!ioUtil.isRooted(this.toolPath) &&
  365. (this.toolPath.includes('/') ||
  366. (IS_WINDOWS && this.toolPath.includes('\\')))) {
  367. // prefer options.cwd if it is specified, however options.cwd may also need to be rooted
  368. this.toolPath = path.resolve(process.cwd(), this.options.cwd || process.cwd(), this.toolPath);
  369. }
  370. // if the tool is only a file name, then resolve it from the PATH
  371. // otherwise verify it exists (add extension on Windows if necessary)
  372. this.toolPath = yield io.which(this.toolPath, true);
  373. return new Promise((resolve, reject) => {
  374. this._debug(`exec tool: ${this.toolPath}`);
  375. this._debug('arguments:');
  376. for (const arg of this.args) {
  377. this._debug(` ${arg}`);
  378. }
  379. const optionsNonNull = this._cloneExecOptions(this.options);
  380. if (!optionsNonNull.silent && optionsNonNull.outStream) {
  381. optionsNonNull.outStream.write(this._getCommandString(optionsNonNull) + os.EOL);
  382. }
  383. const state = new ExecState(optionsNonNull, this.toolPath);
  384. state.on('debug', (message) => {
  385. this._debug(message);
  386. });
  387. const fileName = this._getSpawnFileName();
  388. const cp = child.spawn(fileName, this._getSpawnArgs(optionsNonNull), this._getSpawnOptions(this.options, fileName));
  389. const stdbuffer = '';
  390. if (cp.stdout) {
  391. cp.stdout.on('data', (data) => {
  392. if (this.options.listeners && this.options.listeners.stdout) {
  393. this.options.listeners.stdout(data);
  394. }
  395. if (!optionsNonNull.silent && optionsNonNull.outStream) {
  396. optionsNonNull.outStream.write(data);
  397. }
  398. this._processLineBuffer(data, stdbuffer, (line) => {
  399. if (this.options.listeners && this.options.listeners.stdline) {
  400. this.options.listeners.stdline(line);
  401. }
  402. });
  403. });
  404. }
  405. const errbuffer = '';
  406. if (cp.stderr) {
  407. cp.stderr.on('data', (data) => {
  408. state.processStderr = true;
  409. if (this.options.listeners && this.options.listeners.stderr) {
  410. this.options.listeners.stderr(data);
  411. }
  412. if (!optionsNonNull.silent &&
  413. optionsNonNull.errStream &&
  414. optionsNonNull.outStream) {
  415. const s = optionsNonNull.failOnStdErr
  416. ? optionsNonNull.errStream
  417. : optionsNonNull.outStream;
  418. s.write(data);
  419. }
  420. this._processLineBuffer(data, errbuffer, (line) => {
  421. if (this.options.listeners && this.options.listeners.errline) {
  422. this.options.listeners.errline(line);
  423. }
  424. });
  425. });
  426. }
  427. cp.on('error', (err) => {
  428. state.processError = err.message;
  429. state.processExited = true;
  430. state.processClosed = true;
  431. state.CheckComplete();
  432. });
  433. cp.on('exit', (code) => {
  434. state.processExitCode = code;
  435. state.processExited = true;
  436. this._debug(`Exit code ${code} received from tool '${this.toolPath}'`);
  437. state.CheckComplete();
  438. });
  439. cp.on('close', (code) => {
  440. state.processExitCode = code;
  441. state.processExited = true;
  442. state.processClosed = true;
  443. this._debug(`STDIO streams have closed for tool '${this.toolPath}'`);
  444. state.CheckComplete();
  445. });
  446. state.on('done', (error, exitCode) => {
  447. if (stdbuffer.length > 0) {
  448. this.emit('stdline', stdbuffer);
  449. }
  450. if (errbuffer.length > 0) {
  451. this.emit('errline', errbuffer);
  452. }
  453. cp.removeAllListeners();
  454. if (error) {
  455. reject(error);
  456. }
  457. else {
  458. resolve(exitCode);
  459. }
  460. });
  461. });
  462. });
  463. }
  464. }
  465. exports.ToolRunner = ToolRunner;
  466. /**
  467. * Convert an arg string to an array of args. Handles escaping
  468. *
  469. * @param argString string of arguments
  470. * @returns string[] array of arguments
  471. */
  472. function argStringToArray(argString) {
  473. const args = [];
  474. let inQuotes = false;
  475. let escaped = false;
  476. let arg = '';
  477. function append(c) {
  478. // we only escape double quotes.
  479. if (escaped && c !== '"') {
  480. arg += '\\';
  481. }
  482. arg += c;
  483. escaped = false;
  484. }
  485. for (let i = 0; i < argString.length; i++) {
  486. const c = argString.charAt(i);
  487. if (c === '"') {
  488. if (!escaped) {
  489. inQuotes = !inQuotes;
  490. }
  491. else {
  492. append(c);
  493. }
  494. continue;
  495. }
  496. if (c === '\\' && escaped) {
  497. append(c);
  498. continue;
  499. }
  500. if (c === '\\' && inQuotes) {
  501. escaped = true;
  502. continue;
  503. }
  504. if (c === ' ' && !inQuotes) {
  505. if (arg.length > 0) {
  506. args.push(arg);
  507. arg = '';
  508. }
  509. continue;
  510. }
  511. append(c);
  512. }
  513. if (arg.length > 0) {
  514. args.push(arg.trim());
  515. }
  516. return args;
  517. }
  518. exports.argStringToArray = argStringToArray;
  519. class ExecState extends events.EventEmitter {
  520. constructor(options, toolPath) {
  521. super();
  522. this.processClosed = false; // tracks whether the process has exited and stdio is closed
  523. this.processError = '';
  524. this.processExitCode = 0;
  525. this.processExited = false; // tracks whether the process has exited
  526. this.processStderr = false; // tracks whether stderr was written to
  527. this.delay = 10000; // 10 seconds
  528. this.done = false;
  529. this.timeout = null;
  530. if (!toolPath) {
  531. throw new Error('toolPath must not be empty');
  532. }
  533. this.options = options;
  534. this.toolPath = toolPath;
  535. if (options.delay) {
  536. this.delay = options.delay;
  537. }
  538. }
  539. CheckComplete() {
  540. if (this.done) {
  541. return;
  542. }
  543. if (this.processClosed) {
  544. this._setResult();
  545. }
  546. else if (this.processExited) {
  547. this.timeout = setTimeout(ExecState.HandleTimeout, this.delay, this);
  548. }
  549. }
  550. _debug(message) {
  551. this.emit('debug', message);
  552. }
  553. _setResult() {
  554. // determine whether there is an error
  555. let error;
  556. if (this.processExited) {
  557. if (this.processError) {
  558. error = new Error(`There was an error when attempting to execute the process '${this.toolPath}'. This may indicate the process failed to start. Error: ${this.processError}`);
  559. }
  560. else if (this.processExitCode !== 0 && !this.options.ignoreReturnCode) {
  561. error = new Error(`The process '${this.toolPath}' failed with exit code ${this.processExitCode}`);
  562. }
  563. else if (this.processStderr && this.options.failOnStdErr) {
  564. error = new Error(`The process '${this.toolPath}' failed because one or more lines were written to the STDERR stream`);
  565. }
  566. }
  567. // clear the timeout
  568. if (this.timeout) {
  569. clearTimeout(this.timeout);
  570. this.timeout = null;
  571. }
  572. this.done = true;
  573. this.emit('done', error, this.processExitCode);
  574. }
  575. static HandleTimeout(state) {
  576. if (state.done) {
  577. return;
  578. }
  579. if (!state.processClosed && state.processExited) {
  580. const message = `The STDIO streams did not close within ${state.delay /
  581. 1000} seconds of the exit event from process '${state.toolPath}'. This may indicate a child process inherited the STDIO streams and has not yet exited.`;
  582. state._debug(message);
  583. }
  584. state._setResult();
  585. }
  586. }
  587. //# sourceMappingURL=toolrunner.js.map