aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.69k stars 3.93k forks source link

Synth NodejsFunction: Error: ENOTEMPTY: directory not empty, rmdir #32098

Open rupe120 opened 1 week ago

rupe120 commented 1 week ago

Describe the bug

cdk synth fails if a prior synth exists in cdk.out with the error:

Error: ENOTEMPTY: directory not empty, rmdir '\\?\C:\CodeCommit\project\group-document-processing\cdk.out\bundling-temp-dc725a4c4531f07cc12d4a763596738b6f71449ea4146dc0c69217eb5b7c5f27'
    at Object.rmdirSync (node:fs:1219:10)
    at _rmdirSync (node:internal/fs/rimraf:260:21)
    at rimrafSync (node:internal/fs/rimraf:193:7)
    at Object.rmSync (node:fs:1268:10)
    at Object.removeSync (C:\CodeCommit\project\group-document-processing\node_modules\aws-cdk-lib\node_modules\fs-extra\lib\remove\index.js:11:6)   
    at AssetStaging.stageAsset (C:\CodeCommit\project\group-document-processing\node_modules\aws-cdk-lib\core\lib\asset-staging.js:1:6427)
    at AssetStaging.stageByBundling (C:\CodeCommit\project\group-document-processing\node_modules\aws-cdk-lib\core\lib\asset-staging.js:1:5777)      
    at stageThisAsset (C:\CodeCommit\project\group-document-processing\node_modules\aws-cdk-lib\core\lib\asset-staging.js:1:2728)
    at Cache.obtain (C:\CodeCommit\project\group-document-processing\node_modules\aws-cdk-lib\core\lib\private\cache.js:1:242)
    at new AssetStaging (C:\CodeCommit\project\group-document-processing\node_modules\aws-cdk-lib\core\lib\asset-staging.js:1:3125) {
  errno: -4051,
  syscall: 'rmdir',
  code: 'ENOTEMPTY',
  path: '\\\\?\\C:\\CodeCommit\\project\\group-document-processing\\cdk.out\\bundling-temp-dc725a4c4531f07cc12d4a763596738b6f71449ea4146dc0c69217eb5b7c5f27'

Regression Issue

Last Known Working CDK Version

Unknown, I tried prior versions and was not able to find one that worked. This was working for me about a week ago and then suddenly stopped.

Expected Behavior

A successful synth

Current Behavior

See description

Reproduction Steps

I have not had the time to rebuild a project to see if it happens with the lambda definition in a fresh project.

This is the lambda definition though:

    const testGetTextractTextDetectionLambda = new NodejsFunction(this, 'test-get-textract-text-detection',
    {
      codeSigningConfig: props.currentEnvironment.deployEnvironment === "dev" ? undefined : codeSigningConfig,
      functionName:`test-get-textract-text-detection-${props.currentEnvironment.deployEnvironment}`,
      description: `Get Textract text detection`,
      runtime: Runtime.NODEJS_20_X,
      entry: 'lib/lambda-stack.test-get-from-textract-text-detection.ts',
      handler: "lambdaHandler",
      timeout: cdk.Duration.minutes(15),
      tracing: Tracing.ACTIVE,
      insightsVersion: LambdaInsightsVersion.VERSION_1_0_143_0,
      deadLetterQueue: dlQueue,
      deadLetterQueueEnabled: true,
      memorySize: 1024,
      environment: {
        REGION_ID: props.env!.region!,
        SOURCE_BUCKET_PATH: props.currentEnvironment.sourceBucketPrefix,
        DOCUMENT_DATE_ANALYSIS_QUESTION_ALIAS: documentDateAnalysisQuestionAlias,
        DOCUMENT_DATE_ANALYSIS_QUESTION: props.appConfig.documentDateAnalysisQuestion,
        EMPLOYER_NAME_ANALYSIS_QUESTION_ALIAS: employerNameAnalysisQuestionAlias,
        EMPLOYER_NAME_ANALYSIS_QUESTION: props.appConfig.employerNameAnalysisQuestion,
        CHAMBER_OF_COMMERCE_ANALYSIS_QUESTION_ALIAS: chamberOfCommerceAnalysisQuestionAlias,
        CHAMBER_OF_COMMERCE_ANALYSIS_QUESTION: props.appConfig.chamberOfCommerceAnalysisQuestion,
        ENVIRONMENT: props.currentEnvironment.deployEnvironment,
        NODE_ENV: "test"
      }
    });
    sourceBucket.grantReadWrite(testGetTextractTextDetectionLambda);
    testGetTextractTextDetectionLambda.addToRolePolicy(new PolicyStatement(
      { 
        actions: [
          'textract:GetDocumentTextDetection'
        ],
        resources: ['*']
      }
    ));

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.166.0

Framework Version

2.166.0

Node.js Version

v20.10.0

OS

Windows 11 Enterprise 22621.4169

Language

TypeScript

Language Version

4.9.5

Other information

No response

khushail commented 1 week ago

Hi @rupe120 , thanks for reaching out.

I tried to repro the issue with this minimal code of Lambda node js function and its succeeding with synth as well as deployment. Sharing the details -

Code -

const dlQueue = new sqs.Queue(this, 'dlQueue', {
      queueName: 'dlQueue'
    });

    const testGetTextractTextDetectionLambda = new NodejsFunction(this, 'test-get-textract-text-detection',
    {
      codeSigningConfig: undefined,
      functionName:'test-get-textract-text-detection',
      description: `Get Textract text detection`,
      runtime: Runtime.NODEJS_20_X,
      timeout: cdk.Duration.minutes(15),
      entry: 'lib/index.ts',
      handler: 'index.handler',
      tracing: Tracing.ACTIVE,
      insightsVersion: LambdaInsightsVersion.VERSION_1_0_143_0,
      deadLetterQueue: dlQueue,
      deadLetterQueueEnabled: true,
      memorySize: 1024,
      environment: {
        NODE_ENV: "test"
      }
    });
    const sourceBucket = new s3.Bucket(this, 'sourceBuckettestDeploy', {
      bucketName: 'source-bucket-test-deploy',
      removalPolicy: cdk.RemovalPolicy.DESTROY
    });

    sourceBucket.grantReadWrite(testGetTextractTextDetectionLambda);
    testGetTextractTextDetectionLambda.addToRolePolicy(new PolicyStatement(
      { 
        actions: [
          'textract:GetDocumentTextDetection'
        ],
        resources: ['*']
      }
    ));
    //added these statement after 1st synth
    new cdk.CfnOutput(this, 'testGetTextractTextDetectionLambda', { value: testGetTextractTextDetectionLambda.functionArn });
    new cdk.CfnOutput(this, 'sourceBucket', { value: sourceBucket.bucketName });
  }

the code synthesized successfully and I am able to see the cdk diff as well -

Screenshot 2024-11-12 at 1 22 20 PM

used cdk version - 2.166.0 Node - v20.17.0

As I am not able to repro the scenario, could you please share the minimal self contained code to repro the issue. Also I would suggest you to check the NodeJS version and CDK version as well.

github-actions[bot] commented 5 days ago

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

rupe120 commented 5 days ago

Hi @rupe120 , thanks for reaching out.

I tried to repro the issue with this minimal code of Lambda node js function and its succeeding with synth as well as deployment. Sharing the details -

Code -

const dlQueue = new sqs.Queue(this, 'dlQueue', {
      queueName: 'dlQueue'
    });

    const testGetTextractTextDetectionLambda = new NodejsFunction(this, 'test-get-textract-text-detection',
    {
      codeSigningConfig: undefined,
      functionName:'test-get-textract-text-detection',
      description: `Get Textract text detection`,
      runtime: Runtime.NODEJS_20_X,
      timeout: cdk.Duration.minutes(15),
      entry: 'lib/index.ts',
      handler: 'index.handler',
      tracing: Tracing.ACTIVE,
      insightsVersion: LambdaInsightsVersion.VERSION_1_0_143_0,
      deadLetterQueue: dlQueue,
      deadLetterQueueEnabled: true,
      memorySize: 1024,
      environment: {
        NODE_ENV: "test"
      }
    });
    const sourceBucket = new s3.Bucket(this, 'sourceBuckettestDeploy', {
      bucketName: 'source-bucket-test-deploy',
      removalPolicy: cdk.RemovalPolicy.DESTROY
    });

    sourceBucket.grantReadWrite(testGetTextractTextDetectionLambda);
    testGetTextractTextDetectionLambda.addToRolePolicy(new PolicyStatement(
      { 
        actions: [
          'textract:GetDocumentTextDetection'
        ],
        resources: ['*']
      }
    ));
    //added these statement after 1st synth
    new cdk.CfnOutput(this, 'testGetTextractTextDetectionLambda', { value: testGetTextractTextDetectionLambda.functionArn });
    new cdk.CfnOutput(this, 'sourceBucket', { value: sourceBucket.bucketName });
  }

the code synthesized successfully and I am able to see the cdk diff as well -

Screenshot 2024-11-12 at 1 22 20 PM

used cdk version - 2.166.0 Node - v20.17.0

As I am not able to repro the scenario, could you please share the minimal self contained code to repro the issue. Also I would suggest you to check the NodeJS version and CDK version as well.

This seems to be a Windows issue. It works fine, using the same path in WSL