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

Fallback to service local serverless installation by default #8180

Merged
merged 2 commits into from Sep 3, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/deprecations.md
Expand Up @@ -6,6 +6,12 @@ layout: Doc

# Serverless Framework Deprecations

<a name="DISABLE_LOCAL_INSTALLATION_FALLBACK_SETTING"><div>&nbsp;</div></a>

## Support for `enableLocalInstallationFallback` setting is to be removed

Starting with v3.0.0, framework will unconditionally run service local installation of `serverless` if its found.

<a name="LOCAL_INSTALLATION_FALLBACK"><div>&nbsp;</div></a>

## Fallback to service local `serverless` installation
Expand Down
70 changes: 38 additions & 32 deletions lib/Serverless.js
Expand Up @@ -116,40 +116,46 @@ class Serverless {
});
});
}
eventuallyFallbackToLocal() {
return resolve(process.cwd(), 'serverless').then(
({ realPath }) => {
if (realPath === __filename) {
this.isLocallyInstalled = true;
return null;
}
if (!this.pluginManager.serverlessConfigFile) return null;
if (this.pluginManager.serverlessConfigFile.enableLocalInstallationFallback == null) {
this._logDeprecation(
'LOCAL_INSTALLATION_FALLBACK',
'Local installation of Serverless detected. Starting with next major version, CLI ' +
'will run it instead of globally installed version.\n' +
'Set "enableLocalInstallationFallback" to "true" to switch to new behavior now, ' +
'set to "false" to keep current behavior and hide this message'
);
return null;
}
if (!this.pluginManager.serverlessConfigFile.enableLocalInstallationFallback) {
return null;
}
this.cli.log('Running "serverless" installed locally (in service node_modules)');
// TODO: Replace below fallback logic with more straightforward one at top of the CLI
// when we willl drop support for the "disableLocalInstallationFallback" setting
this.isOverridenByLocal = true;
const ServerlessLocal = require(realPath);
const serverlessLocal = new ServerlessLocal();
this.invokedInstance = serverlessLocal;
return serverlessLocal.init();
},
error => {
async eventuallyFallbackToLocal() {
if (
this.pluginManager.serverlessConfigFile &&
this.pluginManager.serverlessConfigFile.enableLocalInstallationFallback != null
) {
this._logDeprecation(
'DISABLE_LOCAL_INSTALLATION_FALLBACK_SETTING',
'Starting with next major version, "enableLocalInstallationFallback" setting will no longer be supported.' +
'CLI will unconditionally fallback to service local installation when its found.\n' +
'Remove this setting to clear this deprecation warning'
);
}
const localServerlessPath = await (async () => {
try {
return (await resolve(process.cwd(), 'serverless')).realPath;
} catch (error) {
if (!isModuleNotFoundError(error, 'serverless')) throw error;
return null;
}
);
})();
if (!localServerlessPath) return;
if (localServerlessPath === __filename) {
this.isLocallyInstalled = true;
return;
}
if (
this.pluginManager.serverlessConfigFile &&
this.pluginManager.serverlessConfigFile.enableLocalInstallationFallback != null &&
!this.pluginManager.serverlessConfigFile.enableLocalInstallationFallback
) {
return;
}
this.cli.log('Running "serverless" installed locally (in service node_modules)');
// TODO: Replace below fallback logic with more straightforward one at top of the CLI
// when we willl drop support for the "disableLocalInstallationFallback" setting
this.isOverridenByLocal = true;
const ServerlessLocal = require(localServerlessPath);
const serverlessLocal = new ServerlessLocal();
this.invokedInstance = serverlessLocal;
await serverlessLocal.init();
}

run() {
Expand Down
27 changes: 18 additions & 9 deletions lib/Serverless.test.js
Expand Up @@ -307,28 +307,37 @@ describe('Serverless [new tests]', () => {
after(() => fse.remove(path.resolve(fixtures.map.aws, 'node_modules')));

describe('When running global version', () => {
it('Should report deprecation notice when "enableLocalInstallationFallback" not set', () =>
it('Should fallback to local version when it is found and "enableLocalInstallationFallback" is not set', () =>
runServerless({ cwd: fixtures.map.aws, cliArgs: ['-v'] }).then(({ serverless }) => {
expect(Array.from(serverless.triggeredDeprecations)).to.deep.equal([
'LOCAL_INSTALLATION_FALLBACK',
]);
expect(serverless.invokedInstance).to.not.exist;
expect(Array.from(serverless.triggeredDeprecations)).to.deep.equal([]);
expect(serverless.invokedInstance.isLocalStub).to.be.true;
}));
it('Should run without notice when "enableLocalInstallationFallback" set to false', () =>

let serverlessWithDisabledLocalInstallationFallback;
it('Should report deprecation notice when "enableLocalInstallationFallback" is set', () =>
fixtures.extend('aws', { enableLocalInstallationFallback: false }).then(fixturePath =>
runServerless({ cwd: fixturePath, cliArgs: ['-v'] }).then(({ serverless }) => {
expect(Array.from(serverless.triggeredDeprecations)).to.deep.equal([]);
expect(serverless.invokedInstance).to.not.exist;
serverlessWithDisabledLocalInstallationFallback = serverless;
expect(Array.from(serverless.triggeredDeprecations)).to.deep.equal([
'DISABLE_LOCAL_INSTALLATION_FALLBACK_SETTING',
]);
})
));

it('Should not fallback to local when "enableLocalInstallationFallback" set to false', () =>
expect(serverlessWithDisabledLocalInstallationFallback.invokedInstance).to.not.exist);

it('Should fallback to local version when "enableLocalInstallationFallback" set to true', () =>
fixtures.extend('aws', { enableLocalInstallationFallback: true }).then(fixturePath =>
runServerless({ cwd: fixturePath, cliArgs: ['-v'] }).then(({ serverless }) => {
expect(Array.from(serverless.triggeredDeprecations)).to.deep.equal([]);
expect(Array.from(serverless.triggeredDeprecations)).to.deep.equal([
'DISABLE_LOCAL_INSTALLATION_FALLBACK_SETTING',
]);
expect(serverless.invokedInstance.isLocalStub).to.be.true;
})
));
});

describe('When running local version', () => {
it('Should run without notice', () =>
runServerless({
Expand Down