Skip to content

Commit 601a2c1

Browse files
authored
dedupe environment variables in amplify env type generator (#2153)
1 parent 7d74e8e commit 601a2c1

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

.changeset/silver-rocks-return.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@aws-amplify/backend-function': patch
3+
'@aws-amplify/backend': patch
4+
---
5+
6+
dedupe environment variables in amplify env type generator

packages/backend-function/src/function_env_type_generator.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,36 @@ void describe('FunctionEnvironmentTypeGenerator', () => {
6969

7070
await fsp.rm(targetDirectory, { recursive: true, force: true });
7171
});
72+
73+
void it('does not generate duplicate environment variables', () => {
74+
const fsOpenSyncMock = mock.method(fs, 'openSync');
75+
const fsWriteFileSyncMock = mock.method(fs, 'writeFileSync', () => null);
76+
fsOpenSyncMock.mock.mockImplementation(() => 0);
77+
const functionEnvironmentTypeGenerator =
78+
new FunctionEnvironmentTypeGenerator('testFunction');
79+
80+
functionEnvironmentTypeGenerator.generateTypedProcessEnvShim([
81+
'TEST_ENV',
82+
'TEST_ENV',
83+
'ANOTHER_ENV',
84+
]);
85+
86+
const generatedContent =
87+
fsWriteFileSyncMock.mock.calls[0].arguments[1]?.toString() ?? '';
88+
89+
// Check TEST_ENV appears only once
90+
assert.equal(
91+
(generatedContent.match(/TEST_ENV: string;/g) || []).length,
92+
1,
93+
'TEST_ENV should appear only once'
94+
);
95+
96+
// Check ANOTHER_ENV also appears
97+
assert.ok(
98+
generatedContent.includes('ANOTHER_ENV: string;'),
99+
'ANOTHER_ENV should be included'
100+
);
101+
102+
mock.restoreAll();
103+
});
72104
});

packages/backend-function/src/function_env_type_generator.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ export class FunctionEnvironmentTypeGenerator {
5757
`/** Amplify backend environment variables available at runtime, this includes environment variables defined in \`defineFunction\` and by cross resource mechanisms */`
5858
);
5959
declarations.push(`type ${amplifyBackendEnvVarTypeName} = {`);
60-
amplifyBackendEnvVars.forEach((envName) => {
60+
61+
// Use a Set to remove duplicates
62+
const uniqueEnvVars = new Set(amplifyBackendEnvVars);
63+
64+
uniqueEnvVars.forEach((envName) => {
6165
const declaration = `${this.indentation}${envName}: string;`;
6266

6367
declarations.push(declaration);

0 commit comments

Comments
 (0)