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

feat(provider-generator): Ensure we have unique names for modules #3439

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cdk.tf/vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 23 additions & 0 deletions packages/@cdktf/provider-schema/src/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
TerraformDependencyConstraint,
LANGUAGES,
ConstructsMakerProviderTarget,
ConstructsMakerModuleTarget,
Errors,
} from "@cdktf/commons";
import deepmerge from "deepmerge";
import { readModuleSchema, readProviderSchema } from "./provider-schema";
Expand All @@ -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
Expand All @@ -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`
);
}
});
});
}
7 changes: 6 additions & 1 deletion packages/cdktf-cli/src/bin/cmds/ui/get.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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));
}
};
Expand Down
Loading