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

fix: added static context to clear the .amplify/generated/env Directory before synthesis #2044

Open
wants to merge 39 commits into
base: main
Choose a base branch
from

Conversation

vigy02
Copy link
Contributor

@vigy02 vigy02 commented Sep 23, 2024

Problem

The .amplify/generated/env/ directory is not cleared before deployments, causing outdated/renamed environment files to remain. This can lead to confusion and potential errors when the function's handler code does not match the current environment settings.

@vigy02 vigy02 added the run-e2e Label that will include e2e tests in PR checks workflow label Sep 23, 2024
@vigy02 vigy02 requested a review from a team as a code owner September 23, 2024 23:54
Copy link

changeset-bot bot commented Sep 23, 2024

🦋 Changeset detected

Latest commit: cfd19f7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@aws-amplify/backend-function Patch
@aws-amplify/backend Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Member

@sobolk sobolk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I like the approach.

Couple of naming comments.

@@ -7,10 +7,11 @@ import { EOL } from 'os';
* Generates a typed process.env shim for environment variables
*/
export class FunctionEnvironmentTypeGenerator {
// Using this to ensure directory is cleared once
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Using this to ensure directory is cleared once
// Using this to ensure directory is cleared once per synthesis

/**
* Clear existing files and subdirectories in the generated env directory
*/
public clearGeneratedEnvDirectory = (): void => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be private ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't be private since we're using this in test class

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're calling this in ctor.

This test could be changed to just use multiple ctor calls instead and hide it.

image

fsExistsSyncMock.mock.restore();
fsRmSyncMock.mock.restore();
});
const resetFactoryCount = () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const resetFactoryCount = () => {
const resetFactoryGlobalState = () => {

/**
* Clear existing files and subdirectories in the generated env directory
*/
public clearGeneratedEnvDirectory = (): void => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're calling this in ctor.

This test could be changed to just use multiple ctor calls instead and hide it.

image

Comment on lines 83 to 93
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);
assert.equal(fsExistsSyncMock.mock.calls.length, 1);
assert.equal(fsRmSyncMock.mock.calls.length, 1);
const functionEnvironmentTypeGenerator2 =
new FunctionEnvironmentTypeGenerator('testFunction2');
assert.equal(fsExistsSyncMock.mock.calls.length, 1);
assert.equal(fsRmSyncMock.mock.calls.length, 1);

calling ctor multiple times should be good enough.

sobolk
sobolk previously approved these changes Sep 24, 2024
Comment on lines 1 to 3
---
'@aws-amplify/backend-function': patch
---
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
---
'@aws-amplify/backend-function': patch
---
---
'@aws-amplify/backend': patch
'@aws-amplify/backend-function': patch
---

Looks like new check is working (#2042)

awsluja
awsluja previously approved these changes Sep 24, 2024
@vigy02 vigy02 added run-e2e Label that will include e2e tests in PR checks workflow and removed run-e2e Label that will include e2e tests in PR checks workflow labels Sep 24, 2024
@awsluja awsluja closed this Sep 24, 2024
@awsluja awsluja reopened this Sep 24, 2024
@vigy02 vigy02 dismissed stale reviews from awsluja and sobolk via 223ced4 September 24, 2024 21:49
@vigy02 vigy02 requested a review from a team as a code owner September 24, 2024 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
run-e2e Label that will include e2e tests in PR checks workflow
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants