feat(swift): generate Version.swift with sdkVersion constant#14724
feat(swift): generate Version.swift with sdkVersion constant#14724
Conversation
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
There was a problem hiding this comment.
Claude Code Review
This repository is configured for manual code reviews. Comment @claude review to trigger a review and subscribe this PR to future pushes, or @claude review once for a one-time review.
Tip: disable this comment in your organization's Code Review settings.
SDK Generation Benchmark ResultsComparing PR branch against latest nightly baseline on Full benchmark table (click to expand)
main (generator): generator-only time via --skip-scripts (includes Docker image build, container startup, IR parsing, and code generation — this is the same Docker-based flow customers use via |
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
| context.project.addSourceAsIsFile({ | ||
| nameCandidateWithoutExtension: "Version", | ||
| directory: RelativeFilePath.of(""), | ||
| contents: `public let sdkVersion = "${version}"\n` |
There was a problem hiding this comment.
Unsanitized version string injected into generated Swift source code
The version string is interpolated directly into a Swift string literal without escaping " or \:
contents: `public let sdkVersion = "${version}"\n`version is user-supplied (e.g. via --version CLI flag or generators.yml). A value containing " or a newline breaks out of the Swift string literal and injects arbitrary Swift statements into the generated Version.swift. For example, --version '1.0.0"\npublic func backdoor() {}' would produce valid, compilable Swift with an injected declaration that is committed to the SDK repo and distributed to every SDK consumer.
This was flagged in the previous review pass; the prior thread became "outdated" when the let → public let change landed without adding escaping.
Prompt To Fix With AI
In `generateVersionFile` in `generators/swift/sdk/src/SdkGeneratorCli.ts`, escape `\` and `"` before embedding the version in the Swift string literal.
```typescript
private generateVersionFile(context: SdkGeneratorContext): void {
const spmDetails = context.getSPMDetails();
const version = spmDetails.minVersion;
if (version != null) {
const escapedVersion = version.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
context.project.addSourceAsIsFile({
nameCandidateWithoutExtension: "Version",
directory: RelativeFilePath.of(""),
contents: `public let sdkVersion = "${escapedVersion}"\n`
});
}
}
```
This is the only change needed — `\` must be escaped first (to avoid double-escaping), then `"`.Severity: low | Confidence: 85%
SDK Generation Benchmark ResultsComparing PR branch against latest nightly baseline on Full benchmark table (click to expand)
main (generator): generator-only time via --skip-scripts (includes Docker image build, container startup, IR parsing, and code generation — this is the same Docker-based flow customers use via |
Description
Adds
Version.swiftgeneration to the Swift SDK generator, embedding the SDK version as apublicsource-level constant. This brings the Swift generator in line with other SDK generators and fixes auto-versioning for Swift SDKs (the magic placeholder version will now appear in git diffs, allowing the auto-versioning system to detect and bump versions).Split from #14719 (git tag fallback for auto-versioning) so each concern can be reviewed independently.
Changes Made
generateVersionFile()method toSdkGeneratorCli.tsthat emitsVersion.swiftwith a top-levelpublic let sdkVersionconstantminVersionis available (i.e., GitHub output mode with a version configured — not for local/download modes)versions.ymlentry for Swift SDK generator0.31.0Updates since last revision
sdkVersionconstantpublicso SDK consumers can access it (Swift defaults tointernalaccess)swift-sdk): 122/123 passed (1 expected failure inliteral)Version.swiftwithpublic let sdkVersion = "0.0.1"(the seed default version)Testing
Version.swiftfiles added across all swift-sdk fixtures)Review Checklist
addSourceAsIsFilewith name"Version"won't collide with any IR-generated type namespublic let sdkVersion = "..."is acceptable Swift style (vs. namespacing inside astructorenum)Link to Devin session: https://app.devin.ai/sessions/eeaa831f68d14737a731b1c8cb4968b4
Requested by: @Swimburger