apache/openwhisk-runtime-nodejs

CWD is not the root of the zip action

Closed this issue · 1 comments

Problem when your action wants to reference another file in your zip, locally this is easy because the node process usually get's started from the root of the project where the package.json is located.

For example the following code works fine in my local computer.
Since I'm running the node process locally

const fs = require('fs')
exports.main = () => {
    const cwd = process.cwd()
    const dirname = __dirname
    //in openwhisk need to find the root of the zip
    console.log(`dirname= ${dirname}`)
    console.log(`cwd= ${cwd}`)
    const file = fs.readFileSync('secondfile.txt',{encoding:'utf8'})
    console.log(file)
    return {file, cwd, dirname}
}

I run this code in openwhisk I get an error about not found

In openwhisk currently the current working directory (cwd) is set to where the nodejs process started /nodejsAction

This is different from using for example dockerskeleton or python action, where the cwd is changed to the tmp directory where the the zip action get's unzip.

For now the workaround is to use __dirname like ${dirname}/secondfile.txt instead of secondfile.txt

const fs = require('fs')
exports.main = () => {
    const cwd = process.cwd()
    const dirname = __dirname
    //in openwhisk need to find the root of the zip
    console.log(`dirname= ${dirname}`)
    console.log(`cwd= ${cwd}`)
    const file = fs.readFileSync(`${dirname}/secondfile.txt`,{encoding:'utf8'})
    console.log(file)
    return {file, cwd, dirname}
}

The fix is in runner.js to change the process directory process.chdir() to the directory where the zip get's extracted

https://github.com/apache/incubator-openwhisk-runtime-nodejs/blob/master/core/nodejsActionBase/runner.js#L59

process.chdir(moduleDir)
 thisRunner.userScriptMain = eval('require("' + moduleDir + '").' + message.main);

cc @rabbah

@csantanapr I believe this can be closed now