Conversation
Summary of ChangesHello @CppCXY, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the EmmyLua language server by implementing support for external JSON schemas. It allows developers to define complex types using standard JSON Schema definitions, which the language server will then fetch, convert, and integrate into its type analysis. This feature improves type checking and auto-completion capabilities for projects that rely on external schema definitions, making the development experience more robust and efficient. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request adds support for the @schema tag to load JSON schemas from URLs, which is a great feature. My review focuses on improving the implementation's robustness, particularly around network requests and file handling for the generated schemas. I've suggested using a virtual URI scheme to avoid creating files in the user's workspace, adding status code checks for HTTP requests, and improving a concurrency pattern to prevent race conditions.
| if let Ok(response) = result { | ||
| if let Ok(content) = response.text().await { | ||
| url_contents.insert(url.clone(), content); | ||
| } else { | ||
| log::error!("Failed to read schema content from URL: {:?}", url); | ||
| } | ||
| } else { | ||
| log::error!("Failed to fetch schema from URL: {:?}", url); | ||
| } |
There was a problem hiding this comment.
The current implementation doesn't check the HTTP status code of the response from reqwest. This means it might try to parse an error page (e.g., for a 404 Not Found) as a JSON schema, which will fail later during conversion. It's important to check if the request was successful (i.e., has a 2xx status code) before attempting to process the response body.
if let Ok(response) = result {
if response.status().is_success() {
if let Ok(content) = response.text().await {
url_contents.insert(url.clone(), content);
} else {
log::error!("Failed to read schema content from URL: {:?}", url);
}
} else {
log::error!(
"Failed to fetch schema from URL: {:?}, status: {}",
url,
response.status()
);
}
} else {
log::error!("Failed to fetch schema from URL: {:?}", url);
}| let work_dir = std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")); | ||
| let converter = SchemaConverter::new(true); | ||
| for (url, json_content) in url_contents { | ||
| let short_name = get_schema_short_name(&url); | ||
| match converter.convert_from_str(&json_content) { | ||
| Ok(convert_result) => { | ||
| let path = work_dir.join(short_name); | ||
| let Some(file_id) = | ||
| self.update_file_by_path(&path, Some(convert_result.annotation_text)) | ||
| else { | ||
| continue; | ||
| }; |
There was a problem hiding this comment.
Using std::env::current_dir() to determine the path for generated schema files is unreliable and can lead to issues. The current working directory might not be writable, or it could be the user's project root, which would result in creating unwanted files in their workspace. A more robust approach is to use a custom URI scheme for these virtual files, ensuring they are handled in-memory without affecting the file system. This also makes file identity unique and avoids potential path collisions.
let converter = SchemaConverter::new(true);
for (url, json_content) in url_contents {
match converter.convert_from_str(&json_content) {
Ok(convert_result) => {
let Ok(uri) = Uri::parse(&format!("emmylua-schema:{}", url.as_str())) else {
log::error!("Failed to create schema URI for {}", url);
continue;
};
let Some(file_id) =
self.update_file_by_uri(&uri, Some(convert_result.annotation_text))
else {
continue;
};| let read_analysis = self.analysis.read().await; | ||
| if read_analysis.check_schema_update() { | ||
| drop(read_analysis); | ||
| let mut write_analysis = self.analysis.write().await; | ||
| write_analysis.update_schema().await; | ||
| } |
There was a problem hiding this comment.
The current pattern of dropping a read lock to acquire a write lock can introduce a race condition. Another thread could perform the schema update in the small window between the read lock being released and the write lock being acquired, leading to redundant work. To prevent this, it's a good practice to re-check the condition after acquiring the write lock.
if self.analysis.read().await.check_schema_update() {
let mut write_analysis = self.analysis.write().await;
// Re-check after acquiring write lock to avoid race condition.
if write_analysis.check_schema_update() {
write_analysis.update_schema().await;
}
}
use like: