@@ -475,6 +475,7 @@ def test_changed_file_only_indexes_ruby
475475 path = File . join ( Dir . pwd , "lib" , "foo.rb" )
476476 File . write ( path , "class Foo\n end" )
477477
478+ @server . global_state . index . index_all ( uris : [ ] )
478479 @server . global_state . index . expects ( :index_single ) . once . with do |uri |
479480 uri . full_path == path
480481 end
@@ -501,6 +502,7 @@ def test_changed_file_only_indexes_ruby
501502 end
502503
503504 def test_did_change_watched_files_does_not_fail_for_non_existing_files
505+ @server . global_state . index . index_all ( uris : [ ] )
504506 @server . process_message ( {
505507 method : "workspace/didChangeWatchedFiles" ,
506508 params : {
@@ -530,6 +532,7 @@ def test_did_change_watched_files_handles_deletions
530532
531533 uri = URI ::Generic . from_path ( path : path )
532534
535+ @server . global_state . index . index_all ( uris : [ ] )
533536 @server . process_message ( {
534537 method : "workspace/didChangeWatchedFiles" ,
535538 params : {
@@ -585,6 +588,7 @@ def version
585588 bar . expects ( :workspace_did_change_watched_files ) . once
586589
587590 begin
591+ @server . global_state . index . index_all ( uris : [ ] )
588592 @server . process_message ( {
589593 method : "workspace/didChangeWatchedFiles" ,
590594 params : {
@@ -606,6 +610,7 @@ def version
606610 end
607611
608612 def test_did_change_watched_files_processes_unique_change_entries
613+ @server . global_state . index . index_all ( uris : [ ] )
609614 @server . expects ( :handle_rubocop_config_change ) . once
610615 @server . process_message ( {
611616 method : "workspace/didChangeWatchedFiles" ,
@@ -1155,6 +1160,7 @@ def test_rubocop_config_changes_trigger_workspace_diagnostic_refresh
11551160 } ,
11561161 } )
11571162
1163+ @server . global_state . index . index_all ( uris : [ ] )
11581164 @server . process_message ( {
11591165 method : "workspace/didChangeWatchedFiles" ,
11601166 params : {
@@ -1316,6 +1322,7 @@ class Foo
13161322 } ,
13171323 } )
13181324
1325+ @server . global_state . index . index_all ( uris : [ ] )
13191326 @server . global_state . index . expects ( :handle_change ) . never
13201327 @server . process_message ( {
13211328 method : "workspace/didChangeWatchedFiles" ,
@@ -1370,6 +1377,7 @@ class Foo
13701377 params : { textDocument : { uri : uri } } ,
13711378 } )
13721379
1380+ @server . global_state . index . index_all ( uris : [ ] )
13731381 # Then send a late did change watched files notification for the creation of the file
13741382 @server . process_message ( {
13751383 method : "workspace/didChangeWatchedFiles" ,
@@ -1414,8 +1422,59 @@ def test_diagnose_state
14141422 assert_equal ( 0 , result . response [ :incomingQueueSize ] )
14151423 end
14161424
1425+ def test_modifying_files_during_initial_indexing_does_not_duplicate_entries
1426+ path = File . join ( Dir . pwd , "lib" , "foo.rb" )
1427+ uri = URI ::Generic . from_path ( path : path )
1428+
1429+ begin
1430+ @server . process_message ( {
1431+ id : 1 ,
1432+ method : "initialize" ,
1433+ params : {
1434+ initializationOptions : { } ,
1435+ capabilities : { general : { positionEncodings : [ "utf-8" ] } , window : { workDoneProgress : true } } ,
1436+ } ,
1437+ } )
1438+
1439+ # Start indexing
1440+ File . write ( path , "class Foo\n end" )
1441+ @server . process_message ( { method : "initialized" , params : { } } )
1442+
1443+ # Then immediately notify that a file was modified before indexing is finished
1444+ File . write ( path , "class Foo\n def bar\n end\n end" )
1445+ @server . process_message ( {
1446+ method : "workspace/didChangeWatchedFiles" ,
1447+ params : {
1448+ changes : [
1449+ {
1450+ uri : uri . to_s ,
1451+ type : RubyLsp ::Constant ::FileChangeType ::CHANGED ,
1452+ } ,
1453+ ] ,
1454+ } ,
1455+ } )
1456+
1457+ wait_for_indexing
1458+
1459+ # There should not be a duplicate declaration
1460+ index = @server . global_state . index
1461+ assert_equal ( 1 , index [ "Foo" ] &.length )
1462+ ensure
1463+ FileUtils . rm ( path )
1464+ end
1465+ end
1466+
14171467 private
14181468
1469+ def wait_for_indexing
1470+ message = @server . pop_response
1471+ until message . is_a? ( RubyLsp ::Notification ) && message . method == "$/progress" &&
1472+ T . unsafe ( message ) . params . value . kind == "end"
1473+
1474+ message = @server . pop_response
1475+ end
1476+ end
1477+
14191478 def with_uninstalled_rubocop ( &block )
14201479 rubocop_paths = $LOAD_PATH. select { |path | path . include? ( "gems/rubocop" ) }
14211480 rubocop_paths . each { |path | $LOAD_PATH. delete ( path ) }
0 commit comments