@@ -278,6 +278,97 @@ fn test_workspace_pythonpath_ignored_when_set_in_config_file() {
278278 interaction. shutdown ( ) . expect ( "Failed to shutdown" ) ;
279279}
280280
281+ // Only run this test on unix since windows has no way to mock a .exe without compiling something
282+ // (we call python with python.exe)
283+ #[ cfg( unix) ]
284+ #[ test]
285+ fn test_interpreter_change_removes_type_errors ( ) {
286+ let test_files_root = get_test_files_root ( ) ;
287+ let good_interpreter_path =
288+ setup_dummy_interpreter ( & test_files_root. path ( ) . join ( "custom_interpreter" ) ) ;
289+ let bad_interpreter_path = setup_dummy_interpreter (
290+ & test_files_root
291+ . path ( )
292+ . join ( "interpreter_with_no_site_packages" ) ,
293+ ) ;
294+
295+ let mut interaction = LspInteraction :: new ( ) ;
296+ interaction. set_root ( test_files_root. path ( ) . to_path_buf ( ) ) ;
297+ interaction
298+ . initialize ( InitializeSettings {
299+ configuration : Some ( Some (
300+ json ! ( [ { "pyrefly" : { "displayTypeErrors" : "force-on" } } ] ) ,
301+ ) ) ,
302+ ..Default :: default ( )
303+ } )
304+ . unwrap ( ) ;
305+
306+ interaction. client . did_open ( "custom_interpreter/src/foo.py" ) ;
307+ // Without any interpreter configured, there should be 1 import error
308+ interaction
309+ . client
310+ . expect_publish_diagnostics_error_count (
311+ test_files_root. path ( ) . join ( "custom_interpreter/src/foo.py" ) ,
312+ 1 ,
313+ )
314+ . unwrap ( ) ;
315+ // Configure broken interpreter with empty site-packages - should still have 1 import error
316+ interaction. client . did_change_configuration ( ) ;
317+ interaction
318+ . client
319+ . expect_request :: < WorkspaceConfiguration > ( json ! ( { "items" : [ { "section" : "python" } ] } ) )
320+ . unwrap ( )
321+ . send_configuration_response ( json ! ( [
322+ {
323+ "pythonPath" : bad_interpreter_path. to_str( ) . unwrap( )
324+ }
325+ ] ) ) ;
326+ interaction
327+ . client
328+ . expect_publish_diagnostics_error_count (
329+ test_files_root. path ( ) . join ( "custom_interpreter/src/foo.py" ) ,
330+ 1 ,
331+ )
332+ . unwrap ( ) ;
333+
334+ // Switch to good interpreter with site-packages
335+ interaction. client . did_change_configuration ( ) ;
336+ interaction
337+ . client
338+ . expect_request :: < WorkspaceConfiguration > ( json ! ( { "items" : [ { "section" : "python" } ] } ) )
339+ . unwrap ( )
340+ . send_configuration_response ( json ! ( [
341+ {
342+ "pythonPath" : good_interpreter_path. to_str( ) . unwrap( )
343+ }
344+ ] ) ) ;
345+
346+ // BUG: Expecting 1 error demonstrates incorrect behavior.
347+ // After switching to good interpreter, the error should be resolved to 0.
348+ interaction
349+ . client
350+ . expect_publish_diagnostics_error_count (
351+ test_files_root. path ( ) . join ( "custom_interpreter/src/foo.py" ) ,
352+ 1 ,
353+ )
354+ . unwrap ( ) ;
355+
356+ // BUG: it works after a did_close -> did_open
357+ interaction
358+ . client
359+ . did_close ( "custom_interpreter/src/foo.py" ) ;
360+ interaction. client . did_open ( "custom_interpreter/src/foo.py" ) ;
361+ interaction
362+ . client
363+ . expect_publish_diagnostics_error_count (
364+ test_files_root. path ( ) . join ( "custom_interpreter/src/foo.py" ) ,
365+ 0 ,
366+ )
367+ . unwrap ( ) ;
368+
369+ interaction. shutdown ( ) . unwrap ( ) ;
370+ }
371+
281372#[ test]
282373fn test_disable_language_services ( ) {
283374 let test_files_root = get_test_files_root ( ) ;
0 commit comments