Skip to content

Conversation

@pahud
Copy link
Contributor

@pahud pahud commented Oct 22, 2025

Issue # (if applicable)

Closes #35809.

Reason for this change

Source.data() fails with a TypeError when called with an empty string as the data parameter in CDK v2.207.0+. This is a regression from v2.206.0 where empty string deployment worked correctly.

The issue manifests as:

TypeError: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined
    at Object.writeFileSync (node:fs:2434:5)

This prevents users from deploying empty S3 objects using Source.data("path", ""), which is a valid use case for placeholder files, configuration templates, or initialization markers.

Description of changes

Fixed the empty string handling issue by adding explicit empty string handling in the S3 deployment renderData() function. This targeted approach avoids modifying core CDK libraries and keeps the fix scoped to where the issue manifests.

Root cause: When Source.data("path", "") is processed during synthesis, the empty string goes through the renderData("") function. For empty strings, TokenizedStringFragments has zero fragments, causing join() to call StringConcat.join(undefined, undefined), which returns undefined instead of the expected empty string. This undefined value eventually reaches fs.writeFileSync(), causing the TypeError.

Technical changes:

  • Modified renderData() in packages/aws-cdk-lib/aws-s3-deployment/lib/render-data.ts
  • Added explicit early return for empty string input: if (data === '') return { text: '', markers: {} }
  • Added regression test for the exact reproduction case Source.data('path/to/empty', '')
  • Zero changes to core CDK libraries - fix is isolated to S3 deployment module only

Impact: Restores v2.206.0 behavior for empty string deployment while maintaining a minimal, targeted fix with no risk to other CDK functionality.

Describe any new or updated permissions being added

N/A - No IAM permissions or resource access changes. This is an internal S3 deployment module fix that only affects synthesis-time data rendering for empty strings.

Description of how you validated changes

  • Unit tests: Added test for empty string handling in renderData() function
  • Regression tests: Added test for exact reproduction case Source.data('path/to/empty', '') in packages/aws-cdk-lib/aws-s3-deployment/test/content.test.ts
  • Integration tests: All existing CloudFormation snapshots unchanged (as expected for synthesis-time fix)
  • Manual validation: End-to-end verification that Source.data("empty.txt", "") works without throwing TypeError
  • Scope validation: Confirmed no changes needed to core CDK libraries or other modules
  • Build verification: TypeScript compilation, linting, and module builds all pass

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

- Add explicit handling for empty string input in renderData
- Prevent potential errors when processing empty string data
- Add regression test for empty string Source.data scenario
- Resolve issue aws#35809 related to empty string data handling
@github-actions github-actions bot added bug This issue is a bug. p0 labels Oct 22, 2025
@aws-cdk-automation aws-cdk-automation requested a review from a team October 22, 2025 15:53
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Oct 22, 2025
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

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

The pull request linter fails with the following errors:

❌ Fixes must contain a change to an integration test file and the resulting snapshot.

If you believe this pull request should receive an exemption, please comment and provide a justification. A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed, add Clarification Request to a comment.

✅ A exemption request has been requested. Please wait for a maintainer's review.

@pahud
Copy link
Contributor Author

pahud commented Oct 22, 2025

Exemption Request - we have unit tests to cover the edge case handling.

@aws-cdk-automation aws-cdk-automation added the pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback. label Oct 22, 2025
Copy link
Contributor

@abidhasan-aws abidhasan-aws left a comment

Choose a reason for hiding this comment

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

We should additionally add the case for empty string in the integ test as well.

integ.bucket-deployment-data.ts has different use cases of Source.data. we can add another one with empty string.

Thanks for the quick pr :)

},
});
});
test('empty string data', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

we can rename this to be more specific: 'renderData can render empty string'

'<<marker:0xbaba:0>>': token,
},
});
});
Copy link
Contributor

Choose a reason for hiding this comment

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

lint: should add an blank line here.

});
});

test('Source.data with empty string - issue #35809 regression test', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

lets rename this: 'Source.data does not throw error for empty string'

@kumvprat
Copy link
Contributor

Closing this in favour of #35824

@kumvprat kumvprat closed this Oct 23, 2025
@github-actions
Copy link
Contributor

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 23, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

bug This issue is a bug. contribution/core This is a PR that came from AWS. p0 pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

aws-s3-deployment: Source.data fails for empty content

4 participants