Skip to content

Commit

Permalink
fix: runner/module resolution logic (#562)
Browse files Browse the repository at this point in the history

Co-authored-by: Hugo Dias <hugomrdias@gmail.com>
  • Loading branch information
Gozala and hugomrdias authored Jul 7, 2023
1 parent 88638bd commit a0c5be5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
25 changes: 8 additions & 17 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -506,25 +506,16 @@ export async function createCov(runner, coverage, file, outputDir) {
* @param {string} id - module id
* @param {string} [base=process.cwd()] - base path
*/
export async function resolveModule(id, base = toDirectoryPath(process.cwd())) {
let out
export async function resolveModule(id, base = process.cwd()) {
try {
out = await import(id)
} catch {}

if (!out) {
try {
const filePath = path.resolve(base, id)
fs.accessSync(filePath, fs.constants.R_OK)
out = await import(pathToFileURL(filePath).toString())
} catch {}
// Note we need to ensure base has trailing `/` or the the
// last entry is going to be dropped during resolution.
const path = createRequire(toDirectoryPath(base)).resolve(id)
const url = pathToFileURL(path)
return await import(url.href)
} catch (error) {
throw new Error(`Cannot resolve module "${id}" from "${base}"\n${/** @type {Error} */(error).message}`)
}

if (!out) {
throw new Error(`Cannot resolve module "${id}" from "${base}"`)
}

return out
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ describe('custom runner', function () {
const proc = await execa('./cli.js', [
'mocks/tinybench.js',
'--runner',
'mocks/custom-runner.js',
'./mocks/custom-runner.js',
])

is(proc.exitCode, 0, 'exit code')
Expand Down

0 comments on commit a0c5be5

Please sign in to comment.