serverless-optimizer-plugin icon indicating copy to clipboard operation
serverless-optimizer-plugin copied to clipboard

Optimizer fails when deploy is started from project root

Open HyperBrain opened this issue 9 years ago • 2 comments

The optimizer will fail if you run a sls function deploy -a from the project root and have multiple function roots in a deeper level. It does not find the inclusion paths.

It seems the plugin does not use the magic handler strategy to find the function root from the function definitions.

If you start the deploy from the function root, it works as expected.

@doapp-ryanp I suggest that the plugin uses SLS Project to get the function root and sets its CWD to there and then executes all the stuff. Then it should work.

HyperBrain avatar Apr 08 '16 08:04 HyperBrain

I'm leaving this here to add to the discussion. I think i have identified the problem. I believe the issue is a result of the ternary operator on line 288 of index.js:

destDir = (fs.lstatSync(p).isDirectory()) ? destPath : path.dirname(destPath);

If p does not exist fs.lstatSync(p) will fail and exit. This is the case when you have includesPath in your function but are deploying from the sls project root. My attempt to fix the issue was to use a try catch around fs.lstatSync() although I'm not satisfied with that solution.

hollanddd avatar Apr 25 '16 21:04 hollanddd

I just recently came across this issue, the deployment and packaging works when it is ran from the function root but failed when ran from project root.

I've modified the line of code that @hollanddd found to

destDir = (fs.lstatSync(srcPath).isDirectory()) ? destPath : path.dirname(destPath);

and that seems to make includePaths work to my expectations (there is no comment as to why the includePath are used rather than the srcPath forthe parameter to lstatSync)

malatasf avatar May 16 '16 11:05 malatasf