Skip to content

Commit afad051

Browse files
restore process.env after exec action with side-effects (#153)
* restore process.env after exec action with side-effects * added try/catch/finally to ensure env gets reset even if the action throws * spread syntax for cloning
1 parent bfd5622 commit afad051

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

src/lib/run-dev.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,11 @@ async function invokeAction ({ actionRequestContext, logger }) {
349349
}
350350
}
351351
}
352+
353+
// if we run an action, we will restore the process.env after the call
354+
// we must do this before we load the action because code can execute on require/import
355+
const preCallEnv = { ...process.env }
356+
const originalCwd = process.cwd()
352357
// generate an activationID just like openwhisk
353358
process.env.__OW_ACTIVATION_ID = crypto.randomBytes(16).toString('hex')
354359

@@ -417,6 +422,10 @@ async function invokeAction ({ actionRequestContext, logger }) {
417422
statusCode,
418423
body: { error: 'Response is not valid \'message/http\'.' }
419424
}
425+
} finally {
426+
logger.debug('restoring process.env and cwd')
427+
process.env = preCallEnv // restore the environment variables
428+
process.chdir(originalCwd) // restore the original working directory
420429
}
421430
} else {
422431
// this case the action returned an error object, so we should use it

0 commit comments

Comments
 (0)