From 2b490c5a46e4821d2ce843bb68ec976025a91f4d Mon Sep 17 00:00:00 2001 From: Jono Prest Date: Mon, 8 Jun 2026 13:53:51 +0000 Subject: [PATCH] Report the real package version in the user agent 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/). 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. --- build.rs | 24 ++++++++++++++++++++++++ src/lib.rs | 4 +++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/build.rs b/build.rs index 9fc2367..f7966ef 100644 --- a/build.rs +++ b/build.rs @@ -2,4 +2,28 @@ extern crate napi_build; fn main() { napi_build::setup(); + + // Expose the npm package version (from package.json) so the client can report + // it in its user agent. The Cargo package version is kept at 0.0.0 by napi, so + // CARGO_PKG_VERSION is not usable here. The release artifacts are built via + // `yarn build` from a checkout whose package.json holds the release version, + // so reading it here picks up the real version regardless of the build env. + // Falls back to CARGO_PKG_VERSION if package.json can't be read. + 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()); + + println!("cargo:rustc-env=NPM_PKG_VERSION={version}"); + println!("cargo:rerun-if-changed=package.json"); } diff --git a/src/lib.rs b/src/lib.rs index 277ea07..73bd014 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -89,7 +89,9 @@ impl HypersyncClient { /// Create a new client with given config #[napi(constructor)] pub fn new(cfg: ClientConfig) -> napi::Result { - Self::new_with_agent(cfg, format!("hscn/{}", env!("CARGO_PKG_VERSION"))) + // NPM_PKG_VERSION is the package.json version, injected by build.rs. + // (CARGO_PKG_VERSION is 0.0.0 because napi keeps the Cargo version static.) + Self::new_with_agent(cfg, format!("hscn/{}", env!("NPM_PKG_VERSION"))) } /// Create a new client with custom user agent