-
Notifications
You must be signed in to change notification settings - Fork 161
Add support for TSP snapshot request #1194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f3ba415
Add support for snapshot request
rchiodo 691c655
Fix formatting
rchiodo 93facc5
Fix clippy errors
rchiodo 834c5be
Don't use epoch anymore and add test for doc change
rchiodo 760d871
Add didChangewatchedFiles and handle didChange event too
rchiodo a7e7700
Eliminate double snapshot increment
rchiodo ef74aae
Support async events and write test to verify
rchiodo 9f4cfcf
Fix formatting
rchiodo 73c5e77
Fix clippy errors
rchiodo 3c42cb7
Fix formatting
rchiodo 58b2a00
Merge remote-tracking branch 'upstream/main' into rchiodo/add_tsp_sna…
rchiodo 173f099
Put back configuration changes.
rchiodo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
//! Tests for TSP getSnapshot request | ||
|
||
use lsp_server::RequestId; | ||
use lsp_server::Response; | ||
use tempfile::TempDir; | ||
|
||
use crate::test::tsp::tsp_interaction::object_model::TspInteraction; | ||
|
||
#[test] | ||
fn test_tsp_get_snapshot() { | ||
// Test retrieval of TSP snapshot version | ||
let temp_dir = TempDir::new().unwrap(); | ||
let test_file_path = temp_dir.path().join("test.py"); | ||
|
||
let test_content = r#"# Simple test file | ||
print("Hello, World!") | ||
"#; | ||
|
||
std::fs::write(&test_file_path, test_content).unwrap(); | ||
|
||
// Create a pyproject.toml to make this a recognized Python project | ||
let pyproject_content = r#"[build-system] | ||
requires = ["setuptools>=45", "setuptools-scm[toml]>=6.2"] | ||
build-backend = "setuptools.build_meta" | ||
|
||
[project] | ||
name = "test-project" | ||
version = "1.0.0" | ||
"#; | ||
std::fs::write(temp_dir.path().join("pyproject.toml"), pyproject_content).unwrap(); | ||
|
||
let mut tsp = TspInteraction::new(); | ||
tsp.set_root(temp_dir.path().to_path_buf()); | ||
tsp.initialize(Default::default()); | ||
|
||
// Open the test file | ||
tsp.server.did_open("test.py"); | ||
|
||
// Wait for any diagnostics/RecheckFinished events | ||
tsp.client.expect_any_message(); | ||
|
||
// Get snapshot | ||
tsp.server.get_snapshot(); | ||
|
||
// Expect snapshot response with integer (should increment after RecheckFinished from indexing) | ||
tsp.client.expect_response(Response { | ||
id: RequestId::from(2), | ||
result: Some(serde_json::json!(1)), | ||
error: None, | ||
}); | ||
|
||
tsp.shutdown(); | ||
} | ||
|
||
#[test] | ||
fn test_tsp_snapshot_updates_on_file_change() { | ||
rchiodo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Test that DidChangeWatchedFiles events trigger async recheck and update snapshots | ||
// With the recheck queue thread running, async tasks should execute and generate RecheckFinished events | ||
let temp_dir = TempDir::new().unwrap(); | ||
let test_file_path = temp_dir.path().join("changing_test.py"); | ||
|
||
let initial_content = r#"# Initial content | ||
x = 1 | ||
"#; | ||
|
||
std::fs::write(&test_file_path, initial_content).unwrap(); | ||
|
||
// Create a pyproject.toml to make this a recognized Python project | ||
let pyproject_content = r#"[build-system] | ||
requires = ["setuptools>=45", "setuptools-scm[toml]>=6.2"] | ||
build-backend = "setuptools.build_meta" | ||
|
||
[project] | ||
name = "test-project" | ||
version = "1.0.0" | ||
"#; | ||
std::fs::write(temp_dir.path().join("pyproject.toml"), pyproject_content).unwrap(); | ||
|
||
let mut tsp = TspInteraction::new(); | ||
tsp.set_root(temp_dir.path().to_path_buf()); | ||
tsp.initialize(Default::default()); | ||
|
||
// Open the test file | ||
tsp.server.did_open("changing_test.py"); | ||
|
||
// Wait for any diagnostics/RecheckFinished events | ||
tsp.client.expect_any_message(); | ||
|
||
// Get initial snapshot | ||
tsp.server.get_snapshot(); | ||
|
||
// Expect first snapshot response | ||
tsp.client.expect_response(Response { | ||
id: RequestId::from(2), | ||
result: Some(serde_json::json!(1)), | ||
error: None, | ||
}); | ||
|
||
// Modify the file to trigger a state change | ||
let updated_content = r#"# Updated content | ||
x = 2 | ||
y = "hello" | ||
"#; | ||
|
||
std::fs::write(&test_file_path, updated_content).unwrap(); | ||
|
||
// Simulate the LSP DidChangeWatchedFiles notification for the file change | ||
tsp.server | ||
.did_change_watched_files("changing_test.py", "changed"); | ||
|
||
// Wait for the async RecheckFinished event to be processed | ||
tsp.client.expect_any_message(); | ||
|
||
// Get snapshot after async recheck completes | ||
tsp.server.get_snapshot(); | ||
|
||
// Expect snapshot to be incremented to 2 after RecheckFinished from file change | ||
tsp.client.expect_response(Response { | ||
id: RequestId::from(3), | ||
result: Some(serde_json::json!(2)), // Should be 2 after RecheckFinished from file change | ||
error: None, | ||
}); | ||
|
||
tsp.shutdown(); | ||
} | ||
|
||
#[test] | ||
fn test_tsp_snapshot_updates_on_did_change() { | ||
// Test that didChange events cause snapshot to update | ||
let temp_dir = TempDir::new().unwrap(); | ||
let test_file_path = temp_dir.path().join("change_test.py"); | ||
|
||
let initial_content = r#"# Initial content | ||
x = 1 | ||
"#; | ||
|
||
std::fs::write(&test_file_path, initial_content).unwrap(); | ||
|
||
// Create a pyproject.toml to make this a recognized Python project | ||
let pyproject_content = r#"[build-system] | ||
requires = ["setuptools>=45", "setuptools-scm[toml]>=6.2"] | ||
build-backend = "setuptools.build_meta" | ||
|
||
[project] | ||
name = "test-project" | ||
version = "1.0.0" | ||
"#; | ||
std::fs::write(temp_dir.path().join("pyproject.toml"), pyproject_content).unwrap(); | ||
|
||
let mut tsp = TspInteraction::new(); | ||
tsp.set_root(temp_dir.path().to_path_buf()); | ||
tsp.initialize(Default::default()); | ||
|
||
// Open the test file | ||
tsp.server.did_open("change_test.py"); | ||
|
||
// Wait for any diagnostics/RecheckFinished events from opening | ||
tsp.client.expect_any_message(); | ||
|
||
// Get initial snapshot | ||
tsp.server.get_snapshot(); | ||
|
||
// Expect first snapshot response | ||
tsp.client.expect_response(Response { | ||
id: RequestId::from(2), | ||
result: Some(serde_json::json!(1)), | ||
error: None, | ||
}); | ||
|
||
// Send a didChange notification with updated content | ||
let changed_content = r#"# Changed content | ||
x = 2 | ||
y = 'updated' | ||
"#; | ||
tsp.server.did_change("change_test.py", changed_content, 2); | ||
|
||
// Wait for any RecheckFinished events triggered by the change | ||
tsp.client.expect_any_message(); | ||
|
||
// Get updated snapshot | ||
tsp.server.get_snapshot(); | ||
|
||
// Expect second snapshot response - should be incremented due to didChange | ||
tsp.client.expect_response(Response { | ||
id: RequestId::from(3), | ||
result: Some(serde_json::json!(2)), | ||
error: None, | ||
}); | ||
|
||
tsp.shutdown(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
//! Implementation of the getSnapshot TSP request | ||
|
||
use crate::tsp::server::TspServer; | ||
|
||
impl TspServer { | ||
/// Get the current snapshot version | ||
/// | ||
/// The snapshot represents the current epoch of the global state. | ||
/// It changes whenever files are modified, configuration changes, | ||
/// or any other event that would trigger a recomputation. | ||
pub fn get_snapshot(&self) -> i32 { | ||
*self.current_snapshot.lock().unwrap_or_else(|poisoned| { | ||
// In case of poisoned mutex, recover and return the value | ||
eprintln!("TSP: Warning - snapshot mutex was poisoned, recovering"); | ||
poisoned.into_inner() | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ | |
|
||
//! TSP request implementations | ||
|
||
pub mod get_snapshot; | ||
pub mod get_supported_protocol_version; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.