Skip to content

Commit

Permalink
fix: resolve provider path before requiring it (#9601)
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage authored Oct 16, 2024
1 parent 3388764 commit 20428da
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 42 deletions.
8 changes: 6 additions & 2 deletions packages/core/modules-sdk/src/loaders/register-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 : {}
Expand Down
92 changes: 52 additions & 40 deletions packages/core/modules-sdk/src/loaders/utils/load-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -368,50 +372,56 @@ export async function loadModuleMigrations(
revertMigration?: MigrationFunction
generateMigration?: MigrationFunction
}> {
const mainLoadedModule = await resolveModuleExports({
resolution: { ...resolution, moduleExports },
})
const runMigrationsFn: ((...args) => Promise<any>)[] = []
const revertMigrationFn: ((...args) => Promise<any>)[] = []
const generateMigrationFn: ((...args) => Promise<any>)[] = []

const loadedServices = [mainLoadedModule] as (
| ResolvedModule
| ResolvedModuleProvider
)[]
try {
const mainLoadedModule = await resolveModuleExports({
resolution: { ...resolution, moduleExports },
})
if ("error" in mainLoadedModule) {
throw mainLoadedModule.error
}

if (Array.isArray(resolution?.options?.providers)) {
for (const provider of (resolution.options as any).providers) {
const providerRes = provider.resolve as ModuleProviderExports
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,
},
})

const canLoadProvider =
providerRes && (isString(providerRes) || !providerRes?.services)
if ("error" in loadedProvider) {
throw loadedProvider.error
}

if (!canLoadProvider) {
continue
loadedServices.push(loadedProvider as ResolvedModuleProvider)
}

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 migrationScripts: any[] = []
for (const loadedModule of loadedServices) {
let runMigrationsCustom = loadedModule.runMigrations
Expand Down Expand Up @@ -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}`
)
}
}

Expand Down

0 comments on commit 20428da

Please sign in to comment.