Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Non node functions are attempted to be packaged #808

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/packageModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,12 @@ module.exports = {

// Copy artifacts to package location
if (isIndividialPackaging.call(this)) {
_.forEach(functionNames, funcName => copyArtifactByName.call(this, funcName));
const nodeFunctionNames = _.filter(functionNames, funcName => {
Copy link

@timbodeit timbodeit May 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same nodeFunctionNames would also need to be used below in lines 149 (previously 154) and 165 (previously 160) to avoid serverless-webpack still overriding the artifact path of the given function.

const func = this.serverless.service.getFunction(funcName);
const runtime = func.runtime || this.serverless.service.provider.runtime || 'nodejs';
return runtime.match(/node/);
Comment on lines +143 to +145
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you create a separate function to re-use this code from line 217?
For example, you can create that function just below the getHandlerFile function:

    const isNodeRuntime = funcName => {
      const func = this.serverless.service.getFunction(funcName);
      const runtime = func.runtime || this.serverless.service.provider.runtime || 'nodejs';

      return runtime.match(/node/);
    };

And then, re-use it here (and when we build the allEntryFunctions const):

Suggested change
const func = this.serverless.service.getFunction(funcName);
const runtime = func.runtime || this.serverless.service.provider.runtime || 'nodejs';
return runtime.match(/node/);
return isNodeRuntime(funcName);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option might be to add something like a getAllNodeFunctions() at a higher to replace this.serverless.service.getAllFunctions() calls with a filtered version. This could then also be used to provide similar logic in files like validate.js.

});
_.forEach(nodeFunctionNames, funcName => copyArtifactByName.call(this, funcName));
} else {
// Copy service packaged artifact
const serviceName = this.serverless.service.getServiceObject().name;
Expand Down
22 changes: 18 additions & 4 deletions tests/packageModules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,16 +477,21 @@ describe('packageModules', () => {
});

describe('copyExistingArtifacts()', () => {
const allFunctions = [ 'func1', 'func2' ];
const allFunctions = [ 'func1', 'func2', 'funcPython' ];
const func1 = {
handler: 'src/handler1',
events: []
};
const func2 = {
handler: 'src/handler2',
events: []
events: [],
runtime: 'node'
};
const funcPython = {
handler: 'src/handlerPython',
events: [],
runtime: 'python'
};

const entryFunctions = [
{
handlerFile: 'src/handler1.js',
Expand All @@ -497,6 +502,11 @@ describe('packageModules', () => {
handlerFile: 'src/handler2.js',
funcName: 'func2',
func: func2
},
{
handlerFile: 'src/handlerPython.js',
funcName: 'funcPython',
func: funcPython
}
];

Expand All @@ -515,6 +525,7 @@ describe('packageModules', () => {
getAllFunctionsStub.returns(allFunctions);
getFunctionStub.withArgs('func1').returns(func1);
getFunctionStub.withArgs('func2').returns(func2);
getFunctionStub.withArgs('funcPython').returns(funcPython);
});

it('copies the artifact', () => {
Expand Down Expand Up @@ -660,9 +671,10 @@ describe('packageModules', () => {
getAllFunctionsStub.returns(allFunctions);
getFunctionStub.withArgs('func1').returns(func1);
getFunctionStub.withArgs('func2').returns(func2);
getFunctionStub.withArgs('funcPython').returns(funcPython);
});

it('copies each artifact', () => {
it('copies each node artifact', () => {
const expectedFunc1Destination = path.join('.serverless', 'func1.zip');
const expectedFunc2Destination = path.join('.serverless', 'func2.zip');

Expand All @@ -682,6 +694,8 @@ describe('packageModules', () => {

it('copies only the artifact for function specified in options', () => {
_.set(module, 'options.function', 'func1');
// when options.function is set, validate.js will only have that function as an entry point - mocking that behavior:
_.set(module, 'entryFunctions', _.pickBy(entryFunctions, [ 'funcName', 'func1' ]));
const expectedFunc1Destination = path.join('.serverless', 'func1.zip');

return expect(module.copyExistingArtifacts()).to.be.fulfilled.then(() =>
Expand Down