Skip to content

Commit 2e9bd07

Browse files
author
Alasdair McLeay
committed
handle require.resolve failure
if require.resolve fails, defer to default behaviour -url.resolve(base, ref)
1 parent b170858 commit 2e9bd07

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

extensions/css-language-features/server/src/utils/documentContext.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@ function getModuleNameFromPath(path: string) {
1919
function resolvePathToModule(moduleName: string, relativeTo: string) {
2020
// if we require.resolve('my-module') then it will follow the main property in the linked package.json
2121
// but we want the root of the module so resolve to the package.json and then trim
22-
return require
23-
.resolve(`${moduleName}/package.json`, { paths: [relativeTo] })
24-
.slice(0, -12); // remove trailing `package.json`
22+
let resolved;
23+
try {
24+
resolved = require
25+
.resolve(`${moduleName}/package.json`, { paths: [relativeTo] });
26+
}
27+
catch (ex) {
28+
return null;
29+
}
30+
return resolved.slice(0, -12); // remove trailing `package.json`
2531
}
2632

2733
export function getDocumentContext(documentUri: string, workspaceFolders: WorkspaceFolder[]): DocumentContext {
@@ -55,8 +61,10 @@ export function getDocumentContext(documentUri: string, workspaceFolders: Worksp
5561
if (ref[0] === '~' && ref[1] !== '/') {
5662
const moduleName = getModuleNameFromPath(ref.substring(1));
5763
const modulePath = resolvePathToModule(moduleName, base);
58-
const pathWithinModule = ref.substring(moduleName.length + 2);
59-
return url.resolve(modulePath, pathWithinModule);
64+
if (modulePath) {
65+
const pathWithinModule = ref.substring(moduleName.length + 2);
66+
return url.resolve(modulePath, pathWithinModule);
67+
}
6068
}
6169
return url.resolve(base, ref);
6270
},

0 commit comments

Comments
 (0)