fix(ci): sync Cargo.toml version before native binary build#538
fix(ci): sync Cargo.toml version before native binary build#538carlos-alm merged 4 commits intomainfrom
Conversation
The build-native job ran in parallel with compute-version, so the Rust
crate was compiled with whatever stale version was in Cargo.toml rather
than the release version. This caused `codegraph info` to report
"binary built as 3.2.0" even on a 3.3.0 release.
Make build-native depend on compute-version and update Cargo.toml
before `cargo build` so env!("CARGO_PKG_VERSION") returns the correct
version.
|
Claude finished @carlos-alm's task —— View job Reviewing PR: sync Cargo.toml version before native binary buildTodo List
Root Cause AnalysisProblem Being Solved: The Why This Approach: The fix correctly addresses the root cause by:
Risk Assessment: ✅ LOW RISK - This is a proper fix that addresses the actual problem rather than masking symptoms. Configuration Change Detection
Test Integrity
Code Quality
Minor Improvement Suggestion: The awk pattern could be more specific to avoid accidentally matching commented lines, but given Cargo.toml's structure, this is unlikely to be an issue in practice. Backlog Compliance
Critical ConcernsNone identified. This is a straightforward CI fix that properly addresses a version synchronization issue. Final Recommendation
Recommendation: This PR should be merged. It fixes a real version synchronization issue in the CI pipeline with a proper solution. |
Greptile SummaryThis PR fixes a CI race condition where The fix has two parts:
Key implementation details:
Confidence Score: 5/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant P as preflight
participant CV as compute-version
participant BN as build-native (x6 matrix)
participant PD as publish-dev / publish
P->>CV: needs preflight
CV->>BN: needs preflight + compute-version (NEW)
Note over BN: 1. Patch Cargo.toml with VERSION
Note over BN: 2. napi build --release --target
Note over BN: 3. Upload .node artifact
BN->>PD: needs compute-version + build-native
Note over PD: Download artifacts, npm publish / gh release
Last reviewed commit: "fix: scope awk versi..." |
| run: | | ||
| CARGO="crates/codegraph-core/Cargo.toml" | ||
| awk -v v="$VERSION" '/^version =/{$0="version = \""v"\""}1' "$CARGO" > "${CARGO}.tmp" | ||
| mv "${CARGO}.tmp" "$CARGO" |
There was a problem hiding this comment.
awk replaces all ^version = lines, not just the [package] version
The pattern /^version =/ is a "replace all" in awk (every matching line is rewritten). Currently crates/codegraph-core/Cargo.toml only has one such line so this works, but it would silently corrupt the file the moment a second top-level version = … appears (e.g. a workspace [package.metadata] block, a feature-flag version, etc.).
Compare this with scripts/sync-native-versions.js, which uses a first-match-only replace (no g flag on the regex). For consistency and safety, scope the replacement to the first occurrence only:
| run: | | |
| CARGO="crates/codegraph-core/Cargo.toml" | |
| awk -v v="$VERSION" '/^version =/{$0="version = \""v"\""}1' "$CARGO" > "${CARGO}.tmp" | |
| mv "${CARGO}.tmp" "$CARGO" | |
| CARGO="crates/codegraph-core/Cargo.toml" | |
| sed -i.tmp '0,/^version =/{s/^version = "[^"]*"/version = "'"$VERSION"'"/}' "$CARGO" | |
| rm -f "${CARGO}.tmp" |
Alternatively, using awk with an early-exit flag after the first replacement:
awk -v v="$VERSION" '!done && /^version =/{$0="version = \""v"\""; done=1}1' "$CARGO" > "${CARGO}.tmp"
mv "${CARGO}.tmp" "$CARGO"There was a problem hiding this comment.
Fixed in ff3e76a. Added an early-exit flag to the awk command so only the first ^version = line is replaced:
awk -v v="$VERSION" '!done && /^version =/{$0="version = \""v"\""; done=1}1' "$CARGO" > "${CARGO}.tmp"This matches the first-occurrence-only behavior of scripts/sync-native-versions.js and prevents corruption if a second version = line appears in a different TOML section.
| - name: Sync Cargo.toml version | ||
| env: | ||
| VERSION: ${{ needs.compute-version.outputs.version }} | ||
| shell: bash | ||
| run: | | ||
| CARGO="crates/codegraph-core/Cargo.toml" | ||
| awk -v v="$VERSION" '/^version =/{$0="version = \""v"\""}1' "$CARGO" > "${CARGO}.tmp" | ||
| mv "${CARGO}.tmp" "$CARGO" |
There was a problem hiding this comment.
No guard against an empty
$VERSION value
If compute-version does not produce a version output (e.g. a job failure that somehow still lets downstream jobs proceed, or a future refactor that renames the output key), the awk command will silently write version = "" into Cargo.toml. Cargo will then error with a confusing TOML validation message rather than a clear "version is empty" diagnostic.
A one-line guard at the top of the run block keeps the failure obvious:
| - name: Sync Cargo.toml version | |
| env: | |
| VERSION: ${{ needs.compute-version.outputs.version }} | |
| shell: bash | |
| run: | | |
| CARGO="crates/codegraph-core/Cargo.toml" | |
| awk -v v="$VERSION" '/^version =/{$0="version = \""v"\""}1' "$CARGO" > "${CARGO}.tmp" | |
| mv "${CARGO}.tmp" "$CARGO" | |
| - name: Sync Cargo.toml version | |
| env: | |
| VERSION: ${{ needs.compute-version.outputs.version }} | |
| shell: bash | |
| run: | | |
| [[ -n "$VERSION" ]] || { echo "::error::VERSION is empty — compute-version output missing"; exit 1; } | |
| CARGO="crates/codegraph-core/Cargo.toml" | |
| awk -v v="$VERSION" '/^version =/{$0="version = \""v"\""}1' "$CARGO" > "${CARGO}.tmp" | |
| mv "${CARGO}.tmp" "$CARGO" |
There was a problem hiding this comment.
Fixed in c43a2e4. Added a guard at the top of the run block:
[[ -n "$VERSION" ]] || { echo "::error::VERSION is empty — compute-version output missing"; exit 1; }The workflow will now fail immediately with a clear GitHub Actions error annotation if $VERSION is empty, rather than silently writing version = "" into Cargo.toml.
Add validation that $VERSION is non-empty before writing to Cargo.toml. Prevents silently writing an empty version string if compute-version output is missing.
Use an early-exit flag so only the first ^version = line in Cargo.toml is replaced. Prevents silently corrupting the file if a second version = line appears in a different TOML section.
|
Addressed both Greptile review findings:
|
Summary
build-nativeCI job compiled Rust binaries in parallel withcompute-version, soenv!("CARGO_PKG_VERSION")baked in the staleCargo.tomlversion instead of the release versioncodegraph inforeported "binary built as 3.2.0" on a 3.3.0 release (bug: incremental builds produce fewer edges than full builds (3.3% gap) #533 suggestion 10.1)build-nativedepend oncompute-versionand updateCargo.tomlbeforecargo buildTest plan
publish.ymlYAML is valid (CI will check)codegraph infoshows matching binary and package versions