From 582fafb5199cc66ca9800da91bf753fe69bc54d9 Mon Sep 17 00:00:00 2001 From: Ansgar Mertens Date: Mon, 13 Feb 2023 11:22:52 +0100 Subject: [PATCH 1/2] fix(provider-generator): fix resources named 'license' causing collisions with LICENSE file on case-insensitive filesystems for Go packages Resolves #2627 --- .../__snapshots__/types.test.ts.snap | 88 +++++++++++++++++++ ...ompatible-resource-names.test.fixture.json | 12 +++ .../lib/get/__tests__/generator/types.test.ts | 1 + .../lib/get/generator/resource-parser.ts | 6 ++ 4 files changed, 107 insertions(+) diff --git a/packages/@cdktf/provider-generator/lib/get/__tests__/generator/__snapshots__/types.test.ts.snap b/packages/@cdktf/provider-generator/lib/get/__tests__/generator/__snapshots__/types.test.ts.snap index 13615e7938..451471b494 100644 --- a/packages/@cdktf/provider-generator/lib/get/__tests__/generator/__snapshots__/types.test.ts.snap +++ b/packages/@cdktf/provider-generator/lib/get/__tests__/generator/__snapshots__/types.test.ts.snap @@ -1902,10 +1902,98 @@ export * as objectResource from './object-resource'; export * as functionResource from './function-resource'; export * as staticResource from './static-resource'; export * as providerResource from './provider-resource'; +export * as licenseResource from './license-resource'; " `; +exports[`incompatible resource names: license-resource 1`] = ` +"// https://www.terraform.io/docs/providers/test/r/license +// generated from terraform resource schema + +import { Construct } from 'constructs'; +import * as cdktf from 'cdktf'; + +// Configuration + +export interface LicenseResourceConfig extends cdktf.TerraformMetaArguments { + /** + * Docs at Terraform Registry: {@link https://www.terraform.io/docs/providers/test/r/license#dummy LicenseResource#dummy} + */ + readonly dummy?: string; +} + +/** +* Represents a {@link https://www.terraform.io/docs/providers/test/r/license test_license} +*/ +export class LicenseResource extends cdktf.TerraformResource { + + // ================= + // STATIC PROPERTIES + // ================= + public static readonly tfResourceType = \\"test_license\\"; + + // =========== + // INITIALIZER + // =========== + + /** + * Create a new {@link https://www.terraform.io/docs/providers/test/r/license test_license} Resource + * + * @param scope The scope in which to define this construct + * @param id The scoped construct ID. Must be unique amongst siblings in the same scope + * @param options LicenseResourceConfig = {} + */ + public constructor(scope: Construct, id: string, config: LicenseResourceConfig = {}) { + super(scope, id, { + terraformResourceType: 'test_license', + terraformGeneratorMetadata: { + providerName: 'test' + }, + provider: config.provider, + dependsOn: config.dependsOn, + count: config.count, + lifecycle: config.lifecycle, + provisioners: config.provisioners, + connection: config.connection, + forEach: config.forEach + }); + this._dummy = config.dummy; + } + + // ========== + // ATTRIBUTES + // ========== + + // dummy - computed: false, optional: true, required: false + private _dummy?: string; + public get dummy() { + return this.getStringAttribute('dummy'); + } + public set dummy(value: string) { + this._dummy = value; + } + public resetDummy() { + this._dummy = undefined; + } + // Temporarily expose input value. Use with caution. + public get dummyInput() { + return this._dummy; + } + + // ========= + // SYNTHESIS + // ========= + + protected synthesizeAttributes(): { [name: string]: any } { + return { + dummy: cdktf.stringToTerraform(this._dummy), + }; + } +} +" +`; + exports[`incompatible resource names: object-resource 1`] = ` "// https://registry.terraform.io/providers/hashicorp/test/latest/docs/resources/object // generated from terraform resource schema diff --git a/packages/@cdktf/provider-generator/lib/get/__tests__/generator/fixtures/incompatible-resource-names.test.fixture.json b/packages/@cdktf/provider-generator/lib/get/__tests__/generator/fixtures/incompatible-resource-names.test.fixture.json index 9acde680c3..517ab85cf8 100644 --- a/packages/@cdktf/provider-generator/lib/get/__tests__/generator/fixtures/incompatible-resource-names.test.fixture.json +++ b/packages/@cdktf/provider-generator/lib/get/__tests__/generator/fixtures/incompatible-resource-names.test.fixture.json @@ -61,6 +61,18 @@ } }, "block_types": {} + }, + "test_license": { + "version": 1, + "block": { + "attributes": { + "dummy": { + "type": "string", + "optional": true + } + } + }, + "block_types": {} } } } diff --git a/packages/@cdktf/provider-generator/lib/get/__tests__/generator/types.test.ts b/packages/@cdktf/provider-generator/lib/get/__tests__/generator/types.test.ts index 7134c63ff6..bce4144710 100644 --- a/packages/@cdktf/provider-generator/lib/get/__tests__/generator/types.test.ts +++ b/packages/@cdktf/provider-generator/lib/get/__tests__/generator/types.test.ts @@ -435,6 +435,7 @@ test("incompatible resource names", async () => { [ "function-resource", "index.ts", + "license-resource", "object-resource", "provider-resource", "static-resource", diff --git a/packages/@cdktf/provider-generator/lib/get/generator/resource-parser.ts b/packages/@cdktf/provider-generator/lib/get/generator/resource-parser.ts index 994e157830..487eb7f657 100644 --- a/packages/@cdktf/provider-generator/lib/get/generator/resource-parser.ts +++ b/packages/@cdktf/provider-generator/lib/get/generator/resource-parser.ts @@ -43,12 +43,18 @@ const RESERVED_KEYWORDS_FOR_NAMESPACES = [ "await", ]; +const COLLIDING_NAMESPACE_NAMES = [ + // e.g. hashicorp/consul – collides with the LICENSE file on case-insensitive filesystems in the Go package (#2627) + "license", +]; + const isReservedClassOrNamespaceName = (className: string): boolean => { return [ "string", "object", "function", ...RESERVED_KEYWORDS_FOR_NAMESPACES, + ...COLLIDING_NAMESPACE_NAMES, ].includes(className.toLowerCase()); }; From 4aaf5f2ab9d7860c058f2ca07c502253fc9968ab Mon Sep 17 00:00:00 2001 From: Mutahhir Hayat Date: Tue, 18 Apr 2023 12:36:27 +0200 Subject: [PATCH 2/2] chore: Update snapshots --- .../generator/__snapshots__/types.test.ts.snap | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/@cdktf/provider-generator/lib/get/__tests__/generator/__snapshots__/types.test.ts.snap b/packages/@cdktf/provider-generator/lib/get/__tests__/generator/__snapshots__/types.test.ts.snap index 451471b494..3cc8f065cc 100644 --- a/packages/@cdktf/provider-generator/lib/get/__tests__/generator/__snapshots__/types.test.ts.snap +++ b/packages/@cdktf/provider-generator/lib/get/__tests__/generator/__snapshots__/types.test.ts.snap @@ -1908,7 +1908,7 @@ export * as licenseResource from './license-resource'; `; exports[`incompatible resource names: license-resource 1`] = ` -"// https://www.terraform.io/docs/providers/test/r/license +"// https://registry.terraform.io/providers/hashicorp/test/latest/docs/resources/license // generated from terraform resource schema import { Construct } from 'constructs'; @@ -1918,27 +1918,27 @@ import * as cdktf from 'cdktf'; export interface LicenseResourceConfig extends cdktf.TerraformMetaArguments { /** - * Docs at Terraform Registry: {@link https://www.terraform.io/docs/providers/test/r/license#dummy LicenseResource#dummy} + * Docs at Terraform Registry: {@link https://registry.terraform.io/providers/hashicorp/test/latest/docs/resources/license#dummy LicenseResource#dummy} */ readonly dummy?: string; } /** -* Represents a {@link https://www.terraform.io/docs/providers/test/r/license test_license} +* Represents a {@link https://registry.terraform.io/providers/hashicorp/test/latest/docs/resources/license test_license} */ export class LicenseResource extends cdktf.TerraformResource { // ================= // STATIC PROPERTIES // ================= - public static readonly tfResourceType = \\"test_license\\"; + public static readonly tfResourceType = "test_license"; // =========== // INITIALIZER // =========== /** - * Create a new {@link https://www.terraform.io/docs/providers/test/r/license test_license} Resource + * Create a new {@link https://registry.terraform.io/providers/hashicorp/test/latest/docs/resources/license test_license} Resource * * @param scope The scope in which to define this construct * @param id The scoped construct ID. Must be unique amongst siblings in the same scope