diff --git a/packages/exec/__tests__/exec.test.ts b/packages/exec/__tests__/exec.test.ts index f4dfab0ff9..ae9844d661 100644 --- a/packages/exec/__tests__/exec.test.ts +++ b/packages/exec/__tests__/exec.test.ts @@ -538,6 +538,22 @@ describe('@actions/exec', () => { expect(output.trim()).toBe(`args[0]: "hello"${os.EOL}args[1]: "world"`) }) + it('Exec roots throws friendly error when bad cwd is specified', async () => { + const execOptions = getExecOptions() + execOptions.cwd = 'nonexistent/path' + + await expect(exec.exec('ls', ['-all'], execOptions)).rejects.toThrowError( + `The cwd: ${execOptions.cwd} does not exist!` + ) + }) + + it('Exec roots does not throw when valid cwd is provided', async () => { + const execOptions = getExecOptions() + execOptions.cwd = './' + + await expect(exec.exec('ls', ['-all'], execOptions)).resolves.toBe(0) + }) + it('Exec roots relative tool path using rooted options.cwd', async () => { let command: string if (IS_WINDOWS) { diff --git a/packages/exec/actions-exec-1.0.4.tgz b/packages/exec/actions-exec-1.0.4.tgz new file mode 100644 index 0000000000..f189222ffa Binary files /dev/null and b/packages/exec/actions-exec-1.0.4.tgz differ diff --git a/packages/exec/src/toolrunner.ts b/packages/exec/src/toolrunner.ts index 6b73d608d0..4fe2d9e4e0 100644 --- a/packages/exec/src/toolrunner.ts +++ b/packages/exec/src/toolrunner.ts @@ -414,13 +414,17 @@ export class ToolRunner extends events.EventEmitter { // otherwise verify it exists (add extension on Windows if necessary) this.toolPath = await io.which(this.toolPath, true) - return new Promise((resolve, reject) => { + return new Promise(async (resolve, reject) => { this._debug(`exec tool: ${this.toolPath}`) this._debug('arguments:') for (const arg of this.args) { this._debug(` ${arg}`) } + this.on('error', (error: Error) => { + reject(error) + }) + const optionsNonNull = this._cloneExecOptions(this.options) if (!optionsNonNull.silent && optionsNonNull.outStream) { optionsNonNull.outStream.write( @@ -433,6 +437,14 @@ export class ToolRunner extends events.EventEmitter { this._debug(message) }) + if (this.options.cwd && !(await ioUtil.exists(this.options.cwd))) { + this.emit( + 'error', + new Error(`The cwd: ${this.options.cwd} does not exist!`) + ) + return + } + const fileName = this._getSpawnFileName() const cp = child.spawn( fileName,