Skip to content

fix: correct package.json path in daemon tracing#389

Merged
andreabadesso merged 2 commits intomasterfrom
fix/daemon-tracing-package-json-path
Apr 9, 2026
Merged

fix: correct package.json path in daemon tracing#389
andreabadesso merged 2 commits intomasterfrom
fix/daemon-tracing-package-json-path

Conversation

@andreabadesso
Copy link
Copy Markdown
Collaborator

@andreabadesso andreabadesso commented Apr 9, 2026

Motivation

The daemon crashed on startup in the dev environment with:

Error: Cannot find module '../../package.json'

This was triggered by the OTel tracing module added in #383. At packages/daemon/src/tracing.ts:38, require('../../package.json') resolves to packages/package.json (nonexistent) from both the source and the compiled dist/tracing.js. The correct relative path is ../package.json, which resolves to packages/daemon/package.json.

The fallback path always runs because SERVICE_VERSION is not set in any environment. Dev fired first because it was deployed with OTEL_EXPORTER_OTLP_ENDPOINT set (OTel enabled); prod would hit the same crash as soon as #383 is rolled out.

The sibling packages/wallet-service/src/tracing.ts does not set service.version at all, so it is not affected.

Acceptance Criteria

  • packages/daemon/src/tracing.ts uses require('../package.json') so service.version resolves correctly at runtime
  • Daemon starts successfully in the dev environment with OTel enabled (no Cannot find module error)
  • service.version reported in OTel spans matches the version in packages/daemon/package.json

Checklist

  • If you are requesting a merge into master, confirm this code is production-ready and can be included in future releases as soon as it gets merged
  • Make sure either the unit tests and/or the QA tests are capable of testing the new features
  • Make sure you do not include new dependencies in the project unless strictly necessary and do not include dev-dependencies as production ones. More dependencies increase the possibility of one of them being hijacked and affecting us.

Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Telemetry now reports the correct service version and will fall back to "unknown" if a version cannot be determined. Existing telemetry enable/disable behavior and startup/shutdown remain unchanged.

The OTel tracing module used `require('../../package.json')`, which
resolves to `packages/package.json` (nonexistent) from both the source
and compiled file. The correct relative path is `../package.json`.

This caused the daemon to crash on startup in environments where OTel
was enabled and SERVICE_VERSION was not set.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 9, 2026

📝 Walkthrough

Walkthrough

Modified OpenTelemetry Resource initialization in the daemon to read service.version from the monorepo root package.json (require('../../../package.json').version) and added a fallback value of 'unknown' when the resolved version is falsy. Other OTEL startup/shutdown behavior unchanged.

Changes

Cohort / File(s) Summary
OpenTelemetry Resource Configuration
packages/daemon/src/tracing.ts
Changed service.version resolution to require('../../../package.json').version (monorepo root) and added fallback 'unknown'; no other OTEL logic altered.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested labels

enhancement

Suggested reviewers

  • pedroferreira1
  • luislhl

Poem

🐰 I hopped through folders, near and far,
Found the root where the version are,
A gentle fallback keeps things sound,
Now traces know where versions bound. 🥕

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and clearly identifies the main change: correcting an incorrect package.json path in the daemon's tracing module that was causing startup failures.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/daemon-tracing-package-json-path

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/daemon/src/tracing.ts`:
- Line 38: The telemetry field 'service.version' currently falls back to
require('../package.json').version which is undefined because this package.json
lacks a "version"; update the value used for 'service.version' (the expression
using process.env.SERVICE_VERSION and require('../package.json').version) to
provide a safe default string (e.g., '0.0.0' or 'unknown') when neither env nor
package.json version exist, and also add a proper "version" field in the
package.json for the daemon package so telemetry reflects the real package
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: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2d2bcd69-1668-4b40-aa72-d087cfc9a8c2

📥 Commits

Reviewing files that changed from the base of the PR and between cc5eea2 and 60aa358.

📒 Files selected for processing (1)
  • packages/daemon/src/tracing.ts

Comment thread packages/daemon/src/tracing.ts Outdated
The daemon package.json has no "version" field — the monorepo root
package.json is the single source of truth for the version (kept in
sync with git tags). Pointing at the daemon's own package.json would
still leave service.version undefined in OTel spans. Also add an
'unknown' fallback to guard against the version being missing at
runtime.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/daemon/src/tracing.ts`:
- Around line 38-40: The ESLint suppression currently sits before the comment
and does not cover the require call; move the eslint-disable-next-line
`@typescript-eslint/no-var-requires` directive so it directly precedes the
require('../../../package.json').version expression (the line that assigns
'service.version') so the rule is properly suppressed for that require usage;
update the location of the comment so it sits immediately above the line
containing require('../../../package.json').version (the 'service.version'
assignment).
🪄 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: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9c84a689-0ccb-470c-848b-ef1f738631f4

📥 Commits

Reviewing files that changed from the base of the PR and between 60aa358 and 8102f35.

📒 Files selected for processing (1)
  • packages/daemon/src/tracing.ts

Comment on lines +38 to +40
// The daemon package.json has no "version" field — the monorepo root
// package.json is the single source of truth (kept in sync with git tags).
'service.version': process.env.SERVICE_VERSION || require('../../../package.json').version || 'unknown',
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

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify the eslint disable directive adjacency around the require() call.
rg -n -C1 "eslint-disable-next-line `@typescript-eslint/no-var-requires`|require\('\.\./\.\./\.\./package\.json'\)" packages/daemon/src/tracing.ts

Repository: HathorNetwork/hathor-wallet-service

Length of output: 779


Move the ESLint suppression comment immediately before the require() call.

The eslint-disable-next-line directive on line 37 only suppresses the next line (line 38, a comment), not line 40 where the require() occurs. This allows @typescript-eslint/no-var-requires to trigger on the actual require statement.

Suggested fix
-      // eslint-disable-next-line `@typescript-eslint/no-var-requires`
-      // The daemon package.json has no "version" field — the monorepo root
-      // package.json is the single source of truth (kept in sync with git tags).
-      'service.version': process.env.SERVICE_VERSION || require('../../../package.json').version || 'unknown',
+      // The daemon package.json has no "version" field — the monorepo root
+      // package.json is the single source of truth (kept in sync with git tags).
+      // eslint-disable-next-line `@typescript-eslint/no-var-requires`
+      'service.version': process.env.SERVICE_VERSION || require('../../../package.json').version || 'unknown',
🧰 Tools
🪛 ESLint

[error] 40-40: Require statement not part of import statement.

(@typescript-eslint/no-var-requires)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/daemon/src/tracing.ts` around lines 38 - 40, The ESLint suppression
currently sits before the comment and does not cover the require call; move the
eslint-disable-next-line `@typescript-eslint/no-var-requires` directive so it
directly precedes the require('../../../package.json').version expression (the
line that assigns 'service.version') so the rule is properly suppressed for that
require usage; update the location of the comment so it sits immediately above
the line containing require('../../../package.json').version (the
'service.version' assignment).

@andreabadesso
Copy link
Copy Markdown
Collaborator Author

Good catch @coderabbitai — you're right that packages/daemon/package.json has no "version" field, so the original fix would have left service.version as undefined in every span.

Pushed 8102f35 to address this. Rather than adding a "version" field to the daemon's package.json, I pointed the require at the monorepo root package.json (../../../package.json), which is the single source of truth for the version in this repo (currently 1.12.0, kept in sync with git tags via the release branch flow). Adding a version to the daemon package would duplicate that and drift from the release process, which only bumps the root.

Also added || 'unknown' as a safe fallback per your suggestion.

Verified empirically against the actual Docker build:

  1. docker build --target prod -f packages/daemon/Dockerfile . succeeds.
  2. /app/package.json (monorepo root) is present in the final image — not excluded by packages/daemon/.dockerignore.
  3. From inside the image, a probe script at /app/packages/daemon/dist/ resolving require('../../../package.json') returns name: hathor-wallet-service, version: 1.12.0.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 9, 2026

Tip

For best results, initiate chat on the files or code changes.

@andreabadesso Thanks for the thorough explanation and the empirical Docker verification — that's exactly the right level of confidence for a runtime path resolution change.

The approach is sound:

  • ../../../package.json from packages/daemon/dist/tracing.js correctly traverses dist → daemon → packages → repo root, matching your probe result.
  • Anchoring to the monorepo root avoids the version drift problem that would come from duplicating the field in the daemon's own package.json.
  • The || 'unknown' guard keeps OTel initialization safe even if the file is somehow absent in an unexpected environment.

✏️ Learnings added
Learnt from: andreabadesso
URL: https://github.com/HathorNetwork/hathor-wallet-service/pull/389

Timestamp: 2026-04-09T16:36:50.662Z
Learning: In the hathor-wallet-service monorepo, the single source of truth for the service version is the root `package.json` (currently `1.12.0`), kept in sync with git tags via the release branch flow. The daemon package (`packages/daemon/package.json`) intentionally has no `"version"` field. In `packages/daemon/src/tracing.ts`, `service.version` is resolved via `require('../../../package.json').version` (resolves from `dist/tracing.js` → `dist/` → `packages/daemon/` → `packages/` → repo root). The root `package.json` is included in the production Docker image (not excluded by `packages/daemon/.dockerignore`).

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.


If you found this review helpful, would you consider giving us a shout-out on X?

Thank you for using CodeRabbit!

@andreabadesso andreabadesso merged commit 5df7145 into master Apr 9, 2026
3 checks passed
@andreabadesso andreabadesso deleted the fix/daemon-tracing-package-json-path branch April 9, 2026 16:54
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.

3 participants