Skip to content

Commit b170858

Browse files
author
Alasdair McLeay
committed
resolve to package root
extract resolvePathToModule and ensure we resolve to package root
1 parent 30b5469 commit b170858

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ function getModuleNameFromPath(path: string) {
1616
return path.substring(0, path.indexOf('/'));
1717
}
1818

19+
function resolvePathToModule(moduleName: string, relativeTo: string) {
20+
// if we require.resolve('my-module') then it will follow the main property in the linked package.json
21+
// 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`
25+
}
26+
1927
export function getDocumentContext(documentUri: string, workspaceFolders: WorkspaceFolder[]): DocumentContext {
2028
function getRootFolder(): string | undefined {
2129
for (let folder of workspaceFolders) {
@@ -46,7 +54,7 @@ export function getDocumentContext(documentUri: string, workspaceFolders: Worksp
4654
// *unless* it starts with "~/" as this refers to the user's home directory.
4755
if (ref[0] === '~' && ref[1] !== '/') {
4856
const moduleName = getModuleNameFromPath(ref.substring(1));
49-
const modulePath = require.resolve(moduleName, { paths: [base] });
57+
const modulePath = resolvePathToModule(moduleName, base);
5058
const pathWithinModule = ref.substring(moduleName.length + 2);
5159
return url.resolve(modulePath, pathWithinModule);
5260
}

0 commit comments

Comments
 (0)