diff --git a/crates/oxc_linter/src/service/runtime.rs b/crates/oxc_linter/src/service/runtime.rs index a0c17efaca7c0..27e1b80ac44a9 100644 --- a/crates/oxc_linter/src/service/runtime.rs +++ b/crates/oxc_linter/src/service/runtime.rs @@ -733,6 +733,15 @@ impl Runtime { // } // } + // Clear resolver cache to prevent memory leaks in language server usage. + // The resolver caches filesystem metadata globally, which can accumulate + // indefinitely when processing files repeatedly in a language server context. + // Clearing after each run prevents unbounded memory growth while still + // allowing caching within a single linting session. + if let Some(resolver) = &self.resolver { + resolver.clear_cache(); + } + messages.into_inner().unwrap() } diff --git a/crates/oxc_linter/tests/integration_test.rs b/crates/oxc_linter/tests/integration_test.rs index 56bc2e1f7b0c1..883460af20f49 100644 --- a/crates/oxc_linter/tests/integration_test.rs +++ b/crates/oxc_linter/tests/integration_test.rs @@ -39,3 +39,27 @@ fn test_declare_oxc_lint() { // plugin name is passed to const assert_eq!(TestRule::PLUGIN, "eslint"); } + +/// Test to ensure resolver cache clearing doesn't panic when called +#[test] +fn test_resolver_cache_clearing_integration() { + // This test verifies that the resolver cache clearing functionality + // compiles and can be called without panicking. + // The actual memory leak fix is in Runtime::run_source() method. + + use oxc_resolver::{ResolveOptions, Resolver}; + + // Create a resolver similar to how it's done in Runtime::get_resolver + let resolver = Resolver::new(ResolveOptions { + extensions: vec![".js".into(), ".ts".into(), ".jsx".into(), ".tsx".into()], + main_fields: vec!["module".into(), "main".into()], + condition_names: vec!["module".into(), "import".into()], + ..ResolveOptions::default() + }); + + // Test that clear_cache can be called without panicking + resolver.clear_cache(); + + // If we reach here, the clear_cache method works correctly + println!("Resolver cache clearing integration test passed"); +}