← Back to PRs

#21553: fix: resolve workspace template dir in bundled dist/ layout

by echoVic open 2026-02-20 02:59 View on GitHub →
agents size: S trusted-contributor
## Problem Cron isolated sessions fail with: ``` Error: Missing workspace template: TOOLS.md (/usr/lib/node_modules/docs/reference/templates/TOOLS.md). Ensure docs/reference/templates are packaged. ``` The template file exists at `/usr/lib/node_modules/openclaw/docs/reference/templates/TOOLS.md`, but the fallback path resolves incorrectly. ## Root Cause `FALLBACK_TEMPLATE_DIR` in `workspace-templates.ts` uses a hardcoded relative path: ```typescript path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../docs/reference/templates') ``` This assumes the source layout where the file lives at `src/agents/workspace-templates.ts` (2 levels deep). In the bundled `dist/` layout (1 level deep), this resolves to: - Expected: `/usr/lib/node_modules/openclaw/docs/reference/templates` - Actual: `/usr/lib/node_modules/docs/reference/templates` ❌ When `resolveOpenClawPackageRoot()` also returns `null` (because cwd is the workspace dir in cron isolated sessions, not the package dir), no candidate path is valid. ## Fix Replace the hardcoded `../../` relative path with `findTemplateDirFromAncestors()` — a small helper that walks up from `import.meta.url` to find the nearest parent directory containing `docs/reference/templates/`. This works for both: - Source layout: `src/agents/` (2 levels up) - Bundled layout: `dist/` (1 level up) - Any future restructuring ## Changes - **`src/agents/workspace-templates.ts`** — Replace hardcoded relative fallback with ancestor directory walk - **`src/agents/workspace-templates.ancestor-walk.test.ts`** — 3 test cases: - Module nested 1 level deep (`dist/`) ✅ - Module nested 2 levels deep (`src/agents/`) ✅ - cwd is workspace dir, not package root (cron isolated session scenario) ✅ ## Testing ``` ✓ finds templates when module is nested one level deep (dist/) ✓ finds templates when module is nested two levels deep (src/agents/) ✓ finds templates when cwd is the workspace (not package root) Test Files 1 passed (1) Tests 3 passed (3) ``` <!-- greptile_comment --> <h3>Greptile Summary</h3> Replaces hardcoded relative path fallback with ancestor directory walk to correctly resolve workspace template directory in both source (`src/agents/`) and bundled (`dist/`) layouts. - Introduces `findTemplateDirFromAncestors()` helper that walks up from `import.meta.url` to find `docs/reference/templates/` - Fixes cron isolated session failures where the old hardcoded `../../` path resolved to incorrect location in bundled layout - Adds comprehensive test coverage for 1-level deep (dist), 2-level deep (src/agents), and cwd-mismatch scenarios <h3>Confidence Score: 5/5</h3> - This PR is safe to merge with minimal risk - The fix is well-designed with proper fallback handling, comprehensive test coverage for all scenarios, and directly addresses the root cause. The ancestor walk approach is more robust than hardcoded relative paths and handles both current and future directory structures - No files require special attention <sub>Last reviewed commit: f8cab06</sub> <!-- greptile_other_comments_section --> <!-- /greptile_comment -->

Most Similar PRs