Skip to content

fix: tree-sitter edit_from_change crash #273

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

Merged
merged 4 commits into from
Mar 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ strum = { version = "0.27.1", features = ["derive"] }
sqlx = { version = "0.8.2", features = ["runtime-tokio", "runtime-async-std", "postgres", "json"] }
syn = "1.0.109"
termcolor = "1.4.1"
test-log = "0.2.17"
tokio = { version = "1.40.0", features = ["full"] }
tower-lsp = "0.20.0"
tracing = { version = "0.1.40", default-features = false, features = ["std"] }
Expand Down
9 changes: 4 additions & 5 deletions crates/pgt_analyse/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ version = "0.0.0"


[dependencies]
pgt_console.workspace = true
pgt_diagnostics.workspace = true
pgt_query_ext.workspace = true
pgt_schema_cache.workspace = true
rustc-hash = { workspace = true }
pgt_console.workspace = true
pgt_diagnostics.workspace = true
pgt_query_ext.workspace = true
rustc-hash = { workspace = true }

biome_deserialize = { workspace = true, optional = true }
biome_deserialize_macros = { workspace = true, optional = true }
Expand Down
1 change: 1 addition & 0 deletions crates/pgt_lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ tracing = { workspace = true, features = ["attributes"] }
[dev-dependencies]
pgt_test_utils = { workspace = true }
sqlx = { workspace = true }
test-log = { workspace = true }
tokio = { workspace = true, features = ["macros"] }
tower = { version = "0.4.13", features = ["timeout"] }

Expand Down
203 changes: 203 additions & 0 deletions crates/pgt_lsp/tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use sqlx::Executor;
use std::any::type_name;
use std::fmt::Display;
use std::time::Duration;
use test_log::test;
use tower::timeout::Timeout;
use tower::{Service, ServiceExt};
use tower_lsp::LspService;
Expand Down Expand Up @@ -558,6 +559,208 @@ async fn test_completions() -> Result<()> {
Ok(())
}

#[tokio::test]
async fn test_issue_271() -> Result<()> {
let factory = ServerFactory::default();
let mut fs = MemoryFileSystem::default();
let test_db = get_new_test_db().await;

let setup = r#"
create table public.users (
id serial primary key,
name varchar(255) not null
);
"#;

test_db
.execute(setup)
.await
.expect("Failed to setup test database");

let mut conf = PartialConfiguration::init();
conf.merge_with(PartialConfiguration {
db: Some(PartialDatabaseConfiguration {
database: Some(
test_db
.connect_options()
.get_database()
.unwrap()
.to_string(),
),
..Default::default()
}),
..Default::default()
});
fs.insert(
url!("postgrestools.jsonc").to_file_path().unwrap(),
serde_json::to_string_pretty(&conf).unwrap(),
);

let (service, client) = factory
.create_with_fs(None, DynRef::Owned(Box::new(fs)))
.into_inner();

let (stream, sink) = client.split();
let mut server = Server::new(service);

let (sender, _) = channel(CHANNEL_BUFFER_SIZE);
let reader = tokio::spawn(client_handler(stream, sink, sender));

server.initialize().await?;
server.initialized().await?;

server.load_configuration().await?;

server
.open_document("CREATE COLLATION ignore_accent_case (provider = icu, deterministic = false, locale = 'und-u-ks-level1');\n\n-- CREATE OR REPLACE FUNCTION\n-- add_one(integer)\n-- RETURNS\n-- integer\n-- AS\n-- 'add_one.so', 'add_one'\n-- LANGUAGE\n-- C \n-- STRICT;\n\n\nSELECT pwhash, FROM users;")
.await?;

server
.change_document(
3,
vec![TextDocumentContentChangeEvent {
range: Some(Range {
start: Position {
line: 13,
character: 13,
},
end: Position {
line: 13,
character: 14,
},
}),
range_length: Some(0),
text: "".to_string(),
}],
)
.await?;

server
.change_document(
1,
vec![TextDocumentContentChangeEvent {
range: Some(Range {
start: Position {
line: 13,
character: 13,
},
end: Position {
line: 13,
character: 13,
},
}),
range_length: Some(0),
text: ",".to_string(),
}],
)
.await?;

server
.change_document(
2,
vec![TextDocumentContentChangeEvent {
range: Some(Range {
start: Position {
line: 13,
character: 14,
},
end: Position {
line: 13,
character: 14,
},
}),
range_length: Some(0),
text: " ".to_string(),
}],
)
.await?;

server
.change_document(
3,
vec![TextDocumentContentChangeEvent {
range: Some(Range {
start: Position {
line: 13,
character: 15,
},
end: Position {
line: 13,
character: 15,
},
}),
range_length: Some(0),
text: "county_name".to_string(),
}],
)
.await?;

server
.change_document(
4,
vec![TextDocumentContentChangeEvent {
range: Some(Range {
start: Position {
line: 13,
character: 13,
},
end: Position {
line: 13,
character: 26,
},
}),
range_length: Some(13),
text: "".to_string(),
}],
)
.await?;

server
.change_document(
5,
vec![TextDocumentContentChangeEvent {
range: Some(Range {
start: Position {
line: 13,
character: 13,
},
end: Position {
line: 13,
character: 13,
},
}),
range_length: Some(0),
text: ",".to_string(),
}],
)
.await?;

// crashes with range end index 37 out of range for slice of length 26
let res = server
.get_completion(CompletionParams {
work_done_progress_params: WorkDoneProgressParams::default(),
partial_result_params: PartialResultParams::default(),
context: None,
text_document_position: TextDocumentPositionParams {
text_document: TextDocumentIdentifier {
uri: url!("document.sql"),
},
position: Position {
line: 13,
character: 14,
},
},
})
.await?;

assert!(res.is_some());

server.shutdown().await?;
reader.abort();

Ok(())
}

#[tokio::test]
async fn test_execute_statement() -> Result<()> {
let factory = ServerFactory::default();
Expand Down
4 changes: 2 additions & 2 deletions crates/pgt_treesitter_queries/src/queries/relations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::QueryTryFrom;
static TS_QUERY: LazyLock<tree_sitter::Query> = LazyLock::new(|| {
static QUERY_STR: &str = r#"
(relation
(object_reference
(object_reference
.
(identifier) @schema_or_table
"."?
Expand Down Expand Up @@ -38,7 +38,7 @@ impl RelationMatch<'_> {
pub fn get_table(&self, sql: &str) -> String {
self.table
.utf8_text(sql.as_bytes())
.expect("Failed to get schema from RelationMatch")
.expect("Failed to get table from RelationMatch")
.to_string()
}
}
Expand Down
Loading
Loading