(core): cdk migrate telemetry metadata is lost due to unassigned .concat()
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 83
Description
## Describe the bug
In `packages/aws-cdk-lib/core/lib/private/metadata-resource.ts`, the code attempts to append a migration-specific suffix to the telemetry `analyticsString` when the project is created via `cdk migrate`.
However, it uses the `.concat()` method on a string without assigning the result back to the variable. Since strings in JavaScript are immutable, the `.concat()` call returns a new string that is immediately discarded, leaving the original `analyticsString` unchanged. Consequently, telemetry for `cdk migrate` is never reported.
## Expected Behavior
When the `@aws-cdk/core:cdk-migrate` context flag is set, the generated metadata should include the `:cdk-migrate` suffix (compressed and base64 encoded) so that AWS can track the usage of the migration tool.
## Current Behavior
The code executes `analyticsString.concat(...)` but does not update the `analyticsString` variable. The returned metadata remains identical to a standard (non-migrated) project, causing a silent loss of telemetry.
## Reproduction Steps
The bug is visible in this logic block:
```typescript
// packages/aws-cdk-lib/core/lib/private/metadata-resource.ts
if (process.env.CDK_CONTEXT_JSON && JSON.parse(process.env.CDK_CONTEXT_JSON)['cdk-migrate']) {
const compressedAppInfoBuffer = zlib.gzipSync(Buffer.from('cdk-migrate'));
const compressedAppInfo = compressedAppInfoBuffer.toString('base64');
// BUG: Result is not assigned!
// Should be: analyticsString = analyticsString.concat(...)
analyticsString.concat(':', compressedAppInfo);
}
```
We can reproduce this behavior with a simple script:
```javascript
let analyticsString = "v2:deflate64:base64data";
let suffix = ":compressed-migrate-tag";
// This mimics the bug in the CDK source
analyticsString.concat(suffix);
console.log(analyticsString.includes(suffix)); // Output: false
```
## Possible Solution
Change line 129 in `metadata-resource.ts` to assign the result or use string interpolation:
```typescript
analyticsString += `:${compressedAppInfo}`;
```
## Environment Information
* **AWS CDK Library version:** 2.241.0
* **Node.js Version:** v20.20.0
* **OS:** Windows
* **Language:** TypeScript
## Other information
The failure to capture analytics for `cdk migrate` represents a significant loss of visibility into the adoption and performance of the migration tooling.
I have a reproduction script ready and can provide a PR to fix this if requested.
@pahud Would love your insights on this investigation.
Contributor guide
Research direction
Start in packages/aws-cdk-lib/core/lib/private/metadata-resource.ts at the cdk-migrate metadata logic and inspect how analyticsString is built. Run the reproduction with the cdk-migrate context flag and verify that the generated metadata includes the compressed, base64-encoded :cdk-migrate suffix.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 1/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100