77import de .peeeq .wurstscript .ast .WImport ;
88import de .peeeq .wurstscript .ast .WImports ;
99import de .peeeq .wurstscript .ast .WPackage ;
10+ import de .peeeq .wurstscript .ast .WurstModel ;
1011
1112import java .util .*;
1213
@@ -19,7 +20,8 @@ public static ImmutableList<WPackage> initDependencies(WPackage p) {
1920 packages .addAll (p .attrImportedPackagesTransitive ());
2021
2122 // add config package if it exists:
22- WPackage configPackage = p .getModel ().attrConfigOverridePackages ().get (p );
23+ WurstModel model = safeModel (p );
24+ WPackage configPackage = model == null ? null : model .attrConfigOverridePackages ().get (p );
2325 if (configPackage != null ) {
2426 packages .add (configPackage );
2527 }
@@ -70,8 +72,11 @@ private static String getCyclicDependencyString(List<WPackage> callStack, WPacka
7072 StringBuilder msg = new StringBuilder ();
7173 Map <WPackage , WPackage > configuredPackage = new HashMap <>();
7274 if (considerConfig ) {
73- for (WPackage configured : imported .getModel ().attrConfigOverridePackages ().keySet ()) {
74- configuredPackage .put (imported .getModel ().attrConfigOverridePackages ().get (configured ), configured );
75+ WurstModel model = safeModel (imported );
76+ if (model != null ) {
77+ for (WPackage configured : model .attrConfigOverridePackages ().keySet ()) {
78+ configuredPackage .put (model .attrConfigOverridePackages ().get (configured ), configured );
79+ }
7580 }
7681 }
7782 for (WPackage p : callStack ) {
@@ -152,7 +157,8 @@ private static void addCollectImportedPackage(List<WPackage> callStack, WPackage
152157 // add imports of configured package to config package
153158 // that way cyclic dependencies are checked for the config package and errors will be reported for the config package
154159 if (considerConfig ) {
155- WPackage configPackage = p .getModel ().attrConfigOverridePackages ().get (imported );
160+ WurstModel model = safeModel (p );
161+ WPackage configPackage = model == null ? null : model .attrConfigOverridePackages ().get (imported );
156162 if (configPackage != null && configPackage != p ) {
157163 if (configPackage == callStack .get (0 )) {
158164 reportCyclicDependency (callStack , configPackage , reportedErrors , useWarnings );
@@ -168,6 +174,10 @@ private static void addCollectImportedPackage(List<WPackage> callStack, WPackage
168174 }
169175
170176 private static void collectImportedPackages (List <WPackage > callStack , WPackage p , Collection <WPackage > result , Set <String > reportedErrors , boolean considerConfig , boolean useWarnings ) {
177+ if (safeModel (p ) == null ) {
178+ // Detached packages can occur transiently in language-server workflows; ignore them for init-order analysis.
179+ return ;
180+ }
171181 callStack .add (p );
172182 addCollectImportedPackage (callStack , p , result , p .getImports (), reportedErrors , considerConfig , useWarnings );
173183 /*
@@ -178,7 +188,8 @@ private static void collectImportedPackages(List<WPackage> callStack, WPackage p
178188 even though the configured package will be initialized after the config package.
179189 */
180190 if (considerConfig ) {
181- for (Map .Entry <WPackage , WPackage > e : p .getModel ().attrConfigOverridePackages ().entrySet ()) {
191+ WurstModel model = safeModel (p );
192+ if (model != null ) for (Map .Entry <WPackage , WPackage > e : model .attrConfigOverridePackages ().entrySet ()) {
182193 if (e .getValue ().equals (p )) {
183194 addCollectImportedPackage (callStack , e .getKey (), result , e .getKey ().getImports (), reportedErrors , considerConfig , useWarnings );
184195 }
@@ -187,4 +198,12 @@ private static void collectImportedPackages(List<WPackage> callStack, WPackage p
187198 callStack .remove (callStack .size () - 1 );
188199 }
189200
201+ private static WurstModel safeModel (WPackage p ) {
202+ try {
203+ return p .getModel ();
204+ } catch (Error ignored ) {
205+ return null ;
206+ }
207+ }
208+
190209}
0 commit comments