Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions scopes/harmony/bit/hook-require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ export function hookRequire() {
if (!pkgJson.main || pkgJson.main === 'index.js') throw firstErr;
return this.constructor._load(path.join(id, pkgJson.main), this);
} catch {
// Last resort for Bit's own core-aspect packages (`@teambit/*`). In a workspace where
// these are source components (most notably the Bit repo itself), an env loaded from
// root node_modules can eagerly `require('@teambit/builder')` during `bit install` —
// before the workspace component is compiled — so the resolved copy's `dist` main
// doesn't exist yet and the require above hard-crashes. Bit's own installation always
// ships a compiled copy of its core aspects, so re-resolve from this module's context
// (inside `@teambit/bit`) and load that instead of failing. Only runs on the crash
// path, so the happy path (dist present, e.g. every normal user workspace) is untouched.
if (id.startsWith('@teambit/')) {
try {
const fromBitContext = require.resolve(id, { paths: [__dirname] });
return this.constructor._load(fromBitContext, this);
} catch {
throw firstErr;
}
}
throw firstErr;
}
}
Expand Down