From 92a79975c1bd8ca7ca9200802f301cab5666736e Mon Sep 17 00:00:00 2001 From: Ansgar Mertens Date: Thu, 18 Jan 2024 16:27:15 +0100 Subject: [PATCH] feat(provider-generator): Ensure we have unique names for modules Resolves #3250 --- cdk.tf/vercel.json | 5 +++++ packages/@cdktf/provider-schema/src/read.ts | 23 +++++++++++++++++++++ packages/cdktf-cli/src/bin/cmds/ui/get.tsx | 7 ++++++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/cdk.tf/vercel.json b/cdk.tf/vercel.json index c865e4eebb..9cb32d8533 100644 --- a/cdk.tf/vercel.json +++ b/cdk.tf/vercel.json @@ -218,6 +218,11 @@ "destination": "https://developer.hashicorp.com/terraform/cdktf/concepts/modules#configure-modules", "permanent": false }, + { + "source": "/adding-modules", + "destination": "https://developer.hashicorp.com/terraform/cdktf/concepts/modules#add-module-to-cdktf-json", + "permanent": false + }, { "source": "/registry-providers", "destination": "https://registry.terraform.io/browse/providers", diff --git a/packages/@cdktf/provider-schema/src/read.ts b/packages/@cdktf/provider-schema/src/read.ts index 1a5e0188ab..9968cfb290 100644 --- a/packages/@cdktf/provider-schema/src/read.ts +++ b/packages/@cdktf/provider-schema/src/read.ts @@ -13,6 +13,8 @@ import { TerraformDependencyConstraint, LANGUAGES, ConstructsMakerProviderTarget, + ConstructsMakerModuleTarget, + Errors, } from "@cdktf/commons"; import deepmerge from "deepmerge"; import { readModuleSchema, readProviderSchema } from "./provider-schema"; @@ -31,6 +33,9 @@ export async function readSchema( const targets = constraints.map((constraint) => ConstructsMakerProviderTarget.from(constraint, LANGUAGES[0]) ); + + throwIfTargetsConflict(targets); + const schemas = await Promise.all( targets.map((t) => t.isModule @@ -53,3 +58,21 @@ export async function readSchema( return deepmerge.all(schemas); } + +function throwIfTargetsConflict( + targets: (ConstructsMakerProviderTarget | ConstructsMakerModuleTarget)[] +) { + const modules = targets.filter( + (t) => t.isModule + ) as ConstructsMakerModuleTarget[]; + + modules.forEach((moduleA) => { + modules.forEach((moduleB) => { + if (moduleA !== moduleB && moduleA.name === moduleB.name) { + throw Errors.Usage( + `Found two modules with the same name "${moduleA.name}" which is not supported. Please rename one of the modules in your cdktf.json config. For more information on how to set the name refer to https://cdk.tf/adding-modules` + ); + } + }); + }); +} diff --git a/packages/cdktf-cli/src/bin/cmds/ui/get.tsx b/packages/cdktf-cli/src/bin/cmds/ui/get.tsx index 0a31366bab..7598a20b35 100644 --- a/packages/cdktf-cli/src/bin/cmds/ui/get.tsx +++ b/packages/cdktf-cli/src/bin/cmds/ui/get.tsx @@ -9,6 +9,7 @@ import { Text, Box, useApp, Newline } from "ink"; import Spinner from "ink-spinner"; import { GetOptions } from "@cdktf/provider-generator"; import { + IsErrorType, Language, sendTelemetry, TerraformDependencyConstraint, @@ -61,7 +62,11 @@ export const Get = ({ }), }); } catch (e: any) { - console.error(e); + // No stack trace for usage errors, as they explain themselves + if (!IsErrorType(e, "Usage")) { + console.error(e); + } + exit(new Error(e)); } };