Riff-Raff YAML generator doesn't always handle multiple instances of the same `GuStack`
- Dominant language
- TypeScript
- Stars
- 22
- Forks
- 6
- Avg merge
- 1d 17h
- Merged PRs (30d)
- 9
Description
cc @richpryce
In https://github.com/guardian/datasync-notifier, we're instantiating the same `GuStack` class twice, to create two CloudFormation stacks. They differ only by the `app` property.
The Riff-Raff generator fails to correctly produce a file in this scenario:
```ts
it("should create the correct riff-raff.yaml with multiple uses of the same class", () => {
const app = new App({ outdir: "/tmp/cdk.out" });
interface StackWithLambdaProps extends GuStackProps {
app: string;
}
class StackWithLambda extends GuStack {
// eslint-disable-next-line custom-rules/valid-constructors -- this is a test
constructor(app: App, id: string, props: StackWithLambdaProps) {
super(app, id, props);
new GuLambdaFunction(this, "test", {
app: props.app,
runtime: Runtime.NODEJS_20_X,
fileName: `${props.app}.zip`,
handler: "handler.main",
timeout: Duration.minutes(1),
});
}
}
new StackWithLambda(app, "test-stack-SYD", {
stack: "test",
stage: "TEST",
app: "my-lambda-SYD",
env: { region: "eu-west-1" },
});
new StackWithLambda(app, "test-stack-NYC", {
stack: "test",
stage: "TEST",
app: "my-lambda-NYC",
env: { region: "eu-west-1" },
});
const actual = new RiffRaffYamlFile(app).toYAML();
expect(actual).toMatchSnapshot();
});
```
This is because of [how `GuStack`s are grouped](https://github.com/guardian/cdk/blob/a322c74f0b8f5b5bb2bdd5ce06314334e252877a/src/riff-raff-yaml-file/group-by.ts#L21-L41); as the class name is the same, the generator thinks there is only one `StackWithLambda`.
A work-around is to sub-class:
```ts
class StackWithLambda extends GuStack {
// eslint-disable-next-line custom-rules/valid-constructors -- this is a test
constructor(app: App, id: string, props: StackWithLambdaProps) {
super(app, id, props);
new GuLambdaFunction(this, "test", {
app: props.app,
runtime: Runtime.NODEJS_20_X,
fileName: `${props.app}.zip`,
handler: "handler.main",
timeout: Duration.minutes(1),
});
}
}
class StackOne extends StackWithLambda {}
class StackTwo extends StackWithLambda {}
```
Should there be better first-class support for this?
Contributor guide
Assessment
This issue has not been assessed yet.