Skip to content
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
10 changes: 10 additions & 0 deletions docs/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ layout: Doc

To disable specific deprecations set `SLS_DEPRECATION_DISABLE` environment variable. Setting `SLS_DEPRECATION_DISABLE=*` will disable all deprecations. If you want to disable specific deprecations set `SLS_DEPRECATION_DISABLE=CODE1,CODE2`.

<a name="VARIABLES_ERROR_ON_UNRESOLVED"><div>&nbsp;</div></a>
Comment thread
G-Rath marked this conversation as resolved.

## Erroring on unresolved variable references

Deprecation code: `VARIABLES_ERROR_ON_UNRESOLVED`

Starting with v3.0.0, references to variables that cannot be resolved will result in an error being thrown.

Adapt to this behaviour now by adding `unresolvedVariablesNotificationMode: error` to service configuration.

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

## API Gateway specific configuration
Expand Down
1 change: 1 addition & 0 deletions docs/providers/aws/guide/serverless.yml.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ service: myService
frameworkVersion: '2'
enableLocalInstallationFallback: false # If set to 'true', guarantees that it's a locally (for service, in its node_modules) installed framework which processes the command
useDotenv: false # If set to 'true', environment variables will be automatically loaded from .env files
unresolvedVariablesNotificationMode: warn # If set to 'error', references to variables that cannot be resolved will result in an error being thrown

disabledDeprecations: # Disable deprecation logs by their codes. Default is empty.
- DEP_CODE_1 # Deprecation code to disable
Expand Down
3 changes: 3 additions & 0 deletions docs/providers/aws/guide/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Variables allow users to dynamically replace config values in `serverless.yml` c

They are especially useful when providing secrets for your service to use and when you are working with multiple stages.

If `unresolvedVariablesNotificationMode` is set to `error`, references to variables that cannot be resolved will result in an error being thrown.
This will become the default behaviour in the next major version.

## Syntax

To use variables, you will need to reference values enclosed in `${}` brackets.
Expand Down
1 change: 1 addition & 0 deletions lib/classes/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ class Service {
this.functions = serverlessFile.functions || {};
this.configValidationMode = serverlessFile.configValidationMode || 'warn';
this.disabledDeprecations = serverlessFile.disabledDeprecations;
this.unresolvedVariablesNotificationMode = serverlessFile.unresolvedVariablesNotificationMode;

// merge so that the default settings are still in place and
// won't be overwritten
Expand Down
66 changes: 43 additions & 23 deletions lib/classes/Variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ class Variables {
renderMatches(value, matches, results) {
let result = value;
for (let i = 0; i < matches.length; i += 1) {
this.warnIfNotFound(matches[i].variable, results[i]);
this.handleUnresolved(matches[i].variable, results[i]);
result = this.populateVariable(result, matches[i].match, results[i]);
}
return result;
Expand Down Expand Up @@ -962,33 +962,53 @@ class Variables {
);
}

warnIfNotFound(variableString, valueToPopulate) {
if (
handleUnresolved(variableString, valueToPopulate) {
const isValueEmpty =
valueToPopulate === null ||
typeof valueToPopulate === 'undefined' ||
(typeof valueToPopulate === 'object' &&
!Array.isArray(valueToPopulate) &&
!Object.keys(valueToPopulate).length)
) {
let varType;
if (variableString.match(this.envRefSyntax)) {
varType = 'environment variable';
} else if (variableString.match(this.optRefSyntax)) {
varType = 'option';
} else if (variableString.match(this.selfRefSyntax)) {
varType = 'service attribute';
} else if (variableString.match(this.fileRefSyntax)) {
varType = 'file';
} else if (variableString.match(this.ssmRefSyntax)) {
varType = 'SSM parameter';
}
if (varType) {
logWarning(
`A valid ${varType} to satisfy the declaration '${variableString}' could not be found.`
);
}
!Object.keys(valueToPopulate).length);

if (!isValueEmpty) {
return;
}

let varType;

if (variableString.match(this.envRefSyntax)) {
varType = 'environment variable';
} else if (variableString.match(this.optRefSyntax)) {
varType = 'option';
} else if (variableString.match(this.selfRefSyntax)) {
varType = 'service attribute';
} else if (variableString.match(this.fileRefSyntax)) {
varType = 'file';
} else if (variableString.match(this.ssmRefSyntax)) {
varType = 'SSM parameter';
}

if (!varType) {
return;
}
return valueToPopulate;

if (this.service.unresolvedVariablesNotificationMode === 'error') {
throw new this.serverless.classes.Error(
`A valid ${varType} to satisfy the declaration '${variableString}' could not be found.`,
'UNRESOLVED_CONFIG_VARIABLE'
);
}

if (!this.service.unresolvedVariablesNotificationMode) {
this.serverless._logDeprecation(
'VARIABLES_ERROR_ON_UNRESOLVED',
'Unresolved variable references will be communicated with a thrown error, starting from next major'
);
}

logWarning(
`A valid ${varType} to satisfy the declaration '${variableString}' could not be found.`
);
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/configSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ const schema = {
},
],
},
unresolvedVariablesNotificationMode: { enum: ['error', 'warn'] },
Comment thread
G-Rath marked this conversation as resolved.
useDotenv: { const: true },
},
additionalProperties: false,
Expand Down
Loading