Skip to content

Commit d749c8c

Browse files
committed
fix(studio): improve module resolution logic for better compatibility
1 parent c0cfc0f commit d749c8c

2 files changed

Lines changed: 11 additions & 9 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ temp/
4545
packages/*/dist/
4646
packages/*/*.tsbuildinfo
4747

48+
# Generated JSON Schema (rebuilt by `pnpm gen:schema` during build)
49+
packages/spec/json-schema/
50+
4851
# Exclude generated JS from TypeScript sources
4952
packages/*/src/**/*.js
5053
!packages/*/src/**/*.config.js

packages/cli/src/utils/studio.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import path from 'path';
1010
import fs from 'fs';
1111
import net from 'net';
1212
import { createRequire } from 'module';
13+
import { pathToFileURL } from 'url';
1314
import { spawn, type ChildProcess } from 'child_process';
1415
import chalk from 'chalk';
1516

@@ -49,27 +50,25 @@ export function resolveStudioPath(): string | null {
4950
}
5051
}
5152

52-
// Fallback: resolve from node_modules
53-
// Try resolving from the consumer's working directory first (important for
54-
// pnpm strict isolation where the CLI cannot see the consumer's dependencies),
55-
// then fall back to resolving from the CLI's own location.
53+
// Fallback: resolve from node_modules via createRequire.
54+
// Try the consumer's cwd first (pnpm strict isolation means the CLI's own
55+
// import.meta.url cannot see the consumer's dependencies), then the CLI itself.
5656
const resolutionBases = [
57-
path.join(cwd, '__placeholder__.js'), // consumer workspace
58-
import.meta.url, // CLI package itself
57+
pathToFileURL(path.join(cwd, 'package.json')).href, // consumer workspace
58+
import.meta.url, // CLI package itself
5959
];
6060

6161
for (const base of resolutionBases) {
6262
try {
6363
const req = createRequire(base);
6464
const resolved = req.resolve('@objectstack/studio/package.json');
6565
return path.dirname(resolved);
66-
} catch (e) {
66+
} catch {
6767
// Not resolvable from this base — try next
68-
if (process.env.DEBUG) console.error(` [studio] resolve from ${base} failed:`, (e as Error).message);
6968
}
7069
}
7170

72-
// Last resort: direct filesystem check in node_modules
71+
// Last resort: direct filesystem check in cwd/node_modules
7372
const directPath = path.join(cwd, 'node_modules', '@objectstack', 'studio');
7473
if (fs.existsSync(path.join(directPath, 'package.json'))) {
7574
return directPath;

0 commit comments

Comments
 (0)