Skip to content

Commit

Permalink
fix(terraform): return early on parse errors and do not fail on check…
Browse files Browse the repository at this point in the history
…list false positive (#19778)

Closes #19777
  • Loading branch information
secustor committed Jan 11, 2023
1 parent 51107e3 commit e02ad9e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/modules/manager/terraform/extract.spec.ts
Expand Up @@ -708,5 +708,16 @@ describe('modules/manager/terraform/extract', () => {
},
]);
});

it('return null if invalid HCL file', async () => {
const res = await extractPackageFile(
`
resource my provider
`,
'tfeWorkspace.tf',
{}
);
expect(res).toBeNull();
});
});
});
5 changes: 5 additions & 0 deletions lib/modules/manager/terraform/extract.ts
@@ -1,3 +1,4 @@
import is from '@sindresorhus/is';
import { logger } from '../../../logger';
import type { ExtractConfig, PackageFile } from '../types';
import { resourceExtractors } from './extractors';
Expand Down Expand Up @@ -37,6 +38,10 @@ export async function extractPackageFile(

const dependencies = [];
const hclMap = hcl.parseHCL(content);
if (is.nullOrUndefined(hclMap)) {
logger.trace({ fileName }, 'failed to parse HCL file');
return null;
}

const locks = await extractLocksForPackageFile(fileName);

Expand Down
10 changes: 10 additions & 0 deletions lib/modules/manager/terraform/extractors/others/providers.spec.ts
@@ -0,0 +1,10 @@
import { ProvidersExtractor } from './providers';

describe('modules/manager/terraform/extractors/others/providers', () => {
const extractor = new ProvidersExtractor();

it('return null if no provider returned', () => {
const result = extractor.extract({}, []);
expect(result).toBeArrayOfSize(0);
});
});
Expand Up @@ -9,7 +9,7 @@ export class ProvidersExtractor extends TerraformProviderExtractor {
}

extract(hclRoot: any, locks: ProviderLock[]): PackageDependency[] {
const providerTypes = hclRoot.provider;
const providerTypes = hclRoot?.provider;
if (is.nullOrUndefined(providerTypes)) {
return [];
}
Expand Down

0 comments on commit e02ad9e

Please sign in to comment.