Skip to content

refactor: extract server version helper#545

Open
zereight wants to merge 2 commits into
mainfrom
refactor/server-version
Open

refactor: extract server version helper#545
zereight wants to merge 2 commits into
mainfrom
refactor/server-version

Conversation

@zereight

@zereight zereight commented Jun 21, 2026

Copy link
Copy Markdown
Owner

Summary

Part of #516.

This is a small behavior-preserving extraction:

  • moves package version loading into server/version.ts
  • keeps health endpoint behavior unchanged
  • supports both source/test imports and built output path resolution
  • adds a direct unit test that SERVER_VERSION matches package.json

Verification

  • rtk npm run build
  • node --import tsx/esm --test test/server/version.test.ts
  • node -e "import('./build/server/version.js').then(m => { if (!m.SERVER_VERSION || m.SERVER_VERSION === 'unknown') process.exit(1); console.log(m.SERVER_VERSION); })"
  • rtk npm run test:remote-auth

Note

Low Risk
Small refactor with no auth or API logic changes; version is still read from package.json for health and MCP metadata.

Overview
Moves package version loading out of index.ts into a dedicated server/version.ts module that exports SERVER_VERSION, so the main entry no longer reads package.json inline.

The helper resolves package.json from either the compiled layout (build/server/version.js → repo root) or when loaded from source/tests (server/version.ts → parent), falling back to "unknown" if read fails. index.ts now imports SERVER_VERSION for the MCP server version field and the /health JSON version field—behavior is intended to stay the same.

Adds test/server/version.test.ts (asserts SERVER_VERSION matches root package.json) and includes it in the test:mock script.

Reviewed by Cursor Bugbot for commit 1a1f1d0. Bugbot is set up for automated code reviews on this repo. Configure here.

@coderabbitai

coderabbitai Bot commented Jun 21, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 461fc21c-a0ec-4eb0-99c7-f59072d0b799

📥 Commits

Reviewing files that changed from the base of the PR and between 86a3679 and 1a1f1d0.

📒 Files selected for processing (1)
  • package.json
📜 Recent review details
⏰ Context from checks skipped due to timeout. (2)
  • GitHub Check: test
  • GitHub Check: coverage
🔇 Additional comments (1)
package.json (1)

54-54: LGTM!


📝 Walkthrough

Summary by CodeRabbit

  • Tests
    • Added automated test to verify that server version matches the package manifest version
  • Chores
    • Refactored server version initialization to use a centralized module import
    • Expanded the mock test script to include additional test file executions

Walkthrough

SERVER_VERSION resolution is extracted from an inline runtime block in index.ts into a new server/version.ts module that scans two candidate package.json paths at import time and defaults to "unknown". index.ts is updated to import the constant, a new test verifies the exported value matches the root package.json version, and the test suite script is updated to include the new test.

Changes

SERVER_VERSION module extraction

Layer / File(s) Summary
New version resolver module
server/version.ts
Exports SERVER_VERSION by checking two package.json candidate paths relative to import.meta.url, synchronously reading and JSON-parsing the first found file, and falling back to "unknown" on any error or absent version field.
index.ts wiring and test integration
index.ts, test/server/version.test.ts, package.json
index.ts imports SERVER_VERSION from ./server/version.js, removes the former inline read block, and trims the now-unused dirname/fileURLToPath imports. A new test parses the root package.json and asserts SERVER_VERSION matches its version field via assert.strictEqual. The test:mock npm script is updated to include the new test file in its execution sequence.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: extracting server version loading logic into a dedicated helper module.
Description check ✅ Passed The description is well-detailed and directly related to the changeset, explaining the extraction, implementation details, and verification steps.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/server-version
✨ Simplify code
  • Create PR with simplified code
  • Commit simplified code in branch refactor/server-version

Comment @coderabbitai help to get the list of available commands and usage tips.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: New test missing test:mock
    • Added test/server/version.test.ts to the test:mock script in package.json so it runs in CI and npm run test:all.

You can send follow-ups to the cloud agent here.

Reviewed by Cursor Bugbot for commit 86a3679. Configure here.

Comment thread test/server/version.test.ts

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 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 `@server/version.ts`:
- Around line 8-13: The packageJsonPaths array in the version.ts file tries to
resolve the built file path before the source file path. When running from
source, this can incorrectly resolve to a parent workspace's package.json.
Reorder the paths in the packageJsonPaths array so that the source-local path
(the one using ../package.json relative to __dirname) is attempted first,
followed by the built file path (../../package.json), ensuring that source runs
pick up the correct package.json before falling back to the built location.

In `@test/server/version.test.ts`:
- Line 6: The packageVersion constant is reading package.json using a relative
path which depends on the process current working directory, making the test
flaky when run from different directories. Instead, construct an absolute path
to package.json using __dirname (the directory of the current test file) and the
path module to navigate to the project root where package.json is located.
Replace the relative "package.json" string in the fs.readFileSync call with this
absolute path so the test can find the file regardless of which directory the
test is invoked from.
🪄 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: ASSERTIVE

Plan: Pro Plus

Run ID: 8785fb46-9cee-45c5-a545-9d84ff7f9707

📥 Commits

Reviewing files that changed from the base of the PR and between c662a3d and 86a3679.

📒 Files selected for processing (3)
  • index.ts
  • server/version.ts
  • test/server/version.test.ts
📜 Review details
⏰ Context from checks skipped due to timeout. (4)
  • GitHub Check: Cursor Bugbot Autofix
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: coverage
  • GitHub Check: test
🔇 Additional comments (1)
index.ts (1)

191-193: LGTM!

Also applies to: 202-202

Comment thread server/version.ts
Comment on lines +8 to +13
const packageJsonPaths = [
// Built file: build/server/version.js -> ../../package.json
path.resolve(__dirname, "../../package.json"),
// Source file under tsx/node:test: server/version.ts -> ../package.json
path.resolve(__dirname, "../package.json"),
];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Potentially resolves the wrong package.json in source runs.

At Line 10, ../../package.json is tried before the source-local candidate. When running from server/version.ts, this can pick a parent workspace package.json and report an incorrect SERVER_VERSION.

Suggested fix
 const packageJsonPaths = [
-  // Built file: build/server/version.js -> ../../package.json
-  path.resolve(__dirname, "../../package.json"),
   // Source file under tsx/node:test: server/version.ts -> ../package.json
   path.resolve(__dirname, "../package.json"),
+  // Built file: build/server/version.js -> ../../package.json
+  path.resolve(__dirname, "../../package.json"),
 ];
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const packageJsonPaths = [
// Built file: build/server/version.js -> ../../package.json
path.resolve(__dirname, "../../package.json"),
// Source file under tsx/node:test: server/version.ts -> ../package.json
path.resolve(__dirname, "../package.json"),
];
const packageJsonPaths = [
// Source file under tsx/node:test: server/version.ts -> ../package.json
path.resolve(__dirname, "../package.json"),
// Built file: build/server/version.js -> ../../package.json
path.resolve(__dirname, "../../package.json"),
];
🤖 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 `@server/version.ts` around lines 8 - 13, The packageJsonPaths array in the
version.ts file tries to resolve the built file path before the source file
path. When running from source, this can incorrectly resolve to a parent
workspace's package.json. Reorder the paths in the packageJsonPaths array so
that the source-local path (the one using ../package.json relative to __dirname)
is attempted first, followed by the built file path (../../package.json),
ensuring that source runs pick up the correct package.json before falling back
to the built location.

import fs from "node:fs";
import { SERVER_VERSION } from "../../server/version.js";

const packageVersion = JSON.parse(fs.readFileSync("package.json", "utf8")).version;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Make the fixture path independent of process CWD.

At Line 6, reading "package.json" relative to process.cwd() makes this test flaky when invoked from another directory.

Suggested fix
-const packageVersion = JSON.parse(fs.readFileSync("package.json", "utf8")).version;
+const packageJsonUrl = new URL("../../package.json", import.meta.url);
+const packageVersion = JSON.parse(fs.readFileSync(packageJsonUrl, "utf8")).version;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const packageVersion = JSON.parse(fs.readFileSync("package.json", "utf8")).version;
const packageJsonUrl = new URL("../../package.json", import.meta.url);
const packageVersion = JSON.parse(fs.readFileSync(packageJsonUrl, "utf8")).version;
🤖 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 `@test/server/version.test.ts` at line 6, The packageVersion constant is
reading package.json using a relative path which depends on the process current
working directory, making the test flaky when run from different directories.
Instead, construct an absolute path to package.json using __dirname (the
directory of the current test file) and the path module to navigate to the
project root where package.json is located. Replace the relative "package.json"
string in the fs.readFileSync call with this absolute path so the test can find
the file regardless of which directory the test is invoked from.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants