Skip to content

Commit 06634e1

Browse files
committed
Search in parent folders for node module resolution
as per comments from @octref on microsoft#78894 microsoft#78894 (comment)
1 parent 237c803 commit 06634e1

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ function getModuleNameFromPath(path: string) {
1919
return path.substring(0, path.indexOf('/'));
2020
}
2121

22-
function resolvePathToModule(_moduleName: string, _relativeTo: string): string | undefined {
22+
function resolvePathToModule(_moduleName: string, _relativeToFolder: string, _rootFolder: string | undefined): string | undefined {
2323
// resolve the module relative to the document. We can't use `require` here as the code is webpacked.
24-
const documentFolder = dirname(URI.parse(_relativeTo).fsPath);
25-
const packPath = join(documentFolder, 'node_modules', _moduleName, 'package.json');
24+
25+
const packPath = join(_relativeToFolder, 'node_modules', _moduleName, 'package.json');
2626
if (existsSync(packPath)) {
2727
return URI.file(packPath).toString();
28+
} else if (_rootFolder && _relativeToFolder.startsWith(_rootFolder) && (_relativeToFolder.length !== _rootFolder.length)) {
29+
return resolvePathToModule(_moduleName, dirname(_relativeToFolder), _rootFolder);
2830
}
2931
return undefined;
3032
}
@@ -61,7 +63,13 @@ export function getDocumentContext(documentUri: string, workspaceFolders: Worksp
6163
ref = ref.substring(1);
6264
if (startsWith(base, 'file://')) {
6365
const moduleName = getModuleNameFromPath(ref);
64-
const modulePath = resolvePathToModule(moduleName, base);
66+
const rootFolderUri = getRootFolder();
67+
let rootFolder;
68+
if (rootFolderUri) {
69+
rootFolder = URI.parse(rootFolderUri).fsPath;
70+
}
71+
const documentFolder = dirname(URI.parse(base).fsPath);
72+
const modulePath = resolvePathToModule(moduleName, documentFolder, rootFolder);
6573
if (modulePath) {
6674
const pathWithinModule = ref.substring(moduleName.length + 1);
6775
return url.resolve(modulePath, pathWithinModule);

0 commit comments

Comments
 (0)