From 14ea1af886496fac53d4aaffe009ae78873c81bb Mon Sep 17 00:00:00 2001 From: Mariusz Nowak Date: Tue, 2 Mar 2021 12:16:11 +0100 Subject: [PATCH] refactor(Variables): Do not handle resolution when no vars to resolve --- scripts/serverless.js | 158 +++++++++++++++++++++--------------------- 1 file changed, 80 insertions(+), 78 deletions(-) diff --git a/scripts/serverless.js b/scripts/serverless.js index 3f861dc2b..9bc56b6ec 100755 --- a/scripts/serverless.js +++ b/scripts/serverless.js @@ -98,97 +98,99 @@ const processSpanPromise = (async () => { }; variablesMeta = resolveVariablesMeta(configuration); - if (variablesMeta.has('variablesResolutionMode')) { - throw new ServerlessError( - `Cannot resolve ${path.basename( - configurationPath - )}: "variablesResolutionMode" is not accessible ` + - '(configured behind variables which cannot be resolved at this stage)' - ); - } - - await resolveVariables({ - servicePath: process.cwd(), - configuration, - variablesMeta, - sources: variableSources, - options, - }); - const resolutionErrors = new Set( - Array.from(variablesMeta.values(), ({ error }) => error).filter(Boolean) - ); - - // If non-recoverable errors were approached in variable resolution, report them - if (resolutionErrors.size) { - if (isHelpRequest) { - const log = require('@serverless/utils/log'); - log( - 'Resolution of service configuration failed when resolving variables: ' + - `${Array.from(resolutionErrors, (error) => `\n - ${error.message}`)}\n`, - { color: 'orange' } - ); - } else { - if (configuration.variablesResolutionMode) { - throw new ServerlessError( - `Cannot resolve ${path.basename( - configurationPath - )}: Variables resolution errored with:${Array.from( - resolutionErrors, - (error) => `\n - ${error.message}` - )}\n` - ); - } - logDeprecation( - 'NEW_VARIABLES_RESOLVER', - 'Variables resolver reports following resolution errors:' + - `${Array.from(resolutionErrors, (error) => `\n - ${error.message}`)}\n` + - 'From a next major it we will be communicated with a thrown error.\n' + - 'Set "variablesResolutionMode: 20210219" in your service config, ' + - 'to adapt to this behavior now', - { serviceConfig: configuration } - ); - // Hack to not duplicate the warning with similar deprecation - logDeprecation.triggeredDeprecations.add('VARIABLES_ERROR_ON_UNRESOLVED'); - } - } else if (!isHelpRequest) { - // There are few configuration properties, which have to be resolved at this point - // to move forward. Report errors if that's not the case - if (variablesMeta.has('provider')) { + if (variablesMeta.size) { + if (variablesMeta.has('variablesResolutionMode')) { throw new ServerlessError( `Cannot resolve ${path.basename( configurationPath - )}: "provider" section is not accessible ` + + )}: "variablesResolutionMode" is not accessible ` + '(configured behind variables which cannot be resolved at this stage)' ); } - if (variablesMeta.has('provider\0stage')) { - if (configuration.variablesResolutionMode) { + + await resolveVariables({ + servicePath: process.cwd(), + configuration, + variablesMeta, + sources: variableSources, + options, + }); + const resolutionErrors = new Set( + Array.from(variablesMeta.values(), ({ error }) => error).filter(Boolean) + ); + + // If non-recoverable errors were approached in variable resolution, report them + if (resolutionErrors.size) { + if (isHelpRequest) { + const log = require('@serverless/utils/log'); + log( + 'Resolution of service configuration failed when resolving variables: ' + + `${Array.from(resolutionErrors, (error) => `\n - ${error.message}`)}\n`, + { color: 'orange' } + ); + } else { + if (configuration.variablesResolutionMode) { + throw new ServerlessError( + `Cannot resolve ${path.basename( + configurationPath + )}: Variables resolution errored with:${Array.from( + resolutionErrors, + (error) => `\n - ${error.message}` + )}\n` + ); + } + logDeprecation( + 'NEW_VARIABLES_RESOLVER', + 'Variables resolver reports following resolution errors:' + + `${Array.from(resolutionErrors, (error) => `\n - ${error.message}`)}\n` + + 'From a next major it we will be communicated with a thrown error.\n' + + 'Set "variablesResolutionMode: 20210219" in your service config, ' + + 'to adapt to this behavior now', + { serviceConfig: configuration } + ); + // Hack to not duplicate the warning with similar deprecation + logDeprecation.triggeredDeprecations.add('VARIABLES_ERROR_ON_UNRESOLVED'); + } + } else if (!isHelpRequest) { + // There are few configuration properties, which have to be resolved at this point + // to move forward. Report errors if that's not the case + if (variablesMeta.has('provider')) { throw new ServerlessError( `Cannot resolve ${path.basename( configurationPath - )}: "provider.stage" is not accessible ` + + )}: "provider" section is not accessible ` + '(configured behind variables which cannot be resolved at this stage)' ); } - logDeprecation( - 'NEW_VARIABLES_RESOLVER', - '"provider.stage" is not accessible ' + - '(configured behind variables which cannot be resolved at this stage).\n' + - 'Starting with next major release, ' + - 'this will be communicated with a thrown error.\n' + - 'Set "variablesResolutionMode: 20210219" in your service config, ' + - 'to adapt to this behavior now', - { serviceConfig: configuration } - ); - // Hack to not duplicate the warning with similar deprecation - logDeprecation.triggeredDeprecations.add('VARIABLES_ERROR_ON_UNRESOLVED'); + if (variablesMeta.has('provider\0stage')) { + if (configuration.variablesResolutionMode) { + throw new ServerlessError( + `Cannot resolve ${path.basename( + configurationPath + )}: "provider.stage" is not accessible ` + + '(configured behind variables which cannot be resolved at this stage)' + ); + } + logDeprecation( + 'NEW_VARIABLES_RESOLVER', + '"provider.stage" is not accessible ' + + '(configured behind variables which cannot be resolved at this stage).\n' + + 'Starting with next major release, ' + + 'this will be communicated with a thrown error.\n' + + 'Set "variablesResolutionMode: 20210219" in your service config, ' + + 'to adapt to this behavior now', + { serviceConfig: configuration } + ); + // Hack to not duplicate the warning with similar deprecation + logDeprecation.triggeredDeprecations.add('VARIABLES_ERROR_ON_UNRESOLVED'); + } + } + if (variablesMeta.has('useDotenv')) { + throw new ServerlessError( + `Cannot resolve ${path.basename(configurationPath)}: "useDotenv" is not accessible ` + + '(configured behind variables which cannot be resolved at this stage)' + ); } - } - if (variablesMeta.has('useDotenv')) { - throw new ServerlessError( - `Cannot resolve ${path.basename(configurationPath)}: "useDotenv" is not accessible ` + - '(configured behind variables which cannot be resolved at this stage)' - ); } }