Skip to content

Commit 03ad487

Browse files
authored
fix(schemas): yaml schemas sometimes are not applied to cfn/sam files #2671
Problem: When loading cloudformation files, the validation checks the "Resources" field in the YAML and see if serverless function exists and then validate those resources, make sure handlers, codeUri, etc are available before loading in the schema which doesn't really make sense. This can cause the cloudformation.load() to fail, resulting in the schema not getting applied to the cfn/sam yaml files when it should be. Solution: Skip one of the validation steps. The net result is that now we only check that `AWSTemplateFormatVersion` or `Resources` fields exist.
1 parent b6aca5b commit 03ad487

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "Bug Fix",
3+
"description": "Fix issue where occasionally yaml schemas weren't getting loaded for cfn/sam"
4+
}

src/shared/cloudformation/cloudformation.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ export namespace CloudFormation {
365365
[key: string]: Resource | undefined
366366
}
367367

368-
export async function load(filename: string): Promise<Template> {
368+
export async function load(filename: string, validate: boolean = true): Promise<Template> {
369369
if (!(await SystemUtilities.fileExists(filename))) {
370370
throw new Error(`Template file not found: ${filename}`)
371371
}
@@ -374,7 +374,10 @@ export namespace CloudFormation {
374374
const template = yaml.load(templateAsYaml, {
375375
schema: schema as any,
376376
}) as Template
377-
validateTemplate(template)
377+
378+
if (validate) {
379+
validateTemplate(template)
380+
}
378381

379382
return template
380383
}

src/shared/cloudformation/templateRegistry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class CloudFormationTemplateRegistry extends WatchedFiles<CloudFormation.
2828

2929
let template: CloudFormation.Template | undefined
3030
try {
31-
template = await CloudFormation.load(path)
31+
template = await CloudFormation.load(path, false)
3232
} catch (e) {
3333
globals.schemaService.registerMapping({ path, type: 'yaml', schema: undefined })
3434
return undefined

src/test/shared/cloudformation/cloudformation.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,24 @@ describe('CloudFormation', function () {
151151
const template = await CloudFormation.load(filename)
152152
assert.strictEqual(template.Resources!['TestResource']?.Properties?.CodeUri, '')
153153
})
154+
155+
it('Loads invalid YAML when validation is disabled', async function () {
156+
// handler is missing
157+
const invalidYamlStr: string = `AWSTemplateFormatVersion: "2010-09-09"
158+
Transform: AWS::Serverless-2016-10-31
159+
Resources:
160+
TestResource:
161+
Type: ${CloudFormation.SERVERLESS_FUNCTION_TYPE}
162+
Properties:
163+
CodeUri: asdf
164+
Runtime: runtime
165+
Timeout: 1
166+
Environment:
167+
Variables:
168+
ENVVAR: envvar`
169+
await strToYamlFile(invalidYamlStr, filename)
170+
await assert.doesNotReject(CloudFormation.load(filename, false))
171+
})
154172
})
155173

156174
describe('save', async function () {

0 commit comments

Comments
 (0)