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

build: switch hcl parser #21988

Merged
merged 2 commits into from May 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion lib/modules/manager/terraform/extract.ts
Expand Up @@ -37,7 +37,7 @@ export async function extractPackageFile(
);

const dependencies = [];
const hclMap = hcl.parseHCL(content);
const hclMap = await hcl.parseHCL(content, fileName);
if (is.nullOrUndefined(hclMap)) {
logger.trace({ fileName }, 'failed to parse HCL file');
return null;
Expand Down
12 changes: 6 additions & 6 deletions lib/modules/manager/terraform/hcl/index.spec.ts
Expand Up @@ -8,8 +8,8 @@ const lockedVersion = Fixtures.get('lockedVersion.tf');

describe('modules/manager/terraform/hcl/index', () => {
describe('parseHCL()', () => {
it('should return flat modules', () => {
const res = parseHCL(modulesTF);
it('should return flat modules', async () => {
const res = await parseHCL(modulesTF, 'file.tf');
expect(res?.module).toBeDefined();
expect(Object.keys(res!.module!)).toBeArrayOfSize(6);
expect(res).toMatchObject({
Expand Down Expand Up @@ -50,8 +50,8 @@ describe('modules/manager/terraform/hcl/index', () => {
});
});

it('should return nested terraform block', () => {
const res = parseHCL(lockedVersion);
it('should return nested terraform block', async () => {
const res = await parseHCL(lockedVersion, 'file.tf');
expect(res).toMatchObject({
terraform: [
{
Expand All @@ -67,8 +67,8 @@ describe('modules/manager/terraform/hcl/index', () => {
});
});

it('should return resource blocks', () => {
const res = parseHCL(resourcesTF);
it('should return resource blocks', async () => {
const res = await parseHCL(resourcesTF, 'file.tf');
expect(res).toMatchObject({
resource: {
docker_container: {
Expand Down
10 changes: 6 additions & 4 deletions lib/modules/manager/terraform/hcl/index.ts
@@ -1,10 +1,12 @@
import * as hcl_parser from 'hcl2-parser';

import { parse } from '@cdktf/hcl2json';
import type { TerraformDefinitionFile } from './types';

export function parseHCL(content: string): TerraformDefinitionFile | null {
export async function parseHCL(
content: string,
fileName: string
): Promise<TerraformDefinitionFile | null> {
try {
return hcl_parser.parseToObject(content)[0];
return await parse(fileName, content);
} catch (err) /* istanbul ignore next */ {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -142,6 +142,7 @@
"@aws-sdk/client-rds": "3.314.0",
"@aws-sdk/client-s3": "3.312.0",
"@breejs/later": "4.1.0",
"@cdktf/hcl2json": "0.16.1",
"@cheap-glitch/mi-cron": "1.0.1",
"@iarna/toml": "3.0.0",
"@opentelemetry/api": "1.4.1",
Expand Down Expand Up @@ -198,7 +199,6 @@
"graph-data-structure": "3.3.0",
"handlebars": "4.7.7",
"hasha": "5.2.2",
"hcl2-parser": "1.0.3",
"ignore": "5.2.4",
"ini": "4.1.0",
"js-yaml": "4.1.0",
Expand Down
12 changes: 7 additions & 5 deletions yarn.lock
Expand Up @@ -1525,6 +1525,13 @@
resolved "https://registry.yarnpkg.com/@breejs/later/-/later-4.1.0.tgz#9246907f46cc9e9c9af37d791ab468d98921bcc1"
integrity sha512-QgGnZ9b7o4k0Ai1ZbTJWwZpZcFK9d+Gb+DyNt4UT9x6IEIs5HVu0iIlmgzGqN+t9MoJSpSPo9S/Mm51UtHr3JA==

"@cdktf/hcl2json@0.16.1":
version "0.16.1"
resolved "https://registry.yarnpkg.com/@cdktf/hcl2json/-/hcl2json-0.16.1.tgz#c5d4c965e150d29579d6b53a9fe6c946fca7e614"
integrity sha512-c0AcAzpUYREmkonxai3Jjd7P0c0/TMB+ga3Ir/02MDSdyV8VQ+1CLK2nVMJJqJTsGZkxrZGVj/jlwA6coDYaqg==
dependencies:
fs-extra "^11.1.1"

"@cheap-glitch/mi-cron@1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@cheap-glitch/mi-cron/-/mi-cron-1.0.1.tgz#111f4ce746c269aedf74533ac806881763a68f99"
Expand Down Expand Up @@ -5809,11 +5816,6 @@ hasha@5.2.2:
is-stream "^2.0.0"
type-fest "^0.8.0"

hcl2-parser@1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/hcl2-parser/-/hcl2-parser-1.0.3.tgz#096d0ff5a3c46707ace54fcb7571317f5828ff0e"
integrity sha512-NQUm/BFF+2nrBfeqDhhsy4DxxiLHgkeE3FywtjFiXnjSUaio3w4Tz1MQ3vGJBUhyArzOXJ24pO7JwE5LAn7Ncg==

he@1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/he/-/he-1.2.0.tgz#84ae65fa7eafb165fddb61566ae14baf05664f0f"
Expand Down