@@ -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