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 external modules version for transitive dependencies #541

Merged
Merged
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
4 changes: 4 additions & 0 deletions lib/packExternalModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ function getProdModules(externalModules, packagePath, dependencyGraph, forceExcl
// Add transient dependencies if they appear not in the service's dev dependencies
const originInfo = _.get(dependencyGraph, 'dependencies', {})[module.origin] || {};
moduleVersion = _.get(_.get(originInfo, 'dependencies', {})[module.external], 'version');
if (!moduleVersion) {
// eslint-disable-next-line lodash/path-style
moduleVersion = _.get(dependencyGraph, [ 'dependencies', module.external, 'version' ]);
}
if (!moduleVersion) {
this.serverless.cli.log(`WARNING: Could not determine version of module ${module.external}`);
}
Expand Down
22 changes: 22 additions & 0 deletions tests/data/package-transitive.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "comp-a",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"comp-b": "file:../comp-b",
"comp-c": "file:../comp-c"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"serverless": "^1.53.0",
"serverless-webpack": "^5.3.1",
"webpack": "^4.41.0",
"webpack-node-externals": "^1.7.2"
}
}
88 changes: 88 additions & 0 deletions tests/packExternalModules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1350,5 +1350,93 @@ describe('packExternalModules', () => {
});
});
});

describe('transitive dependencies', () => {
before(() => {
const transitivePackageJson = require('./data/package-transitive.json');
mockery.deregisterMock(path.join(process.cwd(), 'package.json'));
mockery.registerMock(path.join(process.cwd(), 'package.json'), transitivePackageJson);
});

after(() => {
mockery.deregisterMock(path.join(process.cwd(), 'package.json'));
mockery.registerMock(path.join(process.cwd(), 'package.json'), packageMock);
});

it('should install transitive dependencies', () => {
const expectedCompositePackageJSON = {
name: 'test-service',
version: '1.0.0',
description: 'Packaged externals for test-service',
private: true,
scripts: {},
dependencies: {
classnames: '2.2.6'
}
};
const expectedPackageJSON = {
name: 'test-service',
version: '1.0.0',
description: 'Packaged externals for test-service',
private: true,
scripts: {},
dependencies: {
classnames: '2.2.6'
}
};

const dependencyGraph = {
problems: [],
dependencies: {
'comp-b': { version: '1.0.0', dependencies: {} },
'comp-c': { version: '1.0.0', dependencies: {} },
classnames: { version: '2.2.6', dependencies: {} }
}
};

const transitiveDepStats = {
stats: [
{
compilation: {
chunks: [
new ChunkMock([
{
identifier: _.constant('external "classnames"')
}
])
],
compiler: {
outputPath: '/my/Service/Path/.webpack/service'
}
}
}
]
};

module.webpackOutputPath = 'outputPath';
fsExtraMock.pathExists.yields(null, false);
fsExtraMock.copy.yields();
packagerMock.getProdDependencies.returns(BbPromise.resolve(dependencyGraph));
packagerMock.install.returns(BbPromise.resolve());
packagerMock.prune.returns(BbPromise.resolve());
packagerMock.runScripts.returns(BbPromise.resolve());
module.compileStats = transitiveDepStats;
return expect(module.packExternalModules()).to.be.fulfilled.then(() =>
BbPromise.all([
// The module package JSON and the composite one should have been stored
expect(writeFileSyncStub).to.have.been.calledTwice,
expect(writeFileSyncStub.firstCall.args[1]).to.equal(JSON.stringify(expectedCompositePackageJSON, null, 2)),
expect(writeFileSyncStub.secondCall.args[1]).to.equal(JSON.stringify(expectedPackageJSON, null, 2)),
// The modules should have been copied
expect(fsExtraMock.copy).to.have.been.calledOnce,
// npm ls and npm prune should have been called
expect(packagerMock.getProdDependencies).to.have.been.calledOnce,
expect(packagerMock.install).to.have.been.calledOnce,
expect(packagerMock.prune).to.have.been.calledOnce,
expect(packagerMock.runScripts).to.have.been.calledOnce
])
);
});
});
});
});