Skip to content

Commit

Permalink
fix: Fix dynamic loader on Windows (#4707)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S authored Aug 8, 2023
1 parent aa78fdb commit 702d9f4
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 95 deletions.
3 changes: 3 additions & 0 deletions packages/cspell/fixtures/features/reporter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Reporter Test

This directory is used to run a test on the json-reporter.
4 changes: 4 additions & 0 deletions packages/cspell/fixtures/features/reporter/cspell.config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
reporters:
- "@cspell/cspell-json-reporter"
files:
- "*"
2 changes: 1 addition & 1 deletion packages/cspell/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
},
"homepage": "https://streetsidesoftware.github.io/cspell/",
"dependencies": {
"@cspell/cspell-json-reporter": "workspace:*",
"@cspell/cspell-pipe": "workspace:*",
"@cspell/cspell-types": "workspace:*",
"@cspell/dynamic-import": "workspace:*",
Expand All @@ -110,7 +111,6 @@
"node": ">=14"
},
"devDependencies": {
"@cspell/cspell-json-reporter": "workspace:*",
"@types/file-entry-cache": "^5.0.2",
"@types/glob": "^8.1.0",
"@types/imurmurhash": "^0.1.1",
Expand Down
21 changes: 21 additions & 0 deletions packages/cspell/src/app/__snapshots__/app.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,27 @@ exports[`Validate cli > app 'not found error by default' Expect Error: [Function
exports[`Validate cli > app 'not found error by default' Expect Error: [Function CheckFailed] 3`] = `""`;
exports[`Validate cli > app 'reporter' Expect Error: undefined 1`] = `[]`;
exports[`Validate cli > app 'reporter' Expect Error: undefined 2`] = `
"log {
log \\"issues\\": [],
log \\"info\\": [],
log \\"debug\\": [],
log \\"error\\": [],
log \\"progress\\": [],
log \\"result\\": {
log \\"files\\": 2,
log \\"filesWithIssues\\": [],
log \\"issues\\": 0,
log \\"errors\\": 0,
log \\"cachedFiles\\": 0
log }
log }"
`;
exports[`Validate cli > app 'reporter' Expect Error: undefined 3`] = `""`;
exports[`Validate cli > app 'samples/Dutch.txt' Expect Error: [Function CheckFailed] 1`] = `[]`;
exports[`Validate cli > app 'samples/Dutch.txt' Expect Error: [Function CheckFailed] 2`] = `
Expand Down
71 changes: 36 additions & 35 deletions packages/cspell/src/app/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,41 +141,42 @@ describe('Validate cli', () => {
const failFastRoot = pathSamples('fail-fast');

test.each`
msg | testArgs | errorCheck | eError | eLog | eInfo
${'with errors and excludes'} | ${['-r', 'samples', '*', '-e', 'Dutch.txt', '-c', 'samples/.cspell.json']} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'no-args'} | ${[]} | ${'outputHelp'} | ${false} | ${false} | ${false}
${'--help'} | ${['--help']} | ${'outputHelp'} | ${false} | ${false} | ${false}
${'current_file'} | ${[__filename]} | ${undefined} | ${true} | ${false} | ${false}
${'with spelling errors Dutch.txt'} | ${[pathSamples('Dutch.txt')]} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'with spelling errors Dutch.txt words only'} | ${[pathSamples('Dutch.txt'), '--wordsOnly']} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'with spelling errors Dutch.txt --legacy'} | ${[pathSamples('Dutch.txt'), '--legacy']} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'with spelling errors --silent Dutch.txt'} | ${['--silent', pathSamples('Dutch.txt')]} | ${app.CheckFailed} | ${false} | ${false} | ${false}
${'current_file languageId'} | ${[__filename, '--languageId=typescript']} | ${undefined} | ${true} | ${false} | ${false}
${'check help'} | ${['check', '--help']} | ${'outputHelp'} | ${false} | ${false} | ${false}
${'check LICENSE'} | ${['check', pathRoot('LICENSE')]} | ${undefined} | ${false} | ${true} | ${false}
${'check missing'} | ${['check', pathRoot('missing-file.txt')]} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'check with spelling errors'} | ${['check', pathSamples('Dutch.txt')]} | ${app.CheckFailed} | ${false} | ${true} | ${false}
${'LICENSE'} | ${[pathRoot('LICENSE')]} | ${undefined} | ${true} | ${false} | ${false}
${'samples/Dutch.txt'} | ${[pathSamples('Dutch.txt')]} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'with forbidden words'} | ${[pathSamples('src/sample-with-forbidden-words.md')]} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'current_file --verbose'} | ${['--verbose', __filename]} | ${undefined} | ${true} | ${false} | ${true}
${'bad config'} | ${['-c', __filename, __filename]} | ${app.CheckFailed} | ${true} | ${false} | ${false}
${'not found error by default'} | ${['*.not']} | ${app.CheckFailed} | ${true} | ${false} | ${false}
${'must find with error'} | ${['*.not', '--must-find-files']} | ${app.CheckFailed} | ${true} | ${false} | ${false}
${'must find force no error'} | ${['*.not', '--no-must-find-files']} | ${undefined} | ${true} | ${false} | ${false}
${'cspell-bad.json'} | ${['-c', pathSamples('cspell-bad.json'), __filename]} | ${undefined} | ${true} | ${false} | ${false}
${'cspell-import-missing.json'} | ${['-c', pathSamples('linked/cspell-import-missing.json'), __filename]} | ${app.CheckFailed} | ${true} | ${false} | ${false}
${'--fail-fast no option'} | ${['-r', failFastRoot, '*.txt']} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'--fail-fast with option'} | ${['-r', failFastRoot, '--fail-fast', '*.txt']} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'--fail-fast with config'} | ${['-r', failFastRoot, '-c', failFastConfig, '*.txt']} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'--no-fail-fast with config'} | ${['-r', failFastRoot, '--no-fail-fast', '-c', failFastConfig, '*.txt']} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'issue-2998 --language-id'} | ${['-r', pathFix('issue-2998'), '-v', '--language-id=fix', 'fix-words.txt']} | ${undefined} | ${true} | ${false} | ${true}
${'Explicit file://'} | ${['-r', pathFix('misc'), 'file://star-not.md']} | ${undefined} | ${true} | ${false} | ${false}
${'Explicit not found file://'} | ${['-r', pathFix('misc'), 'file://not-fond.md']} | ${app.CheckFailed} | ${true} | ${false} | ${false}
${'typos'} | ${['-r', pathFix('features/typos'), '--no-progress', '.']} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'typos --no-show-suggestions'} | ${['-r', pathFix('features/typos'), '--no-progress', '--no-show-suggestions', '.']} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'typos --show-suggestions'} | ${['-r', pathFix('features/typos'), '--no-progress', '--show-suggestions', '**']} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'inline suggest'} | ${['-r', pathFix('features/inline-suggest'), '--no-progress', '--show-suggestions', '.']} | ${app.CheckFailed} | ${true} | ${true} | ${false}
msg | testArgs | errorCheck | eError | eLog | eInfo
${'with errors and excludes'} | ${['-r', 'samples', '*', '-e', 'Dutch.txt', '-c', 'samples/.cspell.json']} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'no-args'} | ${[]} | ${'outputHelp'} | ${false} | ${false} | ${false}
${'--help'} | ${['--help']} | ${'outputHelp'} | ${false} | ${false} | ${false}
${'current_file'} | ${[__filename]} | ${undefined} | ${true} | ${false} | ${false}
${'with spelling errors Dutch.txt'} | ${[pathSamples('Dutch.txt')]} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'with spelling errors Dutch.txt words only'} | ${[pathSamples('Dutch.txt'), '--wordsOnly']} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'with spelling errors Dutch.txt --legacy'} | ${[pathSamples('Dutch.txt'), '--legacy']} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'with spelling errors --silent Dutch.txt'} | ${['--silent', pathSamples('Dutch.txt')]} | ${app.CheckFailed} | ${false} | ${false} | ${false}
${'current_file languageId'} | ${[__filename, '--languageId=typescript']} | ${undefined} | ${true} | ${false} | ${false}
${'check help'} | ${['check', '--help']} | ${'outputHelp'} | ${false} | ${false} | ${false}
${'check LICENSE'} | ${['check', pathRoot('LICENSE')]} | ${undefined} | ${false} | ${true} | ${false}
${'check missing'} | ${['check', pathRoot('missing-file.txt')]} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'check with spelling errors'} | ${['check', pathSamples('Dutch.txt')]} | ${app.CheckFailed} | ${false} | ${true} | ${false}
${'LICENSE'} | ${[pathRoot('LICENSE')]} | ${undefined} | ${true} | ${false} | ${false}
${'samples/Dutch.txt'} | ${[pathSamples('Dutch.txt')]} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'with forbidden words'} | ${[pathSamples('src/sample-with-forbidden-words.md')]} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'current_file --verbose'} | ${['--verbose', __filename]} | ${undefined} | ${true} | ${false} | ${true}
${'bad config'} | ${['-c', __filename, __filename]} | ${app.CheckFailed} | ${true} | ${false} | ${false}
${'not found error by default'} | ${['*.not']} | ${app.CheckFailed} | ${true} | ${false} | ${false}
${'must find with error'} | ${['*.not', '--must-find-files']} | ${app.CheckFailed} | ${true} | ${false} | ${false}
${'must find force no error'} | ${['*.not', '--no-must-find-files']} | ${undefined} | ${true} | ${false} | ${false}
${'cspell-bad.json'} | ${['-c', pathSamples('cspell-bad.json'), __filename]} | ${undefined} | ${true} | ${false} | ${false}
${'cspell-import-missing.json'} | ${['-c', pathSamples('linked/cspell-import-missing.json'), __filename]} | ${app.CheckFailed} | ${true} | ${false} | ${false}
${'--fail-fast no option'} | ${['-r', failFastRoot, '*.txt']} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'--fail-fast with option'} | ${['-r', failFastRoot, '--fail-fast', '*.txt']} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'--fail-fast with config'} | ${['-r', failFastRoot, '-c', failFastConfig, '*.txt']} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'--no-fail-fast with config'} | ${['-r', failFastRoot, '--no-fail-fast', '-c', failFastConfig, '*.txt']} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'issue-2998 --language-id'} | ${['-r', pathFix('issue-2998'), '-v', '--language-id=fix', 'fix-words.txt']} | ${undefined} | ${true} | ${false} | ${true}
${'Explicit file://'} | ${['-r', pathFix('misc'), 'file://star-not.md']} | ${undefined} | ${true} | ${false} | ${false}
${'Explicit not found file://'} | ${['-r', pathFix('misc'), 'file://not-fond.md']} | ${app.CheckFailed} | ${true} | ${false} | ${false}
${'typos'} | ${['-r', pathFix('features/typos'), '--no-progress', '.']} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'typos --no-show-suggestions'} | ${['-r', pathFix('features/typos'), '--no-progress', '--no-show-suggestions', '.']} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'typos --show-suggestions'} | ${['-r', pathFix('features/typos'), '--no-progress', '--show-suggestions', '**']} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'inline suggest'} | ${['-r', pathFix('features/inline-suggest'), '--no-progress', '--show-suggestions', '.']} | ${app.CheckFailed} | ${true} | ${true} | ${false}
${'reporter'} | ${['-r', pathFix('features/reporter'), '-c', pathFix('features/reporter/cspell.config.yaml')]} | ${undefined} | ${false} | ${true} | ${false}
`('app $msg Expect Error: $errorCheck', async ({ testArgs, errorCheck, eError, eLog, eInfo }: TestCase) => {
chalk.level = 1;
const commander = getCommander();
Expand Down
25 changes: 17 additions & 8 deletions packages/dynamic-import/src/esm/dynamicImport.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { sep as pathSep } from 'path';
import { pathToFileURL } from 'url';

const isWindowsPath = /^[a-z]:\\/i;

/**
* Dynamically import a module using `import`.
* @param moduleName - name of module, or relative path.
Expand All @@ -12,9 +14,12 @@ export async function dynamicImportFrom<Module>(
paths: string | URL | (string | URL)[] | undefined
): Promise<Module> {
paths = Array.isArray(paths) ? paths : paths ? [paths] : undefined;
const modulesNameToImport =
typeof moduleName === 'string' && isWindowsPath.test(moduleName) ? pathToFileURL(moduleName) : moduleName;

if (!paths || !paths.length || typeof moduleName !== 'string') {
try {
return await import(moduleName.toString());
return await import(modulesNameToImport.toString());
} catch (e) {
// console.log('%o', e);
const err = toError(e);
Expand All @@ -30,16 +35,20 @@ export async function dynamicImportFrom<Module>(
let lastError = undefined;

for (const parent of paths) {
const url =
typeof parent === 'string'
? parent.startsWith('file://')
? new URL(parent)
: pathToFileURL(parent + pathSep)
: parent;
let resolved = '';
let location = '';
try {
const url =
typeof parent === 'string'
? parent.startsWith('file://')
? new URL(parent)
: pathToFileURL(parent + pathSep)
: parent;
const location = await resolve(moduleName, url.toString());
resolved = await resolve(modulesNameToImport.toString(), url.toString());
location = isWindowsPath.test(resolved) ? pathToFileURL(resolved).toString() : resolved;
return await import(location);
} catch (err) {
// console.warn('%o', { moduleName, modulesNameToImport, paths, parentUrl: url, err, resolved, location });
lastError = err;
}
}
Expand Down
Loading

0 comments on commit 702d9f4

Please sign in to comment.