@@ -197,43 +197,73 @@ private void AnalyzeDocument(AnalysisModuleKey key, PythonAnalyzerEntry entry, I
197
197
_analysisCompleteEvent . Reset ( ) ;
198
198
_log ? . Log ( TraceEventType . Verbose , $ "Analysis of { entry . Module . Name } ({ entry . Module . ModuleType } ) queued") ;
199
199
200
- var snapshot = _dependencyResolver . NotifyChanges ( key , entry , dependencies ) ;
201
-
200
+ var graphVersion = _dependencyResolver . ChangeValue ( key , entry , dependencies ) ;
201
+
202
202
lock ( _syncObj ) {
203
- if ( _version > snapshot . Version ) {
203
+ if ( _version > graphVersion ) {
204
204
return ;
205
205
}
206
206
207
- _version = snapshot . Version ;
207
+ _version = graphVersion ;
208
+ _currentSession ? . Cancel ( ) ;
208
209
}
209
210
210
- if ( snapshot . MissingKeys . Count > 0 ) {
211
- LoadMissingDocuments ( entry . Module . Interpreter , snapshot . MissingKeys ) ;
212
- }
211
+ UpdateDependentEntriesDepth ( entry , dependencies , graphVersion ) ;
213
212
214
- if ( TryCreateSession ( snapshot , entry , cancellationToken , out var session ) ) {
213
+ if ( TryCreateSession ( graphVersion , entry , cancellationToken , out var session ) ) {
215
214
session . Start ( true ) ;
216
215
}
217
216
}
218
217
219
- private bool TryCreateSession ( DependencyGraphSnapshot < AnalysisModuleKey , PythonAnalyzerEntry > snapshot , PythonAnalyzerEntry entry , CancellationToken cancellationToken , out PythonAnalyzerSession session ) {
218
+ private void UpdateDependentEntriesDepth ( PythonAnalyzerEntry entry , ImmutableArray < AnalysisModuleKey > dependentKeys , int graphVersion ) {
219
+ if ( dependentKeys . Count == 0 ) {
220
+ return ;
221
+ }
222
+
223
+ var dependentEntries = new List < PythonAnalyzerEntry > ( ) ;
224
+ lock ( _syncObj ) {
225
+ if ( _version > graphVersion ) {
226
+ return ;
227
+ }
228
+
229
+ foreach ( var key in dependentKeys ) {
230
+ if ( _analysisEntries . TryGetValue ( key , out var value ) ) {
231
+ dependentEntries . Add ( value ) ;
232
+ }
233
+ }
234
+ }
235
+
236
+ if ( dependentEntries . Count == 0 ) {
237
+ return ;
238
+ }
239
+
240
+ var depth = entry . Depth ;
241
+ foreach ( var dependentEntry in dependentEntries ) {
242
+ dependentEntry . SetDepth ( graphVersion , depth ) ;
243
+ }
244
+ }
245
+
246
+ private bool TryCreateSession ( int graphVersion , PythonAnalyzerEntry entry , CancellationToken cancellationToken , out PythonAnalyzerSession session ) {
220
247
var analyzeUserModuleOutOfOrder = false ;
221
248
lock ( _syncObj ) {
222
249
if ( _currentSession != null ) {
223
- if ( _currentSession . Version > snapshot . Version || _nextSession != null && _nextSession . Version > snapshot . Version ) {
250
+ if ( _currentSession . Version > graphVersion || _nextSession != null && _nextSession . Version > graphVersion ) {
224
251
session = null ;
225
252
return false ;
226
253
}
227
254
228
255
analyzeUserModuleOutOfOrder = ! _currentSession . IsCompleted && entry . IsUserModule && _currentSession . AffectedEntriesCount >= _maxTaskRunning ;
229
- if ( _version > snapshot . Version && analyzeUserModuleOutOfOrder ) {
256
+ if ( _version > graphVersion && analyzeUserModuleOutOfOrder ) {
230
257
session = CreateSession ( null , entry , cancellationToken ) ;
231
258
return true ;
232
259
}
233
260
}
234
261
}
235
262
236
- if ( ! _dependencyResolver . TryCreateWalker ( snapshot . Version , out var walker ) ) {
263
+ var snapshot = _dependencyResolver . CurrentGraphSnapshot ;
264
+ LoadMissingDocuments ( entry . Module . Interpreter , snapshot . MissingKeys ) ;
265
+
266
+ if ( ! _dependencyResolver . TryCreateWalker ( snapshot , out var walker ) ) {
237
267
session = null ;
238
268
return false ;
239
269
}
@@ -254,7 +284,6 @@ private bool TryCreateSession(DependencyGraphSnapshot<AnalysisModuleKey, PythonA
254
284
return true ;
255
285
}
256
286
257
- _currentSession . Cancel ( ) ;
258
287
_nextSession = session = CreateSession ( walker , analyzeUserModuleOutOfOrder ? entry : null , cancellationToken ) ;
259
288
return analyzeUserModuleOutOfOrder ;
260
289
}
@@ -278,9 +307,37 @@ private PythonAnalyzerSession CreateSession(IDependencyChainWalker<AnalysisModul
278
307
=> new PythonAnalyzerSession ( _services , _progress , _analysisCompleteEvent , _startNextSession , _disposeToken . CancellationToken , cancellationToken , walker , _version , entry ) ;
279
308
280
309
private void LoadMissingDocuments ( IPythonInterpreter interpreter , ImmutableArray < AnalysisModuleKey > missingKeys ) {
281
- foreach ( var ( moduleName , _, isTypeshed ) in missingKeys ) {
310
+ if ( missingKeys . Count == 0 ) {
311
+ return ;
312
+ }
313
+
314
+ var foundKeys = ImmutableArray < AnalysisModuleKey > . Empty ;
315
+ foreach ( var missingKey in missingKeys ) {
316
+ lock ( _syncObj ) {
317
+ if ( _analysisEntries . TryGetValue ( missingKey , out _ ) ) {
318
+ continue ;
319
+ }
320
+ }
321
+
322
+ var ( moduleName , _, isTypeshed ) = missingKey ;
282
323
var moduleResolution = isTypeshed ? interpreter . TypeshedResolution : interpreter . ModuleResolution ;
283
- moduleResolution . GetOrLoadModule ( moduleName ) ;
324
+ var module = moduleResolution . GetOrLoadModule ( moduleName ) ;
325
+ if ( module != null && module . ModuleType != ModuleType . Unresolved ) {
326
+ foundKeys = foundKeys . Add ( missingKey ) ;
327
+ }
328
+ }
329
+
330
+ if ( foundKeys . Count > 0 ) {
331
+ foreach ( var foundKey in foundKeys ) {
332
+ PythonAnalyzerEntry entry ;
333
+ lock ( _syncObj ) {
334
+ if ( ! _analysisEntries . TryGetValue ( foundKey , out entry ) ) {
335
+ continue ;
336
+ }
337
+ }
338
+
339
+ _dependencyResolver . TryAddValue ( foundKey , entry , ImmutableArray < AnalysisModuleKey > . Empty ) ;
340
+ }
284
341
}
285
342
}
286
343
}
0 commit comments