fix(mosaic): resolve framework scripts via import.meta.url
All checks were successful
ci/woodpecker/push/ci Pipeline was successful
ci/woodpecker/pr/ci Pipeline was successful

Previously, resolveTool() used createRequire + req.resolve('@mosaic/mosaic/package.json')
to locate the bundled framework directory. The published package.json only exports the
root '.' subpath, so Node throws ERR_PACKAGE_PATH_NOT_EXPORTED for './package.json'. The
catch block silently swallowed this error and fell through to the MOSAIC_HOME fallback
path (~/.config/mosaic/tools/_scripts/mosaic-doctor), which does not exist on a user's
machine, causing every framework-delegated subcommand (doctor, coord, etc.) to fail with
'[mosaic] Script not found'.

Two-layer fix:
1. Replace the createRequire approach with import.meta.url-based resolution. The built
   file lives at dist/commands/launch.js, so ../../framework/tools/... always resolves to
   the bundled framework directory regardless of how the package exports field is
   configured. This is layout-stable for both the published package and local dev.
2. Add './package.json' and './framework/*' subpath exports to package.json as belt-and-
   suspenders, keeping subpath access working for any future caller.

Bump version to 0.0.18.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Jarvis
2026-04-04 20:36:18 -05:00
parent 07a1f5d594
commit ef88f37b9d
2 changed files with 9 additions and 11 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@mosaic/mosaic",
"version": "0.0.17",
"version": "0.0.18",
"repository": {
"type": "git",
"url": "https://git.mosaicstack.dev/mosaic/mosaic-stack.git",
@@ -18,7 +18,9 @@
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
},
"./package.json": "./package.json",
"./framework/*": "./framework/*"
},
"scripts": {
"build": "tsc",

View File

@@ -7,9 +7,9 @@
import { execFileSync, execSync, spawnSync } from 'node:child_process';
import { existsSync, mkdirSync, readFileSync, writeFileSync, readdirSync, rmSync } from 'node:fs';
import { createRequire } from 'node:module';
import { homedir } from 'node:os';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import type { Command } from 'commander';
const MOSAIC_HOME = process.env['MOSAIC_HOME'] ?? join(homedir(), '.config', 'mosaic');
@@ -498,14 +498,10 @@ function delegateToScript(scriptPath: string, args: string[], env?: Record<strin
* CLI version) over the deployed copy in ~/.config/mosaic/ (may be stale).
*/
function resolveTool(...segments: string[]): string {
try {
const req = createRequire(import.meta.url);
const mosaicPkg = dirname(req.resolve('@mosaic/mosaic/package.json'));
const bundled = join(mosaicPkg, 'framework', 'tools', ...segments);
if (existsSync(bundled)) return bundled;
} catch {
// Fall through to deployed copy
}
// Resolve relative to the built file: dist/commands/launch.js → ../../framework/tools/...
const thisFile = fileURLToPath(import.meta.url);
const bundled = join(dirname(thisFile), '..', '..', 'framework', 'tools', ...segments);
if (existsSync(bundled)) return bundled;
return join(MOSAIC_HOME, 'tools', ...segments);
}