@@ -499,6 +499,34 @@ export class YAMLSchemaService extends JSONSchemaService {
499499 }
500500 } ;
501501
502+ const _resolveRefUri = ( parentSchemaURL : string , refUri : string ) : string => {
503+ const resolvedAgainstParent = _resolveAgainstBase ( parentSchemaURL , refUri ) ;
504+ if ( ! refUri . startsWith ( '/' ) ) return resolvedAgainstParent ;
505+ const parentResource = resourceIndexByUri . get ( parentSchemaURL ) ?. root ;
506+ const parentResourceId = parentResource ?. $id || parentResource ?. id ;
507+ const resolvedParentId = _resolveAgainstBase ( parentSchemaURL , parentResourceId ) ;
508+ if ( ! resolvedParentId . startsWith ( 'http://' ) && ! resolvedParentId . startsWith ( 'https://' ) ) return resolvedAgainstParent ;
509+
510+ return _resolveAgainstBase ( resolvedParentId , refUri ) ;
511+ } ;
512+
513+ const _resolveLocalSiblingFromRemoteUri = ( parentSchemaURL : string , resolvedRefUri : string ) : string | undefined => {
514+ try {
515+ const parentUri = URI . parse ( parentSchemaURL ) ;
516+ const targetUri = URI . parse ( resolvedRefUri ) ;
517+ if ( parentUri . scheme !== 'file' ) return undefined ;
518+ if ( targetUri . scheme !== 'http' && targetUri . scheme !== 'https' ) return undefined ;
519+
520+ const localFileName = path . posix . basename ( targetUri . path ) ;
521+ if ( ! localFileName ) return undefined ;
522+ const localDir = path . posix . dirname ( parentUri . path ) ;
523+ const localPath = path . posix . join ( localDir , localFileName ) ;
524+ return parentUri . with ( { path : localPath , query : targetUri . query , fragment : targetUri . fragment } ) . toString ( ) ;
525+ } catch {
526+ return undefined ;
527+ }
528+ } ;
529+
502530 const resolveExternalLink = (
503531 node : JSONSchema ,
504532 uri : string ,
@@ -541,42 +569,57 @@ export class YAMLSchemaService extends JSONSchemaService {
541569 ) ;
542570 } ;
543571
544- const resolvedUri = _resolveAgainstBase ( parentSchemaURL , uri ) ;
545- const embeddedSchema = resourceIndexByUri . get ( resolvedUri ) ?. root ;
546- if ( embeddedSchema ) {
547- return _attachResolvedSchema (
548- node ,
549- embeddedSchema ,
550- resolvedUri ,
551- linkPath ,
552- parentSchemaDependencies ,
553- parentSchemaDependencies ,
554- resolutionStack ,
555- recursiveAnchorBase ,
556- inheritedDynamicScope
557- ) ;
558- }
572+ const _resolveByUri = ( targetUris : string [ ] , index = 0 ) : Promise < unknown > => {
573+ const targetUri = targetUris [ index ] ;
559574
560- const referencedHandle = this . getOrAddSchemaHandle ( resolvedUri ) ;
561- return referencedHandle . getUnresolvedSchema ( ) . then ( async ( unresolvedSchema ) => {
562- if ( unresolvedSchema . errors . length ) {
563- const loc = linkPath ? resolvedUri + '#' + linkPath : resolvedUri ;
564- resolveErrors . push ( l10n . t ( "Problems loading reference '{0}': {1}" , loc , unresolvedSchema . errors [ 0 ] ) ) ;
575+ const embeddedSchema = resourceIndexByUri . get ( targetUri ) ?. root ;
576+ if ( embeddedSchema ) {
577+ return _attachResolvedSchema (
578+ node ,
579+ embeddedSchema ,
580+ targetUri ,
581+ linkPath ,
582+ parentSchemaDependencies ,
583+ parentSchemaDependencies ,
584+ resolutionStack ,
585+ recursiveAnchorBase ,
586+ inheritedDynamicScope
587+ ) ;
565588 }
566- // index resources for the newly loaded schema
567- await _indexSchemaResources ( unresolvedSchema . schema , resolvedUri ) ;
568- return _attachResolvedSchema (
569- node ,
570- unresolvedSchema . schema ,
571- resolvedUri ,
572- linkPath ,
573- parentSchemaDependencies ,
574- referencedHandle . dependencies ,
575- resolutionStack ,
576- recursiveAnchorBase ,
577- inheritedDynamicScope
578- ) ;
579- } ) ;
589+
590+ const referencedHandle = this . getOrAddSchemaHandle ( targetUri ) ;
591+ return referencedHandle . getUnresolvedSchema ( ) . then ( async ( unresolvedSchema ) => {
592+ if (
593+ unresolvedSchema . errors ?. some ( ( error ) => error . toLowerCase ( ) . includes ( 'unable to load schema from' ) ) &&
594+ index + 1 < targetUris . length
595+ ) {
596+ return _resolveByUri ( targetUris , index + 1 ) ;
597+ }
598+
599+ if ( unresolvedSchema . errors . length ) {
600+ const loc = linkPath ? targetUri + '#' + linkPath : targetUri ;
601+ resolveErrors . push ( l10n . t ( "Problems loading reference '{0}': {1}" , loc , unresolvedSchema . errors [ 0 ] ) ) ;
602+ }
603+ // index resources for the newly loaded schema
604+ await _indexSchemaResources ( unresolvedSchema . schema , targetUri ) ;
605+ return _attachResolvedSchema (
606+ node ,
607+ unresolvedSchema . schema ,
608+ targetUri ,
609+ linkPath ,
610+ parentSchemaDependencies ,
611+ referencedHandle . dependencies ,
612+ resolutionStack ,
613+ recursiveAnchorBase ,
614+ inheritedDynamicScope
615+ ) ;
616+ } ) ;
617+ } ;
618+
619+ const resolvedUri = _resolveRefUri ( parentSchemaURL , uri ) ;
620+ const localSiblingUri = _resolveLocalSiblingFromRemoteUri ( parentSchemaURL , resolvedUri ) ;
621+ const targetUris = localSiblingUri && localSiblingUri !== resolvedUri ? [ localSiblingUri , resolvedUri ] : [ resolvedUri ] ;
622+ return _resolveByUri ( targetUris ) ;
580623 } ;
581624
582625 const resolveRefs = async (
0 commit comments