Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle ModuleLoadError from hooks #836

Merged
merged 3 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
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
12 changes: 11 additions & 1 deletion src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,17 @@ export class Config implements IConfig {
} catch (error: any) {
final.failures.push({error: error as Error, plugin: p})
debug(error)
if (!captureErrors && error.oclif?.exit !== undefined) throw error
// Do not throw the error if
// captureErrors is set to true
// error.oclif.exit is undefined or 0
// error.code is MODULE_NOT_FOUND
if (
!captureErrors &&
error.oclif?.exit !== undefined &&
error.oclif?.exit !== 0 &&
error.code !== 'MODULE_NOT_FOUND'
)
throw error
}

marker?.addDetails({
Expand Down
3 changes: 2 additions & 1 deletion src/config/plugin-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export default class PluginLoader {
private async loadPlugins(
root: string,
type: string,
plugins: ({name?: string; root?: string; tag?: string} | string)[],
plugins: ({name?: string; root?: string; tag?: string; url?: string} | string)[],
parent?: Plugin.Plugin,
): Promise<void> {
if (!plugins || plugins.length === 0) return
Expand All @@ -112,6 +112,7 @@ export default class PluginLoader {
if (typeof plugin !== 'string') {
opts.tag = plugin.tag || opts.tag
opts.root = plugin.root || opts.root
opts.url = plugin.url
}

if (parent) {
Expand Down
10 changes: 8 additions & 2 deletions src/config/ts-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ function determinePath(root: string, orig: string): string {
export function tsPath(root: string, orig: string, plugin: Plugin): string
export function tsPath(root: string, orig: string | undefined, plugin?: Plugin): string | undefined
export function tsPath(root: string, orig: string | undefined, plugin?: Plugin): string | undefined {
const rootPlugin = Cache.getInstance().get('rootPlugin')
const rootPlugin = plugin?.options.isRoot ? plugin : Cache.getInstance().get('rootPlugin')

if (!orig) return orig
orig = orig.startsWith(root) ? orig : join(root, orig)
Expand All @@ -223,10 +223,16 @@ export function tsPath(root: string, orig: string | undefined, plugin?: Plugin):
memoizedWarn(
`${plugin?.name} is a linked ESM module and cannot be auto-transpiled. Existing compiled source will be used instead.`,
)

if (plugin?.options.url)
memoizedWarn(
`${plugin?.name} is an ESM module installed from github and cannot be auto-transpiled. Existing compiled source will be used instead.`,
)
return orig
}

if (settings.tsnodeEnabled === undefined && isProduction && plugin?.type !== 'link') {
// Do not skip ts-node registration if the plugin is linked or installed from github
if (settings.tsnodeEnabled === undefined && isProduction && plugin?.type !== 'link' && !plugin?.options.url) {
debug(`Skipping ts-node registration for ${root} because NODE_ENV is NOT "test" or "development"`)
return orig
}
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface PluginOptions {
root: string
tag?: string
type?: string
url?: string
}

export interface Options extends PluginOptions {
Expand Down Expand Up @@ -56,6 +57,7 @@ export interface Plugin {
* name from package.json
*/
name: string
readonly options: Options
/**
* full package.json
*
Expand Down
2 changes: 2 additions & 0 deletions test/config/config.flexible.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ describe('Config with flexible taxonomy', () => {
moduleType: 'commonjs',
hasManifest: false,
isRoot: false,
options: {root: ''},
}

const pluginB: IPlugin = {
Expand All @@ -125,6 +126,7 @@ describe('Config with flexible taxonomy', () => {
moduleType: 'commonjs',
hasManifest: false,
isRoot: false,
options: {root: ''},
}
const plugins = new Map().set(pluginA.name, pluginA).set(pluginB.name, pluginB)

Expand Down
2 changes: 2 additions & 0 deletions test/config/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ describe('Config', () => {
moduleType: 'commonjs',
hasManifest: false,
isRoot: false,
options: {root: ''},
}

const pluginB: IPlugin = {
Expand All @@ -314,6 +315,7 @@ describe('Config', () => {
moduleType: 'commonjs',
hasManifest: false,
isRoot: false,
options: {root: ''},
}
const plugins = new Map().set(pluginA.name, pluginA).set(pluginB.name, pluginB)
let test = fancy
Expand Down
Loading