@@ -737,60 +737,63 @@ async def ensure_initialized(self) -> bool:
737737 if not self ._initialized :
738738 async with self ._initialize_lock :
739739 if not self ._initialized :
740+ try :
741+ self ._logger .debug (f"ensure_initialized -> initialize { self .document } " )
740742
741- self ._logger .debug (f"ensure_initialized -> initialize { self .document } " )
742-
743- imports = await self .get_imports ()
744-
745- data_entry : Optional [Namespace .DataEntry ] = None
746- if self .document is not None :
747- # check or save several data in documents data cache,
748- # if imports are different, then the data is invalid
749- old_imports : List [Import ] = self .document .get_data (Namespace )
750- if old_imports is None :
751- self .document .set_data (Namespace , imports )
752- elif old_imports != imports :
753- new_imports = []
754- for e in old_imports :
755- if e in imports :
756- new_imports .append (e )
757- for e in imports :
758- if e not in new_imports :
759- new_imports .append (e )
760- self .document .set_data (Namespace , new_imports )
761- self .document .set_data (Namespace .DataEntry , None )
762- else :
763- data_entry = self .document .get_data (Namespace .DataEntry )
764-
765- if data_entry is not None :
766- self ._libraries = data_entry .libraries .copy ()
767- self ._resources = data_entry .resources .copy ()
768- self ._variables = data_entry .variables .copy ()
769- self ._diagnostics = data_entry .diagnostics .copy ()
770- self ._import_entries = data_entry .import_entries .copy ()
771- else :
772- variables = await self .get_resolvable_variables ()
773-
774- await self ._import_default_libraries (variables )
775- await self ._import_imports (
776- imports , str (Path (self .source ).parent ), top_level = True , variables = variables
777- )
743+ imports = await self .get_imports ()
778744
745+ data_entry : Optional [Namespace .DataEntry ] = None
779746 if self .document is not None :
780- self .document .set_data (
781- Namespace .DataEntry ,
782- Namespace .DataEntry (
783- self ._libraries .copy (),
784- self ._resources .copy (),
785- self ._variables .copy (),
786- self ._diagnostics .copy (),
787- self ._import_entries .copy (),
788- ),
747+ # check or save several data in documents data cache,
748+ # if imports are different, then the data is invalid
749+ old_imports : List [Import ] = self .document .get_data (Namespace )
750+ if old_imports is None :
751+ self .document .set_data (Namespace , imports )
752+ elif old_imports != imports :
753+ new_imports = []
754+ for e in old_imports :
755+ if e in imports :
756+ new_imports .append (e )
757+ for e in imports :
758+ if e not in new_imports :
759+ new_imports .append (e )
760+ self .document .set_data (Namespace , new_imports )
761+ self .document .set_data (Namespace .DataEntry , None )
762+ else :
763+ data_entry = self .document .get_data (Namespace .DataEntry )
764+
765+ if data_entry is not None :
766+ self ._libraries = data_entry .libraries .copy ()
767+ self ._resources = data_entry .resources .copy ()
768+ self ._variables = data_entry .variables .copy ()
769+ self ._diagnostics = data_entry .diagnostics .copy ()
770+ self ._import_entries = data_entry .import_entries .copy ()
771+ else :
772+ variables = await self .get_resolvable_variables ()
773+
774+ await self ._import_default_libraries (variables )
775+ await self ._import_imports (
776+ imports , str (Path (self .source ).parent ), top_level = True , variables = variables
789777 )
790778
791- await self ._reset_global_variables ()
779+ if self .document is not None :
780+ self .document .set_data (
781+ Namespace .DataEntry ,
782+ Namespace .DataEntry (
783+ self ._libraries .copy (),
784+ self ._resources .copy (),
785+ self ._variables .copy (),
786+ self ._diagnostics .copy (),
787+ self ._import_entries .copy (),
788+ ),
789+ )
792790
793- self ._initialized = True
791+ await self ._reset_global_variables ()
792+
793+ self ._initialized = True
794+ except BaseException :
795+ await self .invalidate ()
796+ raise
794797
795798 return self ._initialized
796799
0 commit comments