Report the real package version in the user agent (hscn/<version>)#65
Report the real package version in the user agent (hscn/<version>)#65JonoPrest wants to merge 1 commit into
Conversation
The user agent was hscn/{CARGO_PKG_VERSION}, but napi keeps the Cargo package
version at 0.0.0, so it always sent hscn/0.0.0. build.rs now reads the version
from package.json and injects it as NPM_PKG_VERSION, and the client uses that
(hscn/<package-version>).
build.rs reads package.json directly rather than relying on a build-env var, so
the published artifacts get the correct version: the publish workflow builds each
target with 'yarn build' from a checkout whose package.json holds the release
version. Falls back to CARGO_PKG_VERSION if package.json can't be read.
📝 WalkthroughWalkthroughThis PR adds build-time npm package version injection. A build script reads ChangesNPM Version Build Injection
🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@build.rs`:
- Around line 12-25: The current manual line-based parsing in build.rs that
computes the local variable version is brittle; instead add serde_json = "1.0"
to [build-dependencies] in Cargo.toml and replace the manual
read/line/strip_prefix logic with a robust JSON parse using serde_json::from_str
to extract the "version" field (falling back to
env!("CARGO_PKG_VERSION").to_string() if parsing or key extraction fails).
Locate the code that defines version in build.rs (and the use of
env!("CARGO_PKG_VERSION")) and update it to read package.json, parse it with
serde_json::Value (or a small struct with a version field), and use that value
as the version.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 1592ab44-7012-4a2c-8e38-443fa2e13429
📒 Files selected for processing (2)
build.rssrc/lib.rs
| let version = std::fs::read_to_string("package.json") | ||
| .ok() | ||
| .and_then(|pkg| { | ||
| pkg.lines().find_map(|line| { | ||
| line.trim() | ||
| .strip_prefix("\"version\":") | ||
| .map(|rest| { | ||
| rest.trim() | ||
| .trim_matches(|c| c == ',' || c == '"' || c == ' ') | ||
| .to_string() | ||
| }) | ||
| }) | ||
| }) | ||
| .unwrap_or_else(|| env!("CARGO_PKG_VERSION").to_string()); |
There was a problem hiding this comment.
Fragile JSON parsing could break on valid formatting variations.
The manual line-by-line string matching requires "version": with no space before the colon. This will fail on valid JSON formatted as "version" : "1.4.0" or if formatting tools reflow the file. When parsing fails, the fallback to CARGO_PKG_VERSION (0.0.0) defeats the purpose of this PR.
🔧 Proposed fix using serde_json for robust parsing
Add serde_json to [build-dependencies] in Cargo.toml:
[build-dependencies]
napi-build = "..."
serde_json = "1.0"Then replace the manual parsing with:
- let version = std::fs::read_to_string("package.json")
- .ok()
- .and_then(|pkg| {
- pkg.lines().find_map(|line| {
- line.trim()
- .strip_prefix("\"version\":")
- .map(|rest| {
- rest.trim()
- .trim_matches(|c| c == ',' || c == '"' || c == ' ')
- .to_string()
- })
- })
- })
- .unwrap_or_else(|| env!("CARGO_PKG_VERSION").to_string());
+ let version = std::fs::read_to_string("package.json")
+ .ok()
+ .and_then(|pkg| {
+ serde_json::from_str::<serde_json::Value>(&pkg)
+ .ok()?
+ .get("version")?
+ .as_str()
+ .map(|s| s.to_string())
+ })
+ .unwrap_or_else(|| env!("CARGO_PKG_VERSION").to_string());This handles all valid JSON formats and is more maintainable.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@build.rs` around lines 12 - 25, The current manual line-based parsing in
build.rs that computes the local variable version is brittle; instead add
serde_json = "1.0" to [build-dependencies] in Cargo.toml and replace the manual
read/line/strip_prefix logic with a robust JSON parse using serde_json::from_str
to extract the "version" field (falling back to
env!("CARGO_PKG_VERSION").to_string() if parsing or key extraction fails).
Locate the code that defines version in build.rs (and the use of
env!("CARGO_PKG_VERSION")) and update it to read package.json, parse it with
serde_json::Value (or a small struct with a version field), and use that value
as the version.
The client already tags requests with
hscn/<version>, but the version came fromCARGO_PKG_VERSION— and napi keeps the Cargo package version pinned at0.0.0, so every request actually went out ashscn/0.0.0.This makes
build.rsread the version frompackage.jsonand inject it asNPM_PKG_VERSION, which the client now uses →hscn/<package-version>(e.g.hscn/1.4.0).Why the release gets the right version
build.rsreadspackage.jsondirectly, not via any build-time env var the pipeline may or may not set. The publish workflow (publish_to_npm.yaml) builds each platform withcheckout→yarn install→yarn build, so the artifact is compiled from a checkout whosepackage.jsonholds the release version (kept in sync bynapi version). It falls back toCARGO_PKG_VERSIONonly ifpackage.jsoncan't be read.Matches the Rust (
hscr/) and Python (hscp/, separate PR) clients. Verified withcargo check(theenv!("NPM_PKG_VERSION")would fail to compile if build.rs didn't set it; the version parses to 1.4.0).Summary by CodeRabbit