1818
1919import org .eclipse .core .resources .IProject ;
2020import org .eclipse .core .resources .IResource ;
21+ import org .eclipse .core .runtime .NullProgressMonitor ;
22+ import org .eclipse .core .runtime .OperationCanceledException ;
23+ import org .eclipse .core .runtime .jobs .Job ;
2124import org .eclipse .jdt .core .Flags ;
2225import org .eclipse .jdt .core .ICompilationUnit ;
2326import org .eclipse .jdt .core .IJavaElement ;
2831import org .eclipse .jdt .core .IType ;
2932import org .eclipse .jdt .core .JavaModelException ;
3033import org .eclipse .jdt .ls .core .internal .JDTUtils ;
34+ import org .eclipse .jdt .ls .core .internal .handlers .DocumentLifeCycleHandler ;
3135import org .eclipse .jdt .ls .core .internal .managers .ProjectsManager ;
3236import org .eclipse .lsp4j .Range ;
3337
@@ -43,6 +47,17 @@ public static Object resolveMainMethods(List<Object> arguments) throws DebugExce
4347 return Collections .emptyList ();
4448 }
4549
50+ // When the current document is changed, the language server will receive a didChange request about the changed text and then
51+ // trigger a background job to update the change to the CompilationUnit. Because of race condition, the resolveMainMethods may read
52+ // an old CompilationUnit. So add some waiting logic to wait the Document Update to finish first.
53+ try {
54+ Job .getJobManager ().join (DocumentLifeCycleHandler .DOCUMENT_LIFE_CYCLE_JOBS , new NullProgressMonitor ());
55+ } catch (OperationCanceledException ignorable ) {
56+ // Do nothing.
57+ } catch (InterruptedException e ) {
58+ // Do nothing.
59+ }
60+
4661 String uri = (String ) arguments .get (0 );
4762 final ICompilationUnit unit = JDTUtils .resolveCompilationUnit (uri );
4863 if (unit == null || unit .getResource () == null || !unit .getResource ().exists ()) {
0 commit comments