fix: resolve provider path before requiring it (#9601)
This commit is contained in:
@@ -67,7 +67,9 @@ function getCustomModuleResolution(
|
||||
const originalPath = normalizeImportPathWithSource(
|
||||
(isString(moduleConfig) ? moduleConfig : moduleConfig.resolve) as string
|
||||
)
|
||||
const resolutionPath = require.resolve(originalPath)
|
||||
const resolutionPath = require.resolve(originalPath, {
|
||||
paths: [process.cwd()],
|
||||
})
|
||||
|
||||
const conf = isObject(moduleConfig)
|
||||
? moduleConfig
|
||||
@@ -142,7 +144,9 @@ function getInternalModuleResolution(
|
||||
const originalPath = normalizeImportPathWithSource(
|
||||
(isString(moduleConfig) ? moduleConfig : moduleConfig.resolve) as string
|
||||
)
|
||||
resolutionPath = require.resolve(originalPath)
|
||||
resolutionPath = require.resolve(originalPath, {
|
||||
paths: [process.cwd()],
|
||||
})
|
||||
}
|
||||
|
||||
const moduleDeclaration = isObj ? moduleConfig : {}
|
||||
|
||||
@@ -143,7 +143,11 @@ async function loadInternalProvider(
|
||||
...resolution.definition,
|
||||
key: provider.id,
|
||||
},
|
||||
resolutionPath: isString(provider.resolve) ? provider.resolve : false,
|
||||
resolutionPath: isString(provider.resolve)
|
||||
? require.resolve(provider.resolve, {
|
||||
paths: [process.cwd()],
|
||||
})
|
||||
: false,
|
||||
},
|
||||
logger,
|
||||
migrationOnly,
|
||||
@@ -368,50 +372,56 @@ export async function loadModuleMigrations(
|
||||
revertMigration?: MigrationFunction
|
||||
generateMigration?: MigrationFunction
|
||||
}> {
|
||||
const mainLoadedModule = await resolveModuleExports({
|
||||
resolution: { ...resolution, moduleExports },
|
||||
})
|
||||
|
||||
const loadedServices = [mainLoadedModule] as (
|
||||
| ResolvedModule
|
||||
| ResolvedModuleProvider
|
||||
)[]
|
||||
|
||||
if (Array.isArray(resolution?.options?.providers)) {
|
||||
for (const provider of (resolution.options as any).providers) {
|
||||
const providerRes = provider.resolve as ModuleProviderExports
|
||||
|
||||
const canLoadProvider =
|
||||
providerRes && (isString(providerRes) || !providerRes?.services)
|
||||
|
||||
if (!canLoadProvider) {
|
||||
continue
|
||||
}
|
||||
|
||||
const loadedProvider = await resolveModuleExports({
|
||||
resolution: {
|
||||
...resolution,
|
||||
moduleExports: !isString(providerRes) ? providerRes : undefined,
|
||||
definition: {
|
||||
...resolution.definition,
|
||||
key: provider.id,
|
||||
},
|
||||
resolutionPath: isString(provider.resolve) ? provider.resolve : false,
|
||||
},
|
||||
})
|
||||
loadedServices.push(loadedProvider as ResolvedModuleProvider)
|
||||
}
|
||||
}
|
||||
|
||||
if ("error" in mainLoadedModule) {
|
||||
throw mainLoadedModule.error
|
||||
}
|
||||
|
||||
const runMigrationsFn: ((...args) => Promise<any>)[] = []
|
||||
const revertMigrationFn: ((...args) => Promise<any>)[] = []
|
||||
const generateMigrationFn: ((...args) => Promise<any>)[] = []
|
||||
|
||||
try {
|
||||
const mainLoadedModule = await resolveModuleExports({
|
||||
resolution: { ...resolution, moduleExports },
|
||||
})
|
||||
if ("error" in mainLoadedModule) {
|
||||
throw mainLoadedModule.error
|
||||
}
|
||||
|
||||
const loadedServices = [mainLoadedModule] as (
|
||||
| ResolvedModule
|
||||
| ResolvedModuleProvider
|
||||
)[]
|
||||
|
||||
if (Array.isArray(resolution?.options?.providers)) {
|
||||
for (const provider of (resolution.options as any).providers) {
|
||||
const providerRes = provider.resolve as ModuleProviderExports
|
||||
|
||||
const canLoadProvider =
|
||||
providerRes && (isString(providerRes) || !providerRes?.services)
|
||||
|
||||
if (!canLoadProvider) {
|
||||
continue
|
||||
}
|
||||
|
||||
const loadedProvider = await resolveModuleExports({
|
||||
resolution: {
|
||||
...resolution,
|
||||
moduleExports: !isString(providerRes) ? providerRes : undefined,
|
||||
definition: {
|
||||
...resolution.definition,
|
||||
key: provider.id,
|
||||
},
|
||||
resolutionPath: isString(provider.resolve)
|
||||
? provider.resolve
|
||||
: false,
|
||||
},
|
||||
})
|
||||
|
||||
if ("error" in loadedProvider) {
|
||||
throw loadedProvider.error
|
||||
}
|
||||
|
||||
loadedServices.push(loadedProvider as ResolvedModuleProvider)
|
||||
}
|
||||
}
|
||||
|
||||
const migrationScripts: any[] = []
|
||||
for (const loadedModule of loadedServices) {
|
||||
let runMigrationsCustom = loadedModule.runMigrations
|
||||
@@ -476,8 +486,10 @@ export async function loadModuleMigrations(
|
||||
revertMigration,
|
||||
generateMigration,
|
||||
}
|
||||
} catch {
|
||||
return {}
|
||||
} catch (e) {
|
||||
throw new Error(
|
||||
`Unable to resolve the migration scripts for the module ${resolution.definition.key}\n${e.message}\n${e.stack}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user