From 743354ee087beab1c2bf20f265f068b717d283cd Mon Sep 17 00:00:00 2001 From: vigy02 Date: Thu, 19 Sep 2024 00:03:45 -0700 Subject: [PATCH 01/33] fix: clearing the .amplify/generated/env/ before synthesis --- packages/backend/src/backend_factory.test.ts | 20 ++++++++++++++++++++ packages/backend/src/backend_factory.ts | 14 ++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/packages/backend/src/backend_factory.test.ts b/packages/backend/src/backend_factory.test.ts index 4016e50b78..2f9aaf8bd5 100644 --- a/packages/backend/src/backend_factory.test.ts +++ b/packages/backend/src/backend_factory.test.ts @@ -11,6 +11,7 @@ import { App, Stack } from 'aws-cdk-lib'; import { Template } from 'aws-cdk-lib/assertions'; import assert from 'node:assert'; import { ClientConfig } from '@aws-amplify/client-config'; +import fs from 'fs'; const createStackAndSetContext = (deploymentType: DeploymentType): Stack => { const app = new App(); @@ -204,4 +205,23 @@ void describe('Backend', () => { }); }); +void it('it should clear the generated env directory', () => { + const pathToDelete = `${process.cwd()}/.amplify/generated/env`; + + // Ensure the directory exists + if (!fs.existsSync(pathToDelete)) { + fs.mkdirSync(pathToDelete, { recursive: true }); + } + + // Write files to the directory + fs.writeFileSync(`${pathToDelete}/file1.ts`, 'content1'); + fs.writeFileSync(`${pathToDelete}/file2.ts`, 'content1'); + + // Clean up after the test + fs.rmSync(pathToDelete, { recursive: true, force: true }); + + assert.strictEqual(fs.existsSync(`${pathToDelete}/file1.ts`), false); + assert.strictEqual(fs.existsSync(`${pathToDelete}/file2.ts`), false); +}); + type TestResourceProvider = ResourceProvider<{ bucket: Bucket }>; diff --git a/packages/backend/src/backend_factory.ts b/packages/backend/src/backend_factory.ts index d9bf2f545a..7a1f8e953d 100644 --- a/packages/backend/src/backend_factory.ts +++ b/packages/backend/src/backend_factory.ts @@ -27,6 +27,7 @@ import { import { CustomOutputsAccumulator } from './engine/custom_outputs_accumulator.js'; import { ObjectAccumulator } from '@aws-amplify/platform-core'; import { DefaultResourceNameValidator } from './engine/validations/default_resource_name_validator.js'; +import fs from 'fs'; // Be very careful editing this value. It is the value used in the BI metrics to attribute stacks as Amplify root stacks const rootStackTypeIdentifier = 'root'; @@ -98,6 +99,19 @@ export class BackendFactory< const resourceNameValidator = new DefaultResourceNameValidator(); + const pathToDelete = `${process.cwd()}/.amplify/generated/env/`; + + // Ensure the directory exists + if (!fs.existsSync(pathToDelete)) { + fs.mkdirSync(pathToDelete, { recursive: true }); + } + + // Clear any existing files and subdirectories in the directory + fs.rmSync(pathToDelete, { recursive: true, force: true }); + + // Recreate the directory to ensure it is empty + fs.mkdirSync(pathToDelete, { recursive: true }); + // register providers but don't actually execute anything yet Object.values(constructFactories).forEach((factory) => { if (typeof factory.provides === 'string') { From ab7f76ff8b3687fcca0ae68cc3b325001c79b29f Mon Sep 17 00:00:00 2001 From: vigy02 Date: Thu, 19 Sep 2024 00:12:18 -0700 Subject: [PATCH 02/33] fix: clearing the .amplify/generated/env/ before synthesis --- .changeset/sixty-bobcats-sing.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/sixty-bobcats-sing.md diff --git a/.changeset/sixty-bobcats-sing.md b/.changeset/sixty-bobcats-sing.md new file mode 100644 index 0000000000..18efac4213 --- /dev/null +++ b/.changeset/sixty-bobcats-sing.md @@ -0,0 +1,5 @@ +--- +'@aws-amplify/backend': patch +--- + +fix: clearing the .amplify/generated/env/ before synthesis From 7c55adce35304dbc919e2b36c7ec85c36d1c85fe Mon Sep 17 00:00:00 2001 From: vigy02 Date: Thu, 19 Sep 2024 09:49:07 -0700 Subject: [PATCH 03/33] fix: clearing the .amplify/generated/env/ before synthesis --- packages/backend/src/backend_factory.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/backend/src/backend_factory.ts b/packages/backend/src/backend_factory.ts index 7a1f8e953d..03dc76bc56 100644 --- a/packages/backend/src/backend_factory.ts +++ b/packages/backend/src/backend_factory.ts @@ -109,9 +109,6 @@ export class BackendFactory< // Clear any existing files and subdirectories in the directory fs.rmSync(pathToDelete, { recursive: true, force: true }); - // Recreate the directory to ensure it is empty - fs.mkdirSync(pathToDelete, { recursive: true }); - // register providers but don't actually execute anything yet Object.values(constructFactories).forEach((factory) => { if (typeof factory.provides === 'string') { From 36727ba33c73d4677e5a856fa7341883721b30e0 Mon Sep 17 00:00:00 2001 From: vigy02 Date: Thu, 19 Sep 2024 09:54:36 -0700 Subject: [PATCH 04/33] fix: clearing the .amplify/generated/env/ before synthesis --- packages/backend/src/backend_factory.test.ts | 21 ++++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/packages/backend/src/backend_factory.test.ts b/packages/backend/src/backend_factory.test.ts index 2f9aaf8bd5..4e8868474f 100644 --- a/packages/backend/src/backend_factory.test.ts +++ b/packages/backend/src/backend_factory.test.ts @@ -206,22 +206,21 @@ void describe('Backend', () => { }); void it('it should clear the generated env directory', () => { - const pathToDelete = `${process.cwd()}/.amplify/generated/env`; - - // Ensure the directory exists - if (!fs.existsSync(pathToDelete)) { - fs.mkdirSync(pathToDelete, { recursive: true }); - } + const pathToFile1 = `${process.cwd()}/.amplify/generated/env/file1.ts`; + const pathToFile2 = `${process.cwd()}/.amplify/generated/env/file2.ts`; // Write files to the directory - fs.writeFileSync(`${pathToDelete}/file1.ts`, 'content1'); - fs.writeFileSync(`${pathToDelete}/file2.ts`, 'content1'); + fs.writeFileSync(pathToFile1, 'content1'); + fs.writeFileSync(pathToFile2, 'content1'); // Clean up after the test - fs.rmSync(pathToDelete, { recursive: true, force: true }); + fs.rmSync(`${process.cwd()}/.amplify/generated/env/`, { + recursive: true, + force: true, + }); - assert.strictEqual(fs.existsSync(`${pathToDelete}/file1.ts`), false); - assert.strictEqual(fs.existsSync(`${pathToDelete}/file2.ts`), false); + assert.strictEqual(fs.existsSync(pathToFile1), false); + assert.strictEqual(fs.existsSync(pathToFile2), false); }); type TestResourceProvider = ResourceProvider<{ bucket: Bucket }>; From aa2f7d94271ee85a1210802cd3823b64ef38c3a3 Mon Sep 17 00:00:00 2001 From: vigy02 Date: Thu, 19 Sep 2024 10:06:48 -0700 Subject: [PATCH 05/33] fix: clearing the .amplify/generated/env/ before synthesis --- package-lock.json | 161 ++++++++++++------------ packages/backend/src/backend_factory.ts | 5 - 2 files changed, 82 insertions(+), 84 deletions(-) diff --git a/package-lock.json b/package-lock.json index 35127fe7aa..1b4823b508 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23296,6 +23296,7 @@ "version": "11.2.0", "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-11.2.0.tgz", "integrity": "sha512-PmDi3uwK5nFuXh7XDTlVnS17xJS7vW36is2+w3xcv8SVxiB4NyATf4ctkVY5bkSjX0Y4nbvZCq1/EjtEyr9ktw==", + "dev": true, "license": "MIT", "dependencies": { "graceful-fs": "^4.2.0", @@ -23970,6 +23971,7 @@ "version": "9.0.6", "resolved": "https://registry.npmjs.org/immer/-/immer-9.0.6.tgz", "integrity": "sha512-G95ivKpy+EvVAnAab4fVa4YGYn24J1SpEktnJX7JJ45Bd7xqME/SCplFzYFmTbrkwZbQ4xJK1xMTUYBkN6pWsQ==", + "dev": true, "license": "MIT", "funding": { "type": "opencollective", @@ -30146,6 +30148,7 @@ "version": "8.3.2", "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", + "dev": true, "license": "MIT", "bin": { "uuid": "dist/bin/uuid" @@ -30929,17 +30932,17 @@ } }, "packages/ampx": { - "version": "0.2.1", + "version": "0.2.2", "license": "Apache-2.0" }, "packages/auth-construct": { "name": "@aws-amplify/auth-construct", - "version": "1.3.0", + "version": "1.3.1", "license": "Apache-2.0", "dependencies": { "@aws-amplify/backend-output-schemas": "^1.1.0", - "@aws-amplify/backend-output-storage": "^1.1.1", - "@aws-amplify/plugin-types": "^1.2.1", + "@aws-amplify/backend-output-storage": "^1.1.2", + "@aws-amplify/plugin-types": "^1.2.2", "@aws-sdk/util-arn-parser": "^3.568.0" }, "peerDependencies": { @@ -30949,20 +30952,20 @@ }, "packages/backend": { "name": "@aws-amplify/backend", - "version": "1.2.1", + "version": "1.2.2", "license": "Apache-2.0", "dependencies": { - "@aws-amplify/backend-auth": "^1.1.2", - "@aws-amplify/backend-data": "^1.1.3", - "@aws-amplify/backend-function": "^1.4.0", + "@aws-amplify/backend-auth": "^1.1.5", + "@aws-amplify/backend-data": "^1.1.4", + "@aws-amplify/backend-function": "^1.4.1", "@aws-amplify/backend-output-schemas": "^1.2.0", - "@aws-amplify/backend-output-storage": "^1.1.1", - "@aws-amplify/backend-secret": "^1.0.1", - "@aws-amplify/backend-storage": "^1.1.1", - "@aws-amplify/client-config": "^1.3.0", + "@aws-amplify/backend-output-storage": "^1.1.2", + "@aws-amplify/backend-secret": "^1.1.2", + "@aws-amplify/backend-storage": "^1.1.3", + "@aws-amplify/client-config": "^1.3.1", "@aws-amplify/data-schema": "^1.0.0", "@aws-amplify/platform-core": "^1.1.0", - "@aws-amplify/plugin-types": "^1.2.1", + "@aws-amplify/plugin-types": "^1.2.2", "@aws-sdk/client-amplify": "^3.624.0", "lodash.snakecase": "^4.1.1" }, @@ -30995,15 +30998,15 @@ }, "packages/backend-auth": { "name": "@aws-amplify/backend-auth", - "version": "1.1.4", + "version": "1.1.5", "license": "Apache-2.0", "dependencies": { - "@aws-amplify/auth-construct": "^1.3.0", - "@aws-amplify/backend-output-storage": "^1.1.1", - "@aws-amplify/plugin-types": "^1.2.1" + "@aws-amplify/auth-construct": "^1.3.1", + "@aws-amplify/backend-output-storage": "^1.1.2", + "@aws-amplify/plugin-types": "^1.2.2" }, "devDependencies": { - "@aws-amplify/backend-platform-test-stubs": "^0.3.4", + "@aws-amplify/backend-platform-test-stubs": "^0.3.5", "@aws-amplify/platform-core": "^1.0.6" }, "peerDependencies": { @@ -31013,17 +31016,17 @@ }, "packages/backend-data": { "name": "@aws-amplify/backend-data", - "version": "1.1.3", + "version": "1.1.4", "license": "Apache-2.0", "dependencies": { "@aws-amplify/backend-output-schemas": "^1.1.0", - "@aws-amplify/backend-output-storage": "^1.1.1", + "@aws-amplify/backend-output-storage": "^1.1.2", "@aws-amplify/data-construct": "^1.10.1", "@aws-amplify/data-schema-types": "^1.1.1", - "@aws-amplify/plugin-types": "^1.2.1" + "@aws-amplify/plugin-types": "^1.2.2" }, "devDependencies": { - "@aws-amplify/backend-platform-test-stubs": "^0.3.4", + "@aws-amplify/backend-platform-test-stubs": "^0.3.5", "@aws-amplify/data-schema": "^1.0.0", "@aws-amplify/platform-core": "^1.0.7" }, @@ -31034,11 +31037,11 @@ }, "packages/backend-deployer": { "name": "@aws-amplify/backend-deployer", - "version": "1.1.2", + "version": "1.1.3", "license": "Apache-2.0", "dependencies": { "@aws-amplify/platform-core": "^1.0.6", - "@aws-amplify/plugin-types": "^1.2.1", + "@aws-amplify/plugin-types": "^1.2.2", "execa": "^8.0.1", "tsx": "^4.6.1" }, @@ -31049,16 +31052,16 @@ }, "packages/backend-function": { "name": "@aws-amplify/backend-function", - "version": "1.4.0", + "version": "1.4.1", "license": "Apache-2.0", "dependencies": { "@aws-amplify/backend-output-schemas": "^1.1.0", - "@aws-amplify/backend-output-storage": "^1.1.1", - "@aws-amplify/plugin-types": "^1.2.1", + "@aws-amplify/backend-output-storage": "^1.1.2", + "@aws-amplify/plugin-types": "^1.2.2", "execa": "^8.0.1" }, "devDependencies": { - "@aws-amplify/backend-platform-test-stubs": "^0.3.4", + "@aws-amplify/backend-platform-test-stubs": "^0.3.5", "@aws-amplify/platform-core": "^1.1.0", "@aws-sdk/client-ssm": "^3.624.0", "aws-sdk": "^2.1550.0", @@ -31096,12 +31099,12 @@ }, "packages/backend-output-storage": { "name": "@aws-amplify/backend-output-storage", - "version": "1.1.1", + "version": "1.1.2", "license": "Apache-2.0", "dependencies": { "@aws-amplify/backend-output-schemas": "^1.2.0", "@aws-amplify/platform-core": "^1.0.6", - "@aws-amplify/plugin-types": "^1.2.1" + "@aws-amplify/plugin-types": "^1.2.2" }, "peerDependencies": { "aws-cdk-lib": "^2.152.0" @@ -31109,21 +31112,21 @@ }, "packages/backend-platform-test-stubs": { "name": "@aws-amplify/backend-platform-test-stubs", - "version": "0.3.4", + "version": "0.3.5", "license": "Apache-2.0", "dependencies": { - "@aws-amplify/plugin-types": "^1.2.1", + "@aws-amplify/plugin-types": "^1.2.2", "aws-cdk-lib": "^2.152.0", "constructs": "^10.0.0" } }, "packages/backend-secret": { "name": "@aws-amplify/backend-secret", - "version": "1.1.1", + "version": "1.1.2", "license": "Apache-2.0", "dependencies": { "@aws-amplify/platform-core": "^1.0.5", - "@aws-amplify/plugin-types": "^1.1.1", + "@aws-amplify/plugin-types": "^1.2.2", "@aws-sdk/client-ssm": "^3.624.0" }, "devDependencies": { @@ -31132,15 +31135,15 @@ }, "packages/backend-storage": { "name": "@aws-amplify/backend-storage", - "version": "1.1.2", + "version": "1.1.3", "license": "Apache-2.0", "dependencies": { "@aws-amplify/backend-output-schemas": "^1.2.0", - "@aws-amplify/backend-output-storage": "^1.1.1", - "@aws-amplify/plugin-types": "^1.2.1" + "@aws-amplify/backend-output-storage": "^1.1.2", + "@aws-amplify/plugin-types": "^1.2.2" }, "devDependencies": { - "@aws-amplify/backend-platform-test-stubs": "^0.3.4", + "@aws-amplify/backend-platform-test-stubs": "^0.3.5", "@aws-amplify/platform-core": "^1.0.6" }, "peerDependencies": { @@ -31150,21 +31153,21 @@ }, "packages/cli": { "name": "@aws-amplify/backend-cli", - "version": "1.2.6", + "version": "1.2.7", "license": "Apache-2.0", "dependencies": { - "@aws-amplify/backend-deployer": "^1.1.1", + "@aws-amplify/backend-deployer": "^1.1.3", "@aws-amplify/backend-output-schemas": "^1.1.0", - "@aws-amplify/backend-secret": "^1.1.0", - "@aws-amplify/cli-core": "^1.1.2", - "@aws-amplify/client-config": "^1.2.1", - "@aws-amplify/deployed-backend-client": "^1.3.0", - "@aws-amplify/form-generator": "^1.0.1", - "@aws-amplify/model-generator": "^1.0.5", + "@aws-amplify/backend-secret": "^1.1.2", + "@aws-amplify/cli-core": "^1.1.3", + "@aws-amplify/client-config": "^1.3.1", + "@aws-amplify/deployed-backend-client": "^1.4.1", + "@aws-amplify/form-generator": "^1.0.2", + "@aws-amplify/model-generator": "^1.0.7", "@aws-amplify/platform-core": "^1.0.5", - "@aws-amplify/plugin-types": "^1.2.1", - "@aws-amplify/sandbox": "^1.2.0", - "@aws-amplify/schema-generator": "^1.2.1", + "@aws-amplify/plugin-types": "^1.2.2", + "@aws-amplify/sandbox": "^1.2.2", + "@aws-amplify/schema-generator": "^1.2.3", "@aws-sdk/client-amplify": "^3.624.0", "@aws-sdk/client-cloudformation": "^3.624.0", "@aws-sdk/client-s3": "^3.624.0", @@ -31197,7 +31200,7 @@ }, "packages/cli-core": { "name": "@aws-amplify/cli-core", - "version": "1.1.2", + "version": "1.1.3", "license": "Apache-2.0", "dependencies": { "@aws-amplify/platform-core": "^1.0.5", @@ -31304,14 +31307,14 @@ }, "packages/client-config": { "name": "@aws-amplify/client-config", - "version": "1.3.0", + "version": "1.3.1", "license": "Apache-2.0", "dependencies": { "@aws-amplify/backend-output-schemas": "^1.2.0", - "@aws-amplify/deployed-backend-client": "^1.4.0", - "@aws-amplify/model-generator": "^1.0.5", + "@aws-amplify/deployed-backend-client": "^1.4.1", + "@aws-amplify/model-generator": "^1.0.7", "@aws-amplify/platform-core": "^1.0.7", - "@aws-amplify/plugin-types": "^1.2.1", + "@aws-amplify/plugin-types": "^1.2.2", "zod": "^3.22.2" }, "devDependencies": { @@ -31326,12 +31329,12 @@ } }, "packages/create-amplify": { - "version": "1.0.5", + "version": "1.0.6", "license": "Apache-2.0", "dependencies": { - "@aws-amplify/cli-core": "^1.1.1", + "@aws-amplify/cli-core": "^1.1.3", "@aws-amplify/platform-core": "^1.0.3", - "@aws-amplify/plugin-types": "^1.1.0", + "@aws-amplify/plugin-types": "^1.2.2", "execa": "^8.0.1", "kleur": "^4.1.5", "yargs": "^17.7.2" @@ -31441,12 +31444,12 @@ }, "packages/deployed-backend-client": { "name": "@aws-amplify/deployed-backend-client", - "version": "1.4.0", + "version": "1.4.1", "license": "Apache-2.0", "dependencies": { "@aws-amplify/backend-output-schemas": "^1.2.0", "@aws-amplify/platform-core": "^1.0.5", - "@aws-amplify/plugin-types": "^1.2.1", + "@aws-amplify/plugin-types": "^1.2.2", "zod": "^3.22.2" }, "peerDependencies": { @@ -31469,7 +31472,7 @@ }, "packages/form-generator": { "name": "@aws-amplify/form-generator", - "version": "1.0.1", + "version": "1.0.2", "license": "Apache-2.0", "dependencies": { "@aws-amplify/appsync-modelgen-plugin": "^2.11.0", @@ -31489,20 +31492,20 @@ }, "packages/integration-tests": { "name": "@aws-amplify/integration-tests", - "version": "0.5.8", + "version": "0.5.9", "license": "Apache-2.0", "devDependencies": { "@apollo/client": "^3.10.1", "@aws-amplify/ai-constructs": "^0.1.0", - "@aws-amplify/auth-construct": "^1.2.2", - "@aws-amplify/backend": "^1.2.1", + "@aws-amplify/auth-construct": "^1.3.1", + "@aws-amplify/backend": "^1.2.2", "@aws-amplify/backend-ai": "^0.1.0", - "@aws-amplify/backend-secret": "^1.0.1", - "@aws-amplify/client-config": "^1.1.3", + "@aws-amplify/backend-secret": "^1.1.2", + "@aws-amplify/client-config": "^1.3.1", "@aws-amplify/data-schema": "^1.0.0", - "@aws-amplify/deployed-backend-client": "^1.3.0", + "@aws-amplify/deployed-backend-client": "^1.4.1", "@aws-amplify/platform-core": "^1.1.0", - "@aws-amplify/plugin-types": "^1.2.1", + "@aws-amplify/plugin-types": "^1.2.2", "@aws-sdk/client-accessanalyzer": "^3.624.0", "@aws-sdk/client-amplify": "^3.624.0", "@aws-sdk/client-bedrock-runtime": "^3.622.0", @@ -31548,15 +31551,15 @@ }, "packages/model-generator": { "name": "@aws-amplify/model-generator", - "version": "1.0.6", + "version": "1.0.7", "license": "Apache-2.0", "dependencies": { "@aws-amplify/backend-output-schemas": "^1.1.0", - "@aws-amplify/deployed-backend-client": "^1.3.0", + "@aws-amplify/deployed-backend-client": "^1.4.1", "@aws-amplify/graphql-generator": "^0.4.0", "@aws-amplify/graphql-types-generator": "^3.6.0", "@aws-amplify/platform-core": "^1.0.5", - "@aws-amplify/plugin-types": "^1.2.1", + "@aws-amplify/plugin-types": "^1.2.2", "@aws-sdk/client-appsync": "^3.624.0", "@aws-sdk/client-s3": "^3.624.0", "@aws-sdk/credential-providers": "^3.624.0", @@ -31602,7 +31605,7 @@ }, "packages/plugin-types": { "name": "@aws-amplify/plugin-types", - "version": "1.2.1", + "version": "1.2.2", "license": "Apache-2.0", "devDependencies": { "execa": "^5.1.1" @@ -31731,16 +31734,16 @@ }, "packages/sandbox": { "name": "@aws-amplify/sandbox", - "version": "1.2.1", + "version": "1.2.2", "license": "Apache-2.0", "dependencies": { - "@aws-amplify/backend-deployer": "^1.1.0", - "@aws-amplify/backend-secret": "^1.1.1", - "@aws-amplify/cli-core": "^1.1.2", - "@aws-amplify/client-config": "^1.1.3", - "@aws-amplify/deployed-backend-client": "^1.3.0", + "@aws-amplify/backend-deployer": "^1.1.3", + "@aws-amplify/backend-secret": "^1.1.2", + "@aws-amplify/cli-core": "^1.1.3", + "@aws-amplify/client-config": "^1.3.1", + "@aws-amplify/deployed-backend-client": "^1.4.1", "@aws-amplify/platform-core": "^1.0.6", - "@aws-amplify/plugin-types": "^1.2.1", + "@aws-amplify/plugin-types": "^1.2.2", "@aws-sdk/client-cloudformation": "^3.624.0", "@aws-sdk/client-cloudwatch-logs": "^3.624.0", "@aws-sdk/client-lambda": "^3.624.0", @@ -31764,7 +31767,7 @@ }, "packages/schema-generator": { "name": "@aws-amplify/schema-generator", - "version": "1.2.2", + "version": "1.2.3", "license": "Apache-2.0", "dependencies": { "@aws-amplify/graphql-schema-generator": "^0.9.4", diff --git a/packages/backend/src/backend_factory.ts b/packages/backend/src/backend_factory.ts index 03dc76bc56..6519943eb7 100644 --- a/packages/backend/src/backend_factory.ts +++ b/packages/backend/src/backend_factory.ts @@ -101,11 +101,6 @@ export class BackendFactory< const pathToDelete = `${process.cwd()}/.amplify/generated/env/`; - // Ensure the directory exists - if (!fs.existsSync(pathToDelete)) { - fs.mkdirSync(pathToDelete, { recursive: true }); - } - // Clear any existing files and subdirectories in the directory fs.rmSync(pathToDelete, { recursive: true, force: true }); From 030251708d889cea5586b6421a22472245cc961f Mon Sep 17 00:00:00 2001 From: vigy02 Date: Thu, 19 Sep 2024 10:10:11 -0700 Subject: [PATCH 06/33] fix: clearing the .amplify/generated/env/ before synthesis --- packages/backend/src/backend_factory.test.ts | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/packages/backend/src/backend_factory.test.ts b/packages/backend/src/backend_factory.test.ts index 4e8868474f..4016e50b78 100644 --- a/packages/backend/src/backend_factory.test.ts +++ b/packages/backend/src/backend_factory.test.ts @@ -11,7 +11,6 @@ import { App, Stack } from 'aws-cdk-lib'; import { Template } from 'aws-cdk-lib/assertions'; import assert from 'node:assert'; import { ClientConfig } from '@aws-amplify/client-config'; -import fs from 'fs'; const createStackAndSetContext = (deploymentType: DeploymentType): Stack => { const app = new App(); @@ -205,22 +204,4 @@ void describe('Backend', () => { }); }); -void it('it should clear the generated env directory', () => { - const pathToFile1 = `${process.cwd()}/.amplify/generated/env/file1.ts`; - const pathToFile2 = `${process.cwd()}/.amplify/generated/env/file2.ts`; - - // Write files to the directory - fs.writeFileSync(pathToFile1, 'content1'); - fs.writeFileSync(pathToFile2, 'content1'); - - // Clean up after the test - fs.rmSync(`${process.cwd()}/.amplify/generated/env/`, { - recursive: true, - force: true, - }); - - assert.strictEqual(fs.existsSync(pathToFile1), false); - assert.strictEqual(fs.existsSync(pathToFile2), false); -}); - type TestResourceProvider = ResourceProvider<{ bucket: Bucket }>; From 0adf87332e613fa994ab360a10746a665be245d2 Mon Sep 17 00:00:00 2001 From: vigy02 Date: Thu, 19 Sep 2024 10:13:57 -0700 Subject: [PATCH 07/33] chore: add changeset --- .changeset/lovely-plums-beam.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/lovely-plums-beam.md diff --git a/.changeset/lovely-plums-beam.md b/.changeset/lovely-plums-beam.md new file mode 100644 index 0000000000..7c96db3a5d --- /dev/null +++ b/.changeset/lovely-plums-beam.md @@ -0,0 +1,5 @@ +--- +'@aws-amplify/backend': patch +--- + +fix: delete generated folder at start of synth From a5b5d3ce804c951f7b5f0187ae68d5681f18bd48 Mon Sep 17 00:00:00 2001 From: vigy02 Date: Thu, 19 Sep 2024 10:53:19 -0700 Subject: [PATCH 08/33] fix: clearing the .amplify/generated/env/ before synthesis --- packages/backend/src/backend_factory.test.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/backend/src/backend_factory.test.ts b/packages/backend/src/backend_factory.test.ts index 4016e50b78..d00f31c20d 100644 --- a/packages/backend/src/backend_factory.test.ts +++ b/packages/backend/src/backend_factory.test.ts @@ -1,4 +1,4 @@ -import { beforeEach, describe, it } from 'node:test'; +import { beforeEach, describe, it, mock } from 'node:test'; import { ConstructFactory, DeepPartialAmplifyGeneratedConfigs, @@ -9,8 +9,9 @@ import { Bucket } from 'aws-cdk-lib/aws-s3'; import { BackendFactory } from './backend_factory.js'; import { App, Stack } from 'aws-cdk-lib'; import { Template } from 'aws-cdk-lib/assertions'; -import assert from 'node:assert'; import { ClientConfig } from '@aws-amplify/client-config'; +import fs from 'fs'; +import assert from 'node:assert'; const createStackAndSetContext = (deploymentType: DeploymentType): Stack => { const app = new App(); @@ -202,6 +203,14 @@ void describe('Backend', () => { Value: JSON.stringify(clientConfigPartial), }); }); + void it('checking if generated/emv/ directory is cleared', () => { + const fsRmSyncMock = mock.method(fs, 'rmSync'); + const backend = new BackendFactory({}, rootStack); + backend.createStack('testStack'); + const callCount = fsRmSyncMock.mock.callCount; + assert.equal(callCount, 1); + mock.restoreAll(); + }); }); type TestResourceProvider = ResourceProvider<{ bucket: Bucket }>; From 753701ac9dec82901eac283f34636ea522334fab Mon Sep 17 00:00:00 2001 From: vigy02 Date: Thu, 19 Sep 2024 11:09:59 -0700 Subject: [PATCH 09/33] fix: clearing the .amplify/generated/env/ before synthesis --- packages/backend/src/backend_factory.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/backend/src/backend_factory.test.ts b/packages/backend/src/backend_factory.test.ts index d00f31c20d..da47b44057 100644 --- a/packages/backend/src/backend_factory.test.ts +++ b/packages/backend/src/backend_factory.test.ts @@ -203,12 +203,12 @@ void describe('Backend', () => { Value: JSON.stringify(clientConfigPartial), }); }); - void it('checking if generated/emv/ directory is cleared', () => { + void it('checking if .amplify/generated/env/ directory is cleared', () => { const fsRmSyncMock = mock.method(fs, 'rmSync'); const backend = new BackendFactory({}, rootStack); backend.createStack('testStack'); const callCount = fsRmSyncMock.mock.callCount; - assert.equal(callCount, 1); + assert.equal(callCount, 2); mock.restoreAll(); }); }); From 955cecc687590e4de0549c8348bb4f353fbda345 Mon Sep 17 00:00:00 2001 From: vigy02 Date: Thu, 19 Sep 2024 11:15:14 -0700 Subject: [PATCH 10/33] fix: clearing the .amplify/generated/env/ before synthesis --- packages/backend/src/backend_factory.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/backend/src/backend_factory.test.ts b/packages/backend/src/backend_factory.test.ts index da47b44057..deb5191afb 100644 --- a/packages/backend/src/backend_factory.test.ts +++ b/packages/backend/src/backend_factory.test.ts @@ -207,8 +207,8 @@ void describe('Backend', () => { const fsRmSyncMock = mock.method(fs, 'rmSync'); const backend = new BackendFactory({}, rootStack); backend.createStack('testStack'); - const callCount = fsRmSyncMock.mock.callCount; - assert.equal(callCount, 2); + const callCount = fsRmSyncMock.mock.callCount(); + assert.equal(callCount, 1); mock.restoreAll(); }); }); From fc0be7e63d073101590ae1533b7df0270444a9c0 Mon Sep 17 00:00:00 2001 From: vigy02 Date: Fri, 20 Sep 2024 13:56:18 -0700 Subject: [PATCH 11/33] fix: Clear generated env directory before shim generation --- .../src/function_env_type_generator.test.ts | 19 +++++++++++++ .../src/function_env_type_generator.ts | 27 ++++++++++++++++--- packages/backend/src/backend_factory.test.ts | 11 +------- packages/backend/src/backend_factory.ts | 6 ----- 4 files changed, 43 insertions(+), 20 deletions(-) diff --git a/packages/backend-function/src/function_env_type_generator.test.ts b/packages/backend-function/src/function_env_type_generator.test.ts index d988e06fec..2aba3db2a5 100644 --- a/packages/backend-function/src/function_env_type_generator.test.ts +++ b/packages/backend-function/src/function_env_type_generator.test.ts @@ -6,6 +6,25 @@ import assert from 'assert'; import { pathToFileURL } from 'url'; void describe('FunctionEnvironmentTypeGenerator', () => { + void it('clears the generated env directory', async () => { + const fsMkdirSyncMock = mock.method(fs, 'mkdirSync', () => {}); + const fsExistsSyncMock = mock.method(fs, 'existsSync', () => true); + const fsRmSyncMock = mock.method(fs, 'rmSync', () => {}); + + FunctionEnvironmentTypeGenerator.clearGeneratedEnvDirectory(); + + assert.equal(fsExistsSyncMock.mock.calls.length, 1); + assert.equal(fsRmSyncMock.mock.calls.length, 1); + assert.deepEqual(fsRmSyncMock.mock.calls[0].arguments[1], { + recursive: true, + force: true, + }); + + fsMkdirSyncMock.mock.restore(); + fsExistsSyncMock.mock.restore(); + fsRmSyncMock.mock.restore(); + }); + void it('generates a type definition file', () => { const fsOpenSyncMock = mock.method(fs, 'openSync'); const fsWriteFileSyncMock = mock.method(fs, 'writeFileSync', () => null); diff --git a/packages/backend-function/src/function_env_type_generator.ts b/packages/backend-function/src/function_env_type_generator.ts index ea650e26c9..0f030e1c79 100644 --- a/packages/backend-function/src/function_env_type_generator.ts +++ b/packages/backend-function/src/function_env_type_generator.ts @@ -10,7 +10,6 @@ export class FunctionEnvironmentTypeGenerator { private readonly header = '// This file is auto-generated by Amplify. Edits will be overwritten.'; - // The variable gets updated when the fully typed file is updated. private readonly envAssignment = 'export const env = process.env'; private typeDefFilePath: string; @@ -21,9 +20,28 @@ export class FunctionEnvironmentTypeGenerator { * Initialize typed process.env shim file name and location */ constructor(private readonly functionName: string) { - this.typeDefFilePath = `${process.cwd()}/.amplify/generated/env/${ - this.functionName - }.ts`; + this.typeDefFilePath = path.join( + process.cwd(), + '.amplify', + 'generated', + 'env', + `${this.functionName}.ts` + ); + } + + /** + * Clear existing files and subdirectories in the generated env directory + */ + clearGeneratedEnvDirectory(): void { + const pathToDelete = path.join( + process.cwd(), + '.amplify', + 'generated', + 'env' + ); + if (fs.existsSync(pathToDelete)) { + fs.rmSync(pathToDelete, { recursive: true, force: true }); + } } /** @@ -85,6 +103,7 @@ export class FunctionEnvironmentTypeGenerator { }; private writeShimFile = (content: string) => { + this.clearGeneratedEnvDirectory(); const typeDefFileDirname = path.dirname(this.typeDefFilePath); if (!fs.existsSync(typeDefFileDirname)) { diff --git a/packages/backend/src/backend_factory.test.ts b/packages/backend/src/backend_factory.test.ts index deb5191afb..7b9840b423 100644 --- a/packages/backend/src/backend_factory.test.ts +++ b/packages/backend/src/backend_factory.test.ts @@ -1,4 +1,4 @@ -import { beforeEach, describe, it, mock } from 'node:test'; +import { beforeEach, describe, it } from 'node:test'; import { ConstructFactory, DeepPartialAmplifyGeneratedConfigs, @@ -10,7 +10,6 @@ import { BackendFactory } from './backend_factory.js'; import { App, Stack } from 'aws-cdk-lib'; import { Template } from 'aws-cdk-lib/assertions'; import { ClientConfig } from '@aws-amplify/client-config'; -import fs from 'fs'; import assert from 'node:assert'; const createStackAndSetContext = (deploymentType: DeploymentType): Stack => { @@ -203,14 +202,6 @@ void describe('Backend', () => { Value: JSON.stringify(clientConfigPartial), }); }); - void it('checking if .amplify/generated/env/ directory is cleared', () => { - const fsRmSyncMock = mock.method(fs, 'rmSync'); - const backend = new BackendFactory({}, rootStack); - backend.createStack('testStack'); - const callCount = fsRmSyncMock.mock.callCount(); - assert.equal(callCount, 1); - mock.restoreAll(); - }); }); type TestResourceProvider = ResourceProvider<{ bucket: Bucket }>; diff --git a/packages/backend/src/backend_factory.ts b/packages/backend/src/backend_factory.ts index 6519943eb7..d9bf2f545a 100644 --- a/packages/backend/src/backend_factory.ts +++ b/packages/backend/src/backend_factory.ts @@ -27,7 +27,6 @@ import { import { CustomOutputsAccumulator } from './engine/custom_outputs_accumulator.js'; import { ObjectAccumulator } from '@aws-amplify/platform-core'; import { DefaultResourceNameValidator } from './engine/validations/default_resource_name_validator.js'; -import fs from 'fs'; // Be very careful editing this value. It is the value used in the BI metrics to attribute stacks as Amplify root stacks const rootStackTypeIdentifier = 'root'; @@ -99,11 +98,6 @@ export class BackendFactory< const resourceNameValidator = new DefaultResourceNameValidator(); - const pathToDelete = `${process.cwd()}/.amplify/generated/env/`; - - // Clear any existing files and subdirectories in the directory - fs.rmSync(pathToDelete, { recursive: true, force: true }); - // register providers but don't actually execute anything yet Object.values(constructFactories).forEach((factory) => { if (typeof factory.provides === 'string') { From 6c916e09e45518240bc2dfd70b9a0db06e3c6758 Mon Sep 17 00:00:00 2001 From: vigy02 Date: Fri, 20 Sep 2024 13:59:21 -0700 Subject: [PATCH 12/33] fix: Clear generated env directory before shim generation --- packages/backend-function/src/function_env_type_generator.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/backend-function/src/function_env_type_generator.ts b/packages/backend-function/src/function_env_type_generator.ts index 0f030e1c79..5ad0c642ec 100644 --- a/packages/backend-function/src/function_env_type_generator.ts +++ b/packages/backend-function/src/function_env_type_generator.ts @@ -32,7 +32,7 @@ export class FunctionEnvironmentTypeGenerator { /** * Clear existing files and subdirectories in the generated env directory */ - clearGeneratedEnvDirectory(): void { + public clearGeneratedEnvDirectory(): void { const pathToDelete = path.join( process.cwd(), '.amplify', From cab3f1aab694b10cbbff5dde9311a375858d3e2b Mon Sep 17 00:00:00 2001 From: vigy02 Date: Fri, 20 Sep 2024 14:08:40 -0700 Subject: [PATCH 13/33] fix: Clear generated env directory before shim generation --- .../src/function_env_type_generator.test.ts | 33 ++++++++----------- .../src/function_env_type_generator.ts | 30 ++++++++--------- 2 files changed, 29 insertions(+), 34 deletions(-) diff --git a/packages/backend-function/src/function_env_type_generator.test.ts b/packages/backend-function/src/function_env_type_generator.test.ts index 2aba3db2a5..e0067ead22 100644 --- a/packages/backend-function/src/function_env_type_generator.test.ts +++ b/packages/backend-function/src/function_env_type_generator.test.ts @@ -6,25 +6,6 @@ import assert from 'assert'; import { pathToFileURL } from 'url'; void describe('FunctionEnvironmentTypeGenerator', () => { - void it('clears the generated env directory', async () => { - const fsMkdirSyncMock = mock.method(fs, 'mkdirSync', () => {}); - const fsExistsSyncMock = mock.method(fs, 'existsSync', () => true); - const fsRmSyncMock = mock.method(fs, 'rmSync', () => {}); - - FunctionEnvironmentTypeGenerator.clearGeneratedEnvDirectory(); - - assert.equal(fsExistsSyncMock.mock.calls.length, 1); - assert.equal(fsRmSyncMock.mock.calls.length, 1); - assert.deepEqual(fsRmSyncMock.mock.calls[0].arguments[1], { - recursive: true, - force: true, - }); - - fsMkdirSyncMock.mock.restore(); - fsExistsSyncMock.mock.restore(); - fsRmSyncMock.mock.restore(); - }); - void it('generates a type definition file', () => { const fsOpenSyncMock = mock.method(fs, 'openSync'); const fsWriteFileSyncMock = mock.method(fs, 'writeFileSync', () => null); @@ -88,4 +69,18 @@ void describe('FunctionEnvironmentTypeGenerator', () => { await fsp.rm(targetDirectory, { recursive: true, force: true }); }); + void it('clears the generated env directory', async () => { + const fsExistsSyncMock = mock.method(fs, 'existsSync', () => true); + const fsRmSyncMock = mock.method(fs, 'rmSync', () => {}); + + const functionEnvironmentTypeGenerator = + new FunctionEnvironmentTypeGenerator('testFunction'); + functionEnvironmentTypeGenerator.clearGeneratedEnvDirectory(); + + assert.equal(fsExistsSyncMock.mock.calls.length, 1); + assert.equal(fsRmSyncMock.mock.calls.length, 1); + + fsExistsSyncMock.mock.restore(); + fsRmSyncMock.mock.restore(); + }); }); diff --git a/packages/backend-function/src/function_env_type_generator.ts b/packages/backend-function/src/function_env_type_generator.ts index 5ad0c642ec..8f2e7d4f3d 100644 --- a/packages/backend-function/src/function_env_type_generator.ts +++ b/packages/backend-function/src/function_env_type_generator.ts @@ -29,21 +29,6 @@ export class FunctionEnvironmentTypeGenerator { ); } - /** - * Clear existing files and subdirectories in the generated env directory - */ - public clearGeneratedEnvDirectory(): void { - const pathToDelete = path.join( - process.cwd(), - '.amplify', - 'generated', - 'env' - ); - if (fs.existsSync(pathToDelete)) { - fs.rmSync(pathToDelete, { recursive: true, force: true }); - } - } - /** * Generate a typed process.env shim */ @@ -102,6 +87,21 @@ export class FunctionEnvironmentTypeGenerator { this.writeShimFile(content); }; + /** + * Clear existing files and subdirectories in the generated env directory + */ + private clearGeneratedEnvDirectory(): void { + const pathToDelete = path.join( + process.cwd(), + '.amplify', + 'generated', + 'env' + ); + if (fs.existsSync(pathToDelete)) { + fs.rmSync(pathToDelete, { recursive: true, force: true }); + } + } + private writeShimFile = (content: string) => { this.clearGeneratedEnvDirectory(); const typeDefFileDirname = path.dirname(this.typeDefFilePath); From 954e1c01210bc97b229330967a995592da67f65d Mon Sep 17 00:00:00 2001 From: vigy02 Date: Fri, 20 Sep 2024 14:11:16 -0700 Subject: [PATCH 14/33] fix: Clear generated env directory before shim generation --- packages/backend-function/src/function_env_type_generator.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/backend-function/src/function_env_type_generator.ts b/packages/backend-function/src/function_env_type_generator.ts index 8f2e7d4f3d..79e346ba52 100644 --- a/packages/backend-function/src/function_env_type_generator.ts +++ b/packages/backend-function/src/function_env_type_generator.ts @@ -90,7 +90,7 @@ export class FunctionEnvironmentTypeGenerator { /** * Clear existing files and subdirectories in the generated env directory */ - private clearGeneratedEnvDirectory(): void { + clearGeneratedEnvDirectory(): void { const pathToDelete = path.join( process.cwd(), '.amplify', From d9212d8122d72e85c0acc191849d8a5ebb727306 Mon Sep 17 00:00:00 2001 From: vigy02 Date: Fri, 20 Sep 2024 14:19:30 -0700 Subject: [PATCH 15/33] chore: add changeset --- .changeset/lovely-cars-carry.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/lovely-cars-carry.md diff --git a/.changeset/lovely-cars-carry.md b/.changeset/lovely-cars-carry.md new file mode 100644 index 0000000000..524d6fa2fd --- /dev/null +++ b/.changeset/lovely-cars-carry.md @@ -0,0 +1,6 @@ +--- +'@aws-amplify/backend-function': patch +'@aws-amplify/backend': patch +--- + +Fix: Clear generated env directory before shim generation From c09e1ee887bcec8796983ed504cda97a7ddb409a Mon Sep 17 00:00:00 2001 From: vigy02 Date: Fri, 20 Sep 2024 14:29:40 -0700 Subject: [PATCH 16/33] fix: Clear generated env directory before shim generation --- packages/backend-function/src/factory.ts | 3 +++ packages/backend-function/src/function_env_type_generator.ts | 1 - 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/backend-function/src/factory.ts b/packages/backend-function/src/factory.ts index 0b184ddda2..848c8549fe 100644 --- a/packages/backend-function/src/factory.ts +++ b/packages/backend-function/src/factory.ts @@ -349,6 +349,9 @@ class AmplifyFunction const functionEnvironmentTypeGenerator = new FunctionEnvironmentTypeGenerator(id); + // Clear the shim generation directory + functionEnvironmentTypeGenerator.clearGeneratedEnvDirectory(); + // esbuild runs as part of the NodejsFunction constructor, so we eagerly generate the process env shim without types so it can be included in the function bundle. // This will be overwritten with the typed file at the end of synthesis functionEnvironmentTypeGenerator.generateProcessEnvShim(); diff --git a/packages/backend-function/src/function_env_type_generator.ts b/packages/backend-function/src/function_env_type_generator.ts index 79e346ba52..5768b4da43 100644 --- a/packages/backend-function/src/function_env_type_generator.ts +++ b/packages/backend-function/src/function_env_type_generator.ts @@ -103,7 +103,6 @@ export class FunctionEnvironmentTypeGenerator { } private writeShimFile = (content: string) => { - this.clearGeneratedEnvDirectory(); const typeDefFileDirname = path.dirname(this.typeDefFilePath); if (!fs.existsSync(typeDefFileDirname)) { From 05e1fd519bbf3d277bbe63309f1da7775f8622ab Mon Sep 17 00:00:00 2001 From: vigy02 Date: Fri, 20 Sep 2024 14:30:31 -0700 Subject: [PATCH 17/33] fix: Clear generated env directory before shim generation --- .changeset/red-badgers-shop.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/red-badgers-shop.md diff --git a/.changeset/red-badgers-shop.md b/.changeset/red-badgers-shop.md new file mode 100644 index 0000000000..ddf1633493 --- /dev/null +++ b/.changeset/red-badgers-shop.md @@ -0,0 +1,6 @@ +--- +'@aws-amplify/backend-function': patch +'@aws-amplify/backend': patch +--- + +Clear the shim generation directory From 3f31b869b57e8c4230763a98cc6dfcebb2c3bad8 Mon Sep 17 00:00:00 2001 From: vigy02 Date: Mon, 23 Sep 2024 10:32:25 -0700 Subject: [PATCH 18/33] fix: added static context to clear the .amplify/generated/env Directory before synthesis --- packages/backend-function/src/factory.ts | 2 +- .../src/function_env_type_generator.test.ts | 4 +-- .../src/function_env_type_generator.ts | 29 +++++++++---------- packages/backend/src/backend_factory.test.ts | 1 - 4 files changed, 16 insertions(+), 20 deletions(-) diff --git a/packages/backend-function/src/factory.ts b/packages/backend-function/src/factory.ts index 848c8549fe..972e9ba2c1 100644 --- a/packages/backend-function/src/factory.ts +++ b/packages/backend-function/src/factory.ts @@ -350,7 +350,7 @@ class AmplifyFunction new FunctionEnvironmentTypeGenerator(id); // Clear the shim generation directory - functionEnvironmentTypeGenerator.clearGeneratedEnvDirectory(); + FunctionEnvironmentTypeGenerator.clearGeneratedEnvDirectory(); // esbuild runs as part of the NodejsFunction constructor, so we eagerly generate the process env shim without types so it can be included in the function bundle. // This will be overwritten with the typed file at the end of synthesis diff --git a/packages/backend-function/src/function_env_type_generator.test.ts b/packages/backend-function/src/function_env_type_generator.test.ts index e0067ead22..c7204ddd28 100644 --- a/packages/backend-function/src/function_env_type_generator.test.ts +++ b/packages/backend-function/src/function_env_type_generator.test.ts @@ -73,9 +73,7 @@ void describe('FunctionEnvironmentTypeGenerator', () => { const fsExistsSyncMock = mock.method(fs, 'existsSync', () => true); const fsRmSyncMock = mock.method(fs, 'rmSync', () => {}); - const functionEnvironmentTypeGenerator = - new FunctionEnvironmentTypeGenerator('testFunction'); - functionEnvironmentTypeGenerator.clearGeneratedEnvDirectory(); + FunctionEnvironmentTypeGenerator.clearGeneratedEnvDirectory(); assert.equal(fsExistsSyncMock.mock.calls.length, 1); assert.equal(fsRmSyncMock.mock.calls.length, 1); diff --git a/packages/backend-function/src/function_env_type_generator.ts b/packages/backend-function/src/function_env_type_generator.ts index 5768b4da43..60f5838000 100644 --- a/packages/backend-function/src/function_env_type_generator.ts +++ b/packages/backend-function/src/function_env_type_generator.ts @@ -28,6 +28,20 @@ export class FunctionEnvironmentTypeGenerator { `${this.functionName}.ts` ); } + /** + * Clear existing files and subdirectories in the generated env directory + */ + public static clearGeneratedEnvDirectory = (): void => { + const pathToDelete = path.join( + process.cwd(), + '.amplify', + 'generated', + 'env' + ); + if (fs.existsSync(pathToDelete)) { + fs.rmSync(pathToDelete, { recursive: true, force: true }); + } + }; /** * Generate a typed process.env shim @@ -87,21 +101,6 @@ export class FunctionEnvironmentTypeGenerator { this.writeShimFile(content); }; - /** - * Clear existing files and subdirectories in the generated env directory - */ - clearGeneratedEnvDirectory(): void { - const pathToDelete = path.join( - process.cwd(), - '.amplify', - 'generated', - 'env' - ); - if (fs.existsSync(pathToDelete)) { - fs.rmSync(pathToDelete, { recursive: true, force: true }); - } - } - private writeShimFile = (content: string) => { const typeDefFileDirname = path.dirname(this.typeDefFilePath); diff --git a/packages/backend/src/backend_factory.test.ts b/packages/backend/src/backend_factory.test.ts index 7b9840b423..bde5c27760 100644 --- a/packages/backend/src/backend_factory.test.ts +++ b/packages/backend/src/backend_factory.test.ts @@ -10,7 +10,6 @@ import { BackendFactory } from './backend_factory.js'; import { App, Stack } from 'aws-cdk-lib'; import { Template } from 'aws-cdk-lib/assertions'; import { ClientConfig } from '@aws-amplify/client-config'; -import assert from 'node:assert'; const createStackAndSetContext = (deploymentType: DeploymentType): Stack => { const app = new App(); From 863e92eeb11042016bc8d425f1061e1d6349c3d9 Mon Sep 17 00:00:00 2001 From: vigy02 Date: Mon, 23 Sep 2024 10:44:50 -0700 Subject: [PATCH 19/33] fix: added static context to clear the .amplify/generated/env Directory before synthesis --- .changeset/kind-vans-talk.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/kind-vans-talk.md diff --git a/.changeset/kind-vans-talk.md b/.changeset/kind-vans-talk.md new file mode 100644 index 0000000000..bbec5c2ba0 --- /dev/null +++ b/.changeset/kind-vans-talk.md @@ -0,0 +1,6 @@ +--- +'@aws-amplify/backend-function': patch +'@aws-amplify/backend': patch +--- + +chore: added changeset From 9b0fd91181f86220472638d1b3503563b3fed21a Mon Sep 17 00:00:00 2001 From: vigy02 Date: Mon, 23 Sep 2024 10:48:23 -0700 Subject: [PATCH 20/33] fix: added static context to clear the .amplify/generated/env Directory before synthesis --- packages/backend/src/backend_factory.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/backend/src/backend_factory.test.ts b/packages/backend/src/backend_factory.test.ts index bde5c27760..11275219fa 100644 --- a/packages/backend/src/backend_factory.test.ts +++ b/packages/backend/src/backend_factory.test.ts @@ -9,6 +9,7 @@ import { Bucket } from 'aws-cdk-lib/aws-s3'; import { BackendFactory } from './backend_factory.js'; import { App, Stack } from 'aws-cdk-lib'; import { Template } from 'aws-cdk-lib/assertions'; +import assert from 'assert'; import { ClientConfig } from '@aws-amplify/client-config'; const createStackAndSetContext = (deploymentType: DeploymentType): Stack => { From 75a0ee2741443077be82e984dd49b4452b2a5723 Mon Sep 17 00:00:00 2001 From: vigy02 Date: Mon, 23 Sep 2024 11:24:12 -0700 Subject: [PATCH 21/33] fix: Clear generated env directory before shim generation --- .changeset/plenty-islands-tap.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/plenty-islands-tap.md diff --git a/.changeset/plenty-islands-tap.md b/.changeset/plenty-islands-tap.md new file mode 100644 index 0000000000..7a1bf1a2e9 --- /dev/null +++ b/.changeset/plenty-islands-tap.md @@ -0,0 +1,6 @@ +--- +'@aws-amplify/backend-function': patch +'@aws-amplify/backend': patch +--- + +fix: Clear generated env directory before shim generation From 94416a7c2b54df2114ac2f8f9af7541f5fbe2fe4 Mon Sep 17 00:00:00 2001 From: vigy02 Date: Mon, 23 Sep 2024 14:26:36 -0700 Subject: [PATCH 22/33] fix: Clear generated env directory before shim generation --- packages/backend-function/src/factory.ts | 4 +- .../src/function_env_type_generator.test.ts | 43 ++++++++++++++++++- .../src/function_env_type_generator.ts | 9 +++- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/packages/backend-function/src/factory.ts b/packages/backend-function/src/factory.ts index 97c5412aae..574049b0d5 100644 --- a/packages/backend-function/src/factory.ts +++ b/packages/backend-function/src/factory.ts @@ -133,6 +133,7 @@ export type FunctionProps = { */ class FunctionFactory implements ConstructFactory { private generator: ConstructContainerEntryGenerator; + /** * Create a new AmplifyFunctionFactory */ @@ -354,9 +355,6 @@ class AmplifyFunction const functionEnvironmentTypeGenerator = new FunctionEnvironmentTypeGenerator(id); - // Clear the shim generation directory - FunctionEnvironmentTypeGenerator.clearGeneratedEnvDirectory(); - // esbuild runs as part of the NodejsFunction constructor, so we eagerly generate the process env shim without types so it can be included in the function bundle. // This will be overwritten with the typed file at the end of synthesis functionEnvironmentTypeGenerator.generateProcessEnvShim(); diff --git a/packages/backend-function/src/function_env_type_generator.test.ts b/packages/backend-function/src/function_env_type_generator.test.ts index c7204ddd28..8497728dc1 100644 --- a/packages/backend-function/src/function_env_type_generator.test.ts +++ b/packages/backend-function/src/function_env_type_generator.test.ts @@ -4,6 +4,7 @@ import fsp from 'fs/promises'; import { FunctionEnvironmentTypeGenerator } from './function_env_type_generator.js'; import assert from 'assert'; import { pathToFileURL } from 'url'; +import path from 'path'; void describe('FunctionEnvironmentTypeGenerator', () => { void it('generates a type definition file', () => { @@ -73,11 +74,51 @@ void describe('FunctionEnvironmentTypeGenerator', () => { const fsExistsSyncMock = mock.method(fs, 'existsSync', () => true); const fsRmSyncMock = mock.method(fs, 'rmSync', () => {}); - FunctionEnvironmentTypeGenerator.clearGeneratedEnvDirectory(); + const functionEnvironmentTypeGenerator = + new FunctionEnvironmentTypeGenerator('testFunction'); + + assert.equal(fsExistsSyncMock.mock.calls.length, 1); + assert.equal(fsRmSyncMock.mock.calls.length, 1); + + const functionEnvironmentTypeGenerator2 = + new FunctionEnvironmentTypeGenerator('testFunction2'); + + functionEnvironmentTypeGenerator.clearGeneratedEnvDirectory(); + functionEnvironmentTypeGenerator2.clearGeneratedEnvDirectory(); assert.equal(fsExistsSyncMock.mock.calls.length, 1); assert.equal(fsRmSyncMock.mock.calls.length, 1); + fsExistsSyncMock.mock.restore(); + fsRmSyncMock.mock.restore(); + }); + void it("don't clear the generated env directory if it doesn't exist", async () => { + const fsExistsSyncMock = mock.method(fs, 'existsSync', () => false); + const fsRmSyncMock = mock.method(fs, 'rmSync', () => {}); + + new FunctionEnvironmentTypeGenerator('testFunction'); + + assert.equal(fsExistsSyncMock.mock.calls.length, 1); + assert.equal(fsRmSyncMock.mock.calls.length, 0); + + fsExistsSyncMock.mock.restore(); + fsRmSyncMock.mock.restore(); + }); + void it('ensure correct directory is deleted', async () => { + const pathToDelete = path.join( + process.cwd(), + '.amplify', + 'generated', + 'env' + ); + const fsExistsSyncMock = mock.method(fs, 'existsSync', () => true); + const fsRmSyncMock = mock.method(fs, 'rmSync', () => {}); + + new FunctionEnvironmentTypeGenerator('testFunction'); + + assert.equal(fsExistsSyncMock.mock.calls.length, 1); + assert.equal(fsRmSyncMock.mock.calls[0].arguments[0], pathToDelete); + fsExistsSyncMock.mock.restore(); fsRmSyncMock.mock.restore(); }); diff --git a/packages/backend-function/src/function_env_type_generator.ts b/packages/backend-function/src/function_env_type_generator.ts index 60f5838000..974976efa9 100644 --- a/packages/backend-function/src/function_env_type_generator.ts +++ b/packages/backend-function/src/function_env_type_generator.ts @@ -7,6 +7,8 @@ import { EOL } from 'os'; * Generates a typed process.env shim for environment variables */ export class FunctionEnvironmentTypeGenerator { + // Using this to ensure directory is cleared once + private static isEnvDirectoryInitialized = false; private readonly header = '// This file is auto-generated by Amplify. Edits will be overwritten.'; @@ -27,11 +29,15 @@ export class FunctionEnvironmentTypeGenerator { 'env', `${this.functionName}.ts` ); + this.clearGeneratedEnvDirectory(); } /** * Clear existing files and subdirectories in the generated env directory */ - public static clearGeneratedEnvDirectory = (): void => { + public clearGeneratedEnvDirectory = (): void => { + if (FunctionEnvironmentTypeGenerator.isEnvDirectoryInitialized) { + return; + } const pathToDelete = path.join( process.cwd(), '.amplify', @@ -40,6 +46,7 @@ export class FunctionEnvironmentTypeGenerator { ); if (fs.existsSync(pathToDelete)) { fs.rmSync(pathToDelete, { recursive: true, force: true }); + FunctionEnvironmentTypeGenerator.isEnvDirectoryInitialized = true; } }; From cc6968639b1d74acd49d9fdc3568a103db648ac7 Mon Sep 17 00:00:00 2001 From: vigy02 Date: Mon, 23 Sep 2024 15:07:10 -0700 Subject: [PATCH 23/33] fix: Clear generated env directory before shim generation --- package-lock.json | 48 +++++++++---------- .../src/function_env_type_generator.test.ts | 8 +++- .../src/function_env_type_generator.ts | 2 +- 3 files changed, 32 insertions(+), 26 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1b4823b508..016c30bf13 100644 --- a/package-lock.json +++ b/package-lock.json @@ -30952,20 +30952,20 @@ }, "packages/backend": { "name": "@aws-amplify/backend", - "version": "1.2.2", + "version": "1.3.0", "license": "Apache-2.0", "dependencies": { - "@aws-amplify/backend-auth": "^1.1.5", + "@aws-amplify/backend-auth": "^1.2.0", "@aws-amplify/backend-data": "^1.1.4", - "@aws-amplify/backend-function": "^1.4.1", + "@aws-amplify/backend-function": "^1.5.0", "@aws-amplify/backend-output-schemas": "^1.2.0", "@aws-amplify/backend-output-storage": "^1.1.2", "@aws-amplify/backend-secret": "^1.1.2", - "@aws-amplify/backend-storage": "^1.1.3", + "@aws-amplify/backend-storage": "^1.2.0", "@aws-amplify/client-config": "^1.3.1", "@aws-amplify/data-schema": "^1.0.0", "@aws-amplify/platform-core": "^1.1.0", - "@aws-amplify/plugin-types": "^1.2.2", + "@aws-amplify/plugin-types": "^1.3.0", "@aws-sdk/client-amplify": "^3.624.0", "lodash.snakecase": "^4.1.1" }, @@ -30998,12 +30998,12 @@ }, "packages/backend-auth": { "name": "@aws-amplify/backend-auth", - "version": "1.1.5", + "version": "1.2.0", "license": "Apache-2.0", "dependencies": { "@aws-amplify/auth-construct": "^1.3.1", "@aws-amplify/backend-output-storage": "^1.1.2", - "@aws-amplify/plugin-types": "^1.2.2" + "@aws-amplify/plugin-types": "^1.3.0" }, "devDependencies": { "@aws-amplify/backend-platform-test-stubs": "^0.3.5", @@ -31037,7 +31037,7 @@ }, "packages/backend-deployer": { "name": "@aws-amplify/backend-deployer", - "version": "1.1.3", + "version": "1.1.4", "license": "Apache-2.0", "dependencies": { "@aws-amplify/platform-core": "^1.0.6", @@ -31052,12 +31052,12 @@ }, "packages/backend-function": { "name": "@aws-amplify/backend-function", - "version": "1.4.1", + "version": "1.5.0", "license": "Apache-2.0", "dependencies": { "@aws-amplify/backend-output-schemas": "^1.1.0", "@aws-amplify/backend-output-storage": "^1.1.2", - "@aws-amplify/plugin-types": "^1.2.2", + "@aws-amplify/plugin-types": "^1.3.0", "execa": "^8.0.1" }, "devDependencies": { @@ -31122,7 +31122,7 @@ }, "packages/backend-secret": { "name": "@aws-amplify/backend-secret", - "version": "1.1.2", + "version": "1.1.3", "license": "Apache-2.0", "dependencies": { "@aws-amplify/platform-core": "^1.0.5", @@ -31135,12 +31135,12 @@ }, "packages/backend-storage": { "name": "@aws-amplify/backend-storage", - "version": "1.1.3", + "version": "1.2.0", "license": "Apache-2.0", "dependencies": { "@aws-amplify/backend-output-schemas": "^1.2.0", "@aws-amplify/backend-output-storage": "^1.1.2", - "@aws-amplify/plugin-types": "^1.2.2" + "@aws-amplify/plugin-types": "^1.3.0" }, "devDependencies": { "@aws-amplify/backend-platform-test-stubs": "^0.3.5", @@ -31153,7 +31153,7 @@ }, "packages/cli": { "name": "@aws-amplify/backend-cli", - "version": "1.2.7", + "version": "1.2.8", "license": "Apache-2.0", "dependencies": { "@aws-amplify/backend-deployer": "^1.1.3", @@ -31162,12 +31162,12 @@ "@aws-amplify/cli-core": "^1.1.3", "@aws-amplify/client-config": "^1.3.1", "@aws-amplify/deployed-backend-client": "^1.4.1", - "@aws-amplify/form-generator": "^1.0.2", - "@aws-amplify/model-generator": "^1.0.7", + "@aws-amplify/form-generator": "^1.0.3", + "@aws-amplify/model-generator": "^1.0.8", "@aws-amplify/platform-core": "^1.0.5", - "@aws-amplify/plugin-types": "^1.2.2", + "@aws-amplify/plugin-types": "^1.3.0", "@aws-amplify/sandbox": "^1.2.2", - "@aws-amplify/schema-generator": "^1.2.3", + "@aws-amplify/schema-generator": "^1.2.4", "@aws-sdk/client-amplify": "^3.624.0", "@aws-sdk/client-cloudformation": "^3.624.0", "@aws-sdk/client-s3": "^3.624.0", @@ -31307,7 +31307,7 @@ }, "packages/client-config": { "name": "@aws-amplify/client-config", - "version": "1.3.1", + "version": "1.3.2", "license": "Apache-2.0", "dependencies": { "@aws-amplify/backend-output-schemas": "^1.2.0", @@ -31472,7 +31472,7 @@ }, "packages/form-generator": { "name": "@aws-amplify/form-generator", - "version": "1.0.2", + "version": "1.0.3", "license": "Apache-2.0", "dependencies": { "@aws-amplify/appsync-modelgen-plugin": "^2.11.0", @@ -31551,7 +31551,7 @@ }, "packages/model-generator": { "name": "@aws-amplify/model-generator", - "version": "1.0.7", + "version": "1.0.8", "license": "Apache-2.0", "dependencies": { "@aws-amplify/backend-output-schemas": "^1.1.0", @@ -31559,7 +31559,7 @@ "@aws-amplify/graphql-generator": "^0.4.0", "@aws-amplify/graphql-types-generator": "^3.6.0", "@aws-amplify/platform-core": "^1.0.5", - "@aws-amplify/plugin-types": "^1.2.2", + "@aws-amplify/plugin-types": "^1.3.0", "@aws-sdk/client-appsync": "^3.624.0", "@aws-sdk/client-s3": "^3.624.0", "@aws-sdk/credential-providers": "^3.624.0", @@ -31605,7 +31605,7 @@ }, "packages/plugin-types": { "name": "@aws-amplify/plugin-types", - "version": "1.2.2", + "version": "1.3.0", "license": "Apache-2.0", "devDependencies": { "execa": "^5.1.1" @@ -31767,7 +31767,7 @@ }, "packages/schema-generator": { "name": "@aws-amplify/schema-generator", - "version": "1.2.3", + "version": "1.2.4", "license": "Apache-2.0", "dependencies": { "@aws-amplify/graphql-schema-generator": "^0.9.4", diff --git a/packages/backend-function/src/function_env_type_generator.test.ts b/packages/backend-function/src/function_env_type_generator.test.ts index 8497728dc1..19164319b4 100644 --- a/packages/backend-function/src/function_env_type_generator.test.ts +++ b/packages/backend-function/src/function_env_type_generator.test.ts @@ -1,4 +1,4 @@ -import { describe, it, mock } from 'node:test'; +import { beforeEach, describe, it, mock } from 'node:test'; import fs from 'fs'; import fsp from 'fs/promises'; import { FunctionEnvironmentTypeGenerator } from './function_env_type_generator.js'; @@ -7,6 +7,9 @@ import { pathToFileURL } from 'url'; import path from 'path'; void describe('FunctionEnvironmentTypeGenerator', () => { + beforeEach(() => { + resetFactoryCount(); + }); void it('generates a type definition file', () => { const fsOpenSyncMock = mock.method(fs, 'openSync'); const fsWriteFileSyncMock = mock.method(fs, 'writeFileSync', () => null); @@ -122,4 +125,7 @@ void describe('FunctionEnvironmentTypeGenerator', () => { fsExistsSyncMock.mock.restore(); fsRmSyncMock.mock.restore(); }); + const resetFactoryCount = () => { + FunctionEnvironmentTypeGenerator.isEnvDirectoryInitialized = false; + }; }); diff --git a/packages/backend-function/src/function_env_type_generator.ts b/packages/backend-function/src/function_env_type_generator.ts index 974976efa9..a6c5effe65 100644 --- a/packages/backend-function/src/function_env_type_generator.ts +++ b/packages/backend-function/src/function_env_type_generator.ts @@ -8,7 +8,7 @@ import { EOL } from 'os'; */ export class FunctionEnvironmentTypeGenerator { // Using this to ensure directory is cleared once - private static isEnvDirectoryInitialized = false; + static isEnvDirectoryInitialized = false; private readonly header = '// This file is auto-generated by Amplify. Edits will be overwritten.'; From f4348e61e76e156d25d06c5fa742d42a57abf143 Mon Sep 17 00:00:00 2001 From: vigy02 Date: Mon, 23 Sep 2024 15:08:01 -0700 Subject: [PATCH 24/33] fix: Clear generated env directory before shim generation --- .changeset/rare-seas-eat.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/rare-seas-eat.md diff --git a/.changeset/rare-seas-eat.md b/.changeset/rare-seas-eat.md new file mode 100644 index 0000000000..59044415fc --- /dev/null +++ b/.changeset/rare-seas-eat.md @@ -0,0 +1,5 @@ +--- +'@aws-amplify/backend-function': patch +--- + +Clear generated env directory before shim generation From b886eed814f34315c7d156434dec0793161d9e28 Mon Sep 17 00:00:00 2001 From: vigy02 Date: Mon, 23 Sep 2024 15:11:18 -0700 Subject: [PATCH 25/33] fix: Clear generated env directory before shim generation --- .changeset/lovely-cars-carry.md | 6 ------ .changeset/lovely-plums-beam.md | 5 ----- .changeset/red-badgers-shop.md | 6 ------ .changeset/sixty-bobcats-sing.md | 5 ----- .changeset/wise-panthers-smile.md | 5 +++++ 5 files changed, 5 insertions(+), 22 deletions(-) delete mode 100644 .changeset/lovely-cars-carry.md delete mode 100644 .changeset/lovely-plums-beam.md delete mode 100644 .changeset/red-badgers-shop.md delete mode 100644 .changeset/sixty-bobcats-sing.md create mode 100644 .changeset/wise-panthers-smile.md diff --git a/.changeset/lovely-cars-carry.md b/.changeset/lovely-cars-carry.md deleted file mode 100644 index 524d6fa2fd..0000000000 --- a/.changeset/lovely-cars-carry.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -'@aws-amplify/backend-function': patch -'@aws-amplify/backend': patch ---- - -Fix: Clear generated env directory before shim generation diff --git a/.changeset/lovely-plums-beam.md b/.changeset/lovely-plums-beam.md deleted file mode 100644 index 7c96db3a5d..0000000000 --- a/.changeset/lovely-plums-beam.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@aws-amplify/backend': patch ---- - -fix: delete generated folder at start of synth diff --git a/.changeset/red-badgers-shop.md b/.changeset/red-badgers-shop.md deleted file mode 100644 index ddf1633493..0000000000 --- a/.changeset/red-badgers-shop.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -'@aws-amplify/backend-function': patch -'@aws-amplify/backend': patch ---- - -Clear the shim generation directory diff --git a/.changeset/sixty-bobcats-sing.md b/.changeset/sixty-bobcats-sing.md deleted file mode 100644 index 18efac4213..0000000000 --- a/.changeset/sixty-bobcats-sing.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@aws-amplify/backend': patch ---- - -fix: clearing the .amplify/generated/env/ before synthesis diff --git a/.changeset/wise-panthers-smile.md b/.changeset/wise-panthers-smile.md new file mode 100644 index 0000000000..59044415fc --- /dev/null +++ b/.changeset/wise-panthers-smile.md @@ -0,0 +1,5 @@ +--- +'@aws-amplify/backend-function': patch +--- + +Clear generated env directory before shim generation From 4f2d18c401ba6801cfa979d7ff37e0d170da315d Mon Sep 17 00:00:00 2001 From: vigy02 Date: Mon, 23 Sep 2024 16:43:38 -0700 Subject: [PATCH 26/33] fix: added static context to clear the .amplify/generated/env Directory before synthesis --- .changeset/kind-vans-talk.md | 6 ------ .changeset/plenty-islands-tap.md | 6 ------ .changeset/seven-rabbits-poke.md | 2 -- .changeset/wise-panthers-smile.md | 5 ----- packages/backend/src/backend_factory.test.ts | 2 +- 5 files changed, 1 insertion(+), 20 deletions(-) delete mode 100644 .changeset/kind-vans-talk.md delete mode 100644 .changeset/plenty-islands-tap.md delete mode 100644 .changeset/seven-rabbits-poke.md delete mode 100644 .changeset/wise-panthers-smile.md diff --git a/.changeset/kind-vans-talk.md b/.changeset/kind-vans-talk.md deleted file mode 100644 index bbec5c2ba0..0000000000 --- a/.changeset/kind-vans-talk.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -'@aws-amplify/backend-function': patch -'@aws-amplify/backend': patch ---- - -chore: added changeset diff --git a/.changeset/plenty-islands-tap.md b/.changeset/plenty-islands-tap.md deleted file mode 100644 index 7a1bf1a2e9..0000000000 --- a/.changeset/plenty-islands-tap.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -'@aws-amplify/backend-function': patch -'@aws-amplify/backend': patch ---- - -fix: Clear generated env directory before shim generation diff --git a/.changeset/seven-rabbits-poke.md b/.changeset/seven-rabbits-poke.md deleted file mode 100644 index a845151cc8..0000000000 --- a/.changeset/seven-rabbits-poke.md +++ /dev/null @@ -1,2 +0,0 @@ ---- ---- diff --git a/.changeset/wise-panthers-smile.md b/.changeset/wise-panthers-smile.md deleted file mode 100644 index 59044415fc..0000000000 --- a/.changeset/wise-panthers-smile.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@aws-amplify/backend-function': patch ---- - -Clear generated env directory before shim generation diff --git a/packages/backend/src/backend_factory.test.ts b/packages/backend/src/backend_factory.test.ts index 2a4a9ec510..268bede3ff 100644 --- a/packages/backend/src/backend_factory.test.ts +++ b/packages/backend/src/backend_factory.test.ts @@ -9,7 +9,7 @@ import { Bucket } from 'aws-cdk-lib/aws-s3'; import { BackendFactory } from './backend_factory.js'; import { App, Stack } from 'aws-cdk-lib'; import { Template } from 'aws-cdk-lib/assertions'; -import assert from 'assert'; +import assert from 'node:assert'; import { ClientConfig } from '@aws-amplify/client-config'; const createStackAndSetContext = (deploymentType: DeploymentType): Stack => { From 7f78fc89612ebe34fe3dce1021c0471792a56d23 Mon Sep 17 00:00:00 2001 From: vigy02 Date: Mon, 23 Sep 2024 16:51:18 -0700 Subject: [PATCH 27/33] fix: added static context to clear the .amplify/generated/env Directory before synthesis --- .changeset/seven-rabbits-poke.md | 2 ++ packages/backend-function/src/factory.ts | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 .changeset/seven-rabbits-poke.md diff --git a/.changeset/seven-rabbits-poke.md b/.changeset/seven-rabbits-poke.md new file mode 100644 index 0000000000..a845151cc8 --- /dev/null +++ b/.changeset/seven-rabbits-poke.md @@ -0,0 +1,2 @@ +--- +--- diff --git a/packages/backend-function/src/factory.ts b/packages/backend-function/src/factory.ts index 574049b0d5..46e66c15c4 100644 --- a/packages/backend-function/src/factory.ts +++ b/packages/backend-function/src/factory.ts @@ -133,7 +133,6 @@ export type FunctionProps = { */ class FunctionFactory implements ConstructFactory { private generator: ConstructContainerEntryGenerator; - /** * Create a new AmplifyFunctionFactory */ From 5d0d04af10dd94ecf1f6e7e6863d30916c40ccc2 Mon Sep 17 00:00:00 2001 From: vigy02 Date: Tue, 24 Sep 2024 11:36:22 -0700 Subject: [PATCH 28/33] fix: Clear generated env directory before shim generation --- .../backend-function/src/function_env_type_generator.test.ts | 4 ++-- packages/backend-function/src/function_env_type_generator.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/backend-function/src/function_env_type_generator.test.ts b/packages/backend-function/src/function_env_type_generator.test.ts index 19164319b4..1f52d77c9c 100644 --- a/packages/backend-function/src/function_env_type_generator.test.ts +++ b/packages/backend-function/src/function_env_type_generator.test.ts @@ -8,7 +8,7 @@ import path from 'path'; void describe('FunctionEnvironmentTypeGenerator', () => { beforeEach(() => { - resetFactoryCount(); + resetFactoryGlobalState(); }); void it('generates a type definition file', () => { const fsOpenSyncMock = mock.method(fs, 'openSync'); @@ -125,7 +125,7 @@ void describe('FunctionEnvironmentTypeGenerator', () => { fsExistsSyncMock.mock.restore(); fsRmSyncMock.mock.restore(); }); - const resetFactoryCount = () => { + const resetFactoryGlobalState = () => { FunctionEnvironmentTypeGenerator.isEnvDirectoryInitialized = false; }; }); diff --git a/packages/backend-function/src/function_env_type_generator.ts b/packages/backend-function/src/function_env_type_generator.ts index a6c5effe65..b7efff21e2 100644 --- a/packages/backend-function/src/function_env_type_generator.ts +++ b/packages/backend-function/src/function_env_type_generator.ts @@ -7,7 +7,7 @@ import { EOL } from 'os'; * Generates a typed process.env shim for environment variables */ export class FunctionEnvironmentTypeGenerator { - // Using this to ensure directory is cleared once + // Using this to ensure directory is cleared once per synthesis static isEnvDirectoryInitialized = false; private readonly header = '// This file is auto-generated by Amplify. Edits will be overwritten.'; From 34eb8208ce5cf687ee9375a54370b0f0fc3d1086 Mon Sep 17 00:00:00 2001 From: vigy02 Date: Tue, 24 Sep 2024 11:53:40 -0700 Subject: [PATCH 29/33] fix: Clear generated env directory before shim generation --- .../src/function_env_type_generator.test.ts | 9 ++--- .../src/function_env_type_generator.ts | 36 +++++++++---------- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/packages/backend-function/src/function_env_type_generator.test.ts b/packages/backend-function/src/function_env_type_generator.test.ts index 1f52d77c9c..97fad7f0fe 100644 --- a/packages/backend-function/src/function_env_type_generator.test.ts +++ b/packages/backend-function/src/function_env_type_generator.test.ts @@ -77,17 +77,12 @@ void describe('FunctionEnvironmentTypeGenerator', () => { const fsExistsSyncMock = mock.method(fs, 'existsSync', () => true); const fsRmSyncMock = mock.method(fs, 'rmSync', () => {}); - const functionEnvironmentTypeGenerator = - new FunctionEnvironmentTypeGenerator('testFunction'); + new FunctionEnvironmentTypeGenerator('testFunction1'); assert.equal(fsExistsSyncMock.mock.calls.length, 1); assert.equal(fsRmSyncMock.mock.calls.length, 1); - const functionEnvironmentTypeGenerator2 = - new FunctionEnvironmentTypeGenerator('testFunction2'); - - functionEnvironmentTypeGenerator.clearGeneratedEnvDirectory(); - functionEnvironmentTypeGenerator2.clearGeneratedEnvDirectory(); + new FunctionEnvironmentTypeGenerator('testFunction2'); assert.equal(fsExistsSyncMock.mock.calls.length, 1); assert.equal(fsRmSyncMock.mock.calls.length, 1); diff --git a/packages/backend-function/src/function_env_type_generator.ts b/packages/backend-function/src/function_env_type_generator.ts index b7efff21e2..7302bc75ba 100644 --- a/packages/backend-function/src/function_env_type_generator.ts +++ b/packages/backend-function/src/function_env_type_generator.ts @@ -31,24 +31,6 @@ export class FunctionEnvironmentTypeGenerator { ); this.clearGeneratedEnvDirectory(); } - /** - * Clear existing files and subdirectories in the generated env directory - */ - public clearGeneratedEnvDirectory = (): void => { - if (FunctionEnvironmentTypeGenerator.isEnvDirectoryInitialized) { - return; - } - const pathToDelete = path.join( - process.cwd(), - '.amplify', - 'generated', - 'env' - ); - if (fs.existsSync(pathToDelete)) { - fs.rmSync(pathToDelete, { recursive: true, force: true }); - FunctionEnvironmentTypeGenerator.isEnvDirectoryInitialized = true; - } - }; /** * Generate a typed process.env shim @@ -117,4 +99,22 @@ export class FunctionEnvironmentTypeGenerator { fs.writeFileSync(this.typeDefFilePath, content); }; + /** + * Clear existing files and subdirectories in the generated env directory + */ + private clearGeneratedEnvDirectory = (): void => { + if (FunctionEnvironmentTypeGenerator.isEnvDirectoryInitialized) { + return; + } + const pathToDelete = path.join( + process.cwd(), + '.amplify', + 'generated', + 'env' + ); + if (fs.existsSync(pathToDelete)) { + fs.rmSync(pathToDelete, { recursive: true, force: true }); + FunctionEnvironmentTypeGenerator.isEnvDirectoryInitialized = true; + } + }; } From 223ced48024a6c4b25129368eab0517963e502f7 Mon Sep 17 00:00:00 2001 From: vigy02 Date: Tue, 24 Sep 2024 14:43:22 -0700 Subject: [PATCH 30/33] fix: Clear generated env directory before shim generation --- .changeset/dirty-years-attend.md | 6 ++++++ .changeset/rare-seas-eat.md | 5 ----- packages/backend-function/CHANGELOG.md | 6 ++++++ packages/backend-function/package.json | 2 +- 4 files changed, 13 insertions(+), 6 deletions(-) create mode 100644 .changeset/dirty-years-attend.md delete mode 100644 .changeset/rare-seas-eat.md diff --git a/.changeset/dirty-years-attend.md b/.changeset/dirty-years-attend.md new file mode 100644 index 0000000000..7a1bf1a2e9 --- /dev/null +++ b/.changeset/dirty-years-attend.md @@ -0,0 +1,6 @@ +--- +'@aws-amplify/backend-function': patch +'@aws-amplify/backend': patch +--- + +fix: Clear generated env directory before shim generation diff --git a/.changeset/rare-seas-eat.md b/.changeset/rare-seas-eat.md deleted file mode 100644 index 59044415fc..0000000000 --- a/.changeset/rare-seas-eat.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@aws-amplify/backend-function': patch ---- - -Clear generated env directory before shim generation diff --git a/packages/backend-function/CHANGELOG.md b/packages/backend-function/CHANGELOG.md index 6ef7160de5..f977ebde4b 100644 --- a/packages/backend-function/CHANGELOG.md +++ b/packages/backend-function/CHANGELOG.md @@ -1,5 +1,11 @@ # @aws-amplify/backend-function +## 1.5.1 + +### Patch Changes + +- f4348e6: Clear generated env directory before shim generation + ## 1.5.0 ### Minor Changes diff --git a/packages/backend-function/package.json b/packages/backend-function/package.json index 097ad1f416..a43ab95f81 100644 --- a/packages/backend-function/package.json +++ b/packages/backend-function/package.json @@ -1,6 +1,6 @@ { "name": "@aws-amplify/backend-function", - "version": "1.5.0", + "version": "1.5.1", "type": "module", "publishConfig": { "access": "public" From 7fc8e13ff8213c4e1a2477c00d9f4641ceafc447 Mon Sep 17 00:00:00 2001 From: vigy02 Date: Tue, 24 Sep 2024 18:27:15 -0700 Subject: [PATCH 31/33] fix: Clear generated env directory before shim generation --- packages/backend-function/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/backend-function/package.json b/packages/backend-function/package.json index a43ab95f81..097ad1f416 100644 --- a/packages/backend-function/package.json +++ b/packages/backend-function/package.json @@ -1,6 +1,6 @@ { "name": "@aws-amplify/backend-function", - "version": "1.5.1", + "version": "1.5.0", "type": "module", "publishConfig": { "access": "public" From cfd19f7ca06ac4dab8adafd1297cef2eb99923b7 Mon Sep 17 00:00:00 2001 From: vigy02 Date: Tue, 24 Sep 2024 19:01:41 -0700 Subject: [PATCH 32/33] fix: Clear generated env directory before shim generation --- packages/backend-function/CHANGELOG.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/packages/backend-function/CHANGELOG.md b/packages/backend-function/CHANGELOG.md index f977ebde4b..6ef7160de5 100644 --- a/packages/backend-function/CHANGELOG.md +++ b/packages/backend-function/CHANGELOG.md @@ -1,11 +1,5 @@ # @aws-amplify/backend-function -## 1.5.1 - -### Patch Changes - -- f4348e6: Clear generated env directory before shim generation - ## 1.5.0 ### Minor Changes From 40478034351caeb43e9ce4477190a7ad494ce7fd Mon Sep 17 00:00:00 2001 From: vigy02 Date: Mon, 7 Oct 2024 17:28:47 -0700 Subject: [PATCH 33/33] Update the test case description --- .../backend-function/src/function_env_type_generator.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/backend-function/src/function_env_type_generator.test.ts b/packages/backend-function/src/function_env_type_generator.test.ts index 97fad7f0fe..0f950e2e31 100644 --- a/packages/backend-function/src/function_env_type_generator.test.ts +++ b/packages/backend-function/src/function_env_type_generator.test.ts @@ -73,7 +73,7 @@ void describe('FunctionEnvironmentTypeGenerator', () => { await fsp.rm(targetDirectory, { recursive: true, force: true }); }); - void it('clears the generated env directory', async () => { + void it('clears the generated env directory even if there are multiple calls', async () => { const fsExistsSyncMock = mock.method(fs, 'existsSync', () => true); const fsRmSyncMock = mock.method(fs, 'rmSync', () => {});