Skip to content

Commit

Permalink
Improve resolve reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Dec 6, 2023
1 parent b4e90de commit f52da81
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 134 deletions.
7 changes: 7 additions & 0 deletions packages/cspell-lib/api/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -955,9 +955,16 @@ declare function setLogger(logger: Logger): Logger;
declare function getLogger(): Logger;

interface ResolveFileResult {
/**
* Absolute path or URL to the file.
*/
filename: string;
relativeTo: string | undefined;
found: boolean;
/**
* A warning message if the file was found, but there was a problem.
*/
warning?: string;
}
/**
* Resolve filename to absolute paths.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,10 @@ function resolveFilename(filename: string | URL, relativeTo: string | URL): Impo
if (filename instanceof URL) return { filename: toFilePathOrHref(filename) };
const r = resolveFile(filename, relativeTo);

if (r.warning) {
logWarning(r.warning);
}

return {
filename: r.filename.startsWith('file:/') ? fileURLToPath(r.filename) : r.filename,
error: r.found ? undefined : new Error(`Failed to resolve file: "${filename}"`),
Expand Down
48 changes: 44 additions & 4 deletions packages/cspell-lib/src/lib/util/resolveFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,16 @@ import {
} from './url.js';

export interface ResolveFileResult {
/**
* Absolute path or URL to the file.
*/
filename: string;
relativeTo: string | undefined;
found: boolean;
/**
* A warning message if the file was found, but there was a problem.
*/
warning?: string;
}

const testNodeModules = /^node_modules\//;
Expand All @@ -45,6 +52,7 @@ export function resolveFile(filename: string, relativeTo: string | URL): Resolve
{ filename, fn: tryResolveFrom },
{ filename: filename.replace(testNodeModules, ''), fn: tryResolveFrom },
{ filename, fn: tryResolveGlobal },
{ filename, fn: tryLegacyResolve },
];

for (const step of steps) {
Expand Down Expand Up @@ -119,7 +127,7 @@ function tryNodeResolveDefaultPaths(filename: string): ResolveFileResult | undef

function tryNodeRequireResolve(filenameOrURL: string, relativeTo: string | URL): ResolveFileResult | undefined {
const filename = fileURLOrPathToPath(filenameOrURL);
const relativeToPath = fileURLOrPathToPath(relativeTo);
const relativeToPath = pathFromRelativeTo(relativeTo);
const home = os.homedir();
function calcPaths(p: string) {
const paths = [p];
Expand Down Expand Up @@ -157,9 +165,13 @@ function tryResolveGlobal(filename: string): ResolveFileResult | undefined {
}

function tryResolveExists(filename: string | URL, relativeTo: string | URL): ResolveFileResult | undefined {
if (filename instanceof URL || isURLLike(filename) || isURLLike(relativeTo)) return undefined;
if (filename instanceof URL || isURLLike(filename) || (isURLLike(relativeTo) && !isFileURL(relativeTo))) {
return undefined;
}

relativeTo = pathFromRelativeTo(relativeTo);

const toTry = [{ filename }, { filename: path.resolve(relativeTo.toString(), filename), relativeTo }];
const toTry = [{ filename }, { filename: path.resolve(relativeTo, filename), relativeTo }];
for (const { filename, relativeTo } of toTry) {
const found = path.isAbsolute(filename) && fs.existsSync(filename);
if (found) return { filename, relativeTo: relativeTo?.toString(), found };
Expand All @@ -175,13 +187,37 @@ function tryResolveExists(filename: string | URL, relativeTo: string | URL): Res
function tryResolveFrom(filename: string, relativeTo: string | URL): ResolveFileResult | undefined {
if (relativeTo instanceof URL) return undefined;
try {
return { filename: resolveFrom(relativeTo, filename), relativeTo, found: true };
return { filename: resolveFrom(pathFromRelativeTo(relativeTo), filename), relativeTo, found: true };
} catch (error) {
// Failed to resolve a relative module request
return undefined;
}
}

const regExpStartsWidthNodeModules = /^node_modules[/\\]/;

function tryLegacyResolve(filename: string | URL, relativeTo: string | URL): ResolveFileResult | undefined {
if (filename instanceof URL || isURLLike(filename) || (isURLLike(relativeTo) && !isFileURL(relativeTo))) {
return undefined;
}

const relativeToPath = isURLLike(relativeTo) ? fileURLToPath(new URL('./', relativeTo)) : relativeTo.toString();

const match = filename.match(regExpStartsWidthNodeModules);

if (match) {
const found = tryImportResolve(filename.replace(regExpStartsWidthNodeModules, ''), relativeToPath);
if (found?.found) {
found.warning = `Import of '${filename}' should not start with '${match[0]}' in '${toFilePathOrHref(
relativeTo,
)}'`;
return found;
}
}

return undefined;
}

function isRelative(filename: string | URL): boolean {
if (filename instanceof URL) return false;
if (filename.startsWith('./')) return true;
Expand All @@ -197,6 +233,10 @@ function joinWith(filename: string, relativeTo: string | URL): string {
: path.resolve(relativeTo, filename);
}

function pathFromRelativeTo(relativeTo: string | URL): string {
return relativeTo instanceof URL || isURLLike(relativeTo) ? fileURLToPath(new URL('./', relativeTo)) : relativeTo;
}

export const __testing__ = {
isRelative,
isFileURL,
Expand Down
6 changes: 5 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 2 additions & 122 deletions test-fixtures/issues/issue-5034/.cspell.json
Original file line number Diff line number Diff line change
@@ -1,126 +1,6 @@
{
"version": "0.2",
"ignorePaths": [
".neoconf.json",
".vscode/settings.json",
".vim/coc-settings.json",
"frontend/node_modules/**",
"frontend/coverage/**",
"frontend/build/**",
"frontend/db/dist/**",
"frontend/package.json",
"frontend/public/locales/ar/**",
"frontend/public/locales/zgh/**",
"frontend/public/supportedBrowsers.js",
"docker-compose*.yml",
"cloudformation.yml",
"vendor",
"web/src/composer.json",
"web/src/vendor",
"web/src/shared/scripts",
"web/src/css",
"web/src/locales/ar/translations.yml",
"node/src/locales/ar.json",
"web/src/locales/zgh/translations.yml",
"web/src/api/Validation/lang/ar/validation.php",
".env",
"renovate.json",
"web/conf/**/*",
"*Dockerfile"
],
"allowCompoundWords": true,
"ignoreWords": [
"culturehq",
"yiisoft",
"lintstagedrc",
"rehype",
"noto",
"graphiql",
"webp"
],
"words": [
"AMZN",
"capce",
"cedata",
"crunz",
"cronitor",
"destructured",
"droppable",
"entityid",
"esetnik",
"esnext",
"falsey",
"Gravatar",
"hacky",
"Handtevy",
"hocs",
"iframe",
"imgix",
"immer",
"junit",
"jwplayer",
"JWPLAYER",
"Lngs",
"luxon",
"Maknz",
"Mebibytes",
"msodbcsql",
"newid",
"noopener",
"NREMT",
"nuka",
"oems",
"onstatechange",
"onupdatefound",
"phpcs",
"phpcbf",
"phuocng",
"recertification",
"reduxjs",
"resizer",
"signup",
"testid",
"tamazight",
"tifinagh",
"tsql",
"Tuupola",
"unmount",
"unregister",
"UPDLOCK",
"wdyr",
"xlarge"
],
"language": "en,fr",
"import": [
"./frontend/node_modules/@cspell/dict-fr-fr/cspell-ext.json"
],
"overrides": [
{
"filename": "**/*.md",
"language": "en,fr"
},
{
"filename": "**/locales/fr/**/{*.json,*.yml}",
"language": "en,fr"
},
{
"filename": "**/__test__/**",
"language": "en,fr"
},
{
"filename": "**/{*.test.ts,*.test.tsx}",
"language": "en,fr"
},
{
"filename": "**/Validation/lang/fr/validation.php",
"language": "en,fr"
},
{
"filename": "node/src/locales/fr.json",
"language": "en,fr"
},
{
"filename": "frontend/src/themes/*.json",
"languageId": "css"
}
"frontend/node_modules/@cspell/dict-fr-fr/cspell-ext.json"
]
}

This file was deleted.

2 changes: 1 addition & 1 deletion test-fixtures/issues/issue-5034/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"dependencies": {},
"scripts": {
"spellcheck": "cspell -c \"../.cspell.json\" -r \"..\" --no-progress --dot --gitignore \"**/*.{js,jsx,ts,tsx,json,yml,md}\"",
"spellcheck": "cspell -c \"../.cspell.json\" -r \"..\" --dot --gitignore \"**/*.{js,jsx,ts,tsx,json,yml,md}\"",
"test": "pnpm run spellcheck"
},
"devDependencies": {
Expand Down
9 changes: 9 additions & 0 deletions test-fixtures/issues/issue-5034/frontend/src/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Read me

Related to [#5034](https://github.com/streetsidesoftware/cspell/issues/5034)

bon après-midi

<!---
cspell:language en,fr
--->
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: Nested Config
version: "0.2"
import: "node_modules/@cspell/dict-fr-fr/cspell-ext.json"
words:
- cspell
6 changes: 4 additions & 2 deletions test-fixtures/issues/issue-5034/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"private": true,
"dependencies": {},
"scripts": {
"test": "yarn --cwd frontend spellcheck"
"test": "pnpm run -r spellcheck"
},
"devDependencies": {}
"devDependencies": {
"cspell": "workspace:*"
}
}

0 comments on commit f52da81

Please sign in to comment.