Skip to content

Ensure all messages are JSON rpc payloads #23362

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 1 commit into from
May 7, 2024
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
2 changes: 1 addition & 1 deletion native_locator/src/common_python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn get_env_path(path: &str) -> Option<String> {
fn report_path_python(dispatcher: &mut impl messaging::MessageDispatcher, path: &str) {
let version = utils::get_version(path);
let env_path = get_env_path(path);
dispatcher.send_message(messaging::PythonEnvironment::new(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The send_message is too generic.
Here we're sending PythonEnvironment directly, when instead we should be sending the PythonEnvironmentMessage

Added specific methods to avoid this issue (else the messages sent in the wire wasn't json rpc)

dispatcher.report_environment(messaging::PythonEnvironment::new(
"Python".to_string(),
vec![path.to_string()],
messaging::PythonEnvironmentCategory::System,
Expand Down
6 changes: 2 additions & 4 deletions native_locator/src/conda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,7 @@ pub fn find_and_report(
Some(conda_binary) => {
let params =
messaging::EnvManager::new(vec![conda_binary.to_string_lossy().to_string()], None);
let message = messaging::EnvManagerMessage::new(params);
dispatcher.send_message(message);
dispatcher.report_environment_manager(params);

let envs = get_distinct_conda_envs(conda_binary.to_path_buf(), environment);
for env in envs {
Expand Down Expand Up @@ -406,8 +405,7 @@ pub fn find_and_report(
Some(env_path.clone()),
Some(env_path),
);
let message = messaging::PythonEnvironmentMessage::new(params);
dispatcher.send_message(message);
dispatcher.report_environment(params);
}
}
None => (),
Expand Down
2 changes: 1 addition & 1 deletion native_locator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ fn main() {
}
}

dispatcher.send_message(messaging::ExitMessage::new());
dispatcher.exit();
}
37 changes: 24 additions & 13 deletions native_locator/src/messaging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use crate::logging::{LogLevel, LogMessage};
use serde::{Deserialize, Serialize};

pub trait MessageDispatcher {
fn send_message<T: serde::Serialize>(&mut self, message: T) -> ();
fn report_environment_manager(&mut self, env: EnvManager) -> ();
fn report_environment(&mut self, env: PythonEnvironment) -> ();
fn exit(&mut self) -> ();
fn log_debug(&mut self, message: &str) -> ();
fn log_info(&mut self, message: &str) -> ();
fn log_warning(&mut self, message: &str) -> ();
Expand Down Expand Up @@ -84,7 +86,7 @@ impl PythonEnvironment {
version,
activated_run,
env_path,
sys_prefix_path
sys_prefix_path,
}
}
}
Expand Down Expand Up @@ -126,26 +128,35 @@ impl ExitMessage {
}

pub struct JsonRpcDispatcher {}
fn send_message<T: serde::Serialize>(message: T) -> () {
let message = serde_json::to_string(&message).unwrap();
print!(
"Content-Length: {}\r\nContent-Type: application/vscode-jsonrpc; charset=utf-8\r\n\r\n{}",
message.len(),
message
);
}
impl MessageDispatcher for JsonRpcDispatcher {
fn send_message<T: serde::Serialize>(&mut self, message: T) -> () {
let message = serde_json::to_string(&message).unwrap();
print!(
"Content-Length: {}\r\nContent-Type: application/vscode-jsonrpc; charset=utf-8\r\n\r\n{}",
message.len(),
message
);
fn report_environment_manager(&mut self, env: EnvManager) -> () {
send_message(EnvManagerMessage::new(env));
}
fn report_environment(&mut self, env: PythonEnvironment) -> () {
send_message(PythonEnvironmentMessage::new(env));
}
fn exit(&mut self) -> () {
send_message(ExitMessage::new());
}
fn log_debug(&mut self, message: &str) -> () {
self.send_message(LogMessage::new(message.to_string(), LogLevel::Debug));
send_message(LogMessage::new(message.to_string(), LogLevel::Debug));
}
fn log_error(&mut self, message: &str) -> () {
self.send_message(LogMessage::new(message.to_string(), LogLevel::Error));
send_message(LogMessage::new(message.to_string(), LogLevel::Error));
}
fn log_info(&mut self, message: &str) -> () {
self.send_message(LogMessage::new(message.to_string(), LogLevel::Info));
send_message(LogMessage::new(message.to_string(), LogLevel::Info));
}
fn log_warning(&mut self, message: &str) -> () {
self.send_message(LogMessage::new(message.to_string(), LogLevel::Warning));
send_message(LogMessage::new(message.to_string(), LogLevel::Warning));
}
}

Expand Down
6 changes: 2 additions & 4 deletions native_locator/src/pyenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ pub fn find_and_report(
let pyenv_dir = get_pyenv_dir(environment)?;

if let Some(pyenv_binary) = get_pyenv_binary(environment) {
let params = messaging::EnvManager::new(vec![pyenv_binary], None);
let message = messaging::EnvManagerMessage::new(params);
dispatcher.send_message(message);
dispatcher.report_environment_manager(messaging::EnvManager::new(vec![pyenv_binary], None));
}

let versions_dir = PathBuf::from(&pyenv_dir)
Expand All @@ -95,7 +93,7 @@ pub fn find_and_report(
if let Some(executable) = find_python_binary_path(&path) {
let version = path.file_name().unwrap().to_string_lossy().to_string();
let env_path = path.to_string_lossy().to_string();
dispatcher.send_message(messaging::PythonEnvironment::new(
dispatcher.report_environment(messaging::PythonEnvironment::new(
"Python".to_string(),
vec![executable.into_os_string().into_string().unwrap()],
messaging::PythonEnvironmentCategory::Pyenv,
Expand Down
2 changes: 1 addition & 1 deletion native_locator/src/windows_python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::path::Path;

fn report_path_python(path: &str, dispatcher: &mut impl messaging::MessageDispatcher) {
let version = utils::get_version(path);
dispatcher.send_message(messaging::PythonEnvironment::new(
dispatcher.report_environment(messaging::PythonEnvironment::new(
"Python".to_string(),
vec![path.to_string()],
messaging::PythonEnvironmentCategory::WindowsStore,
Expand Down
12 changes: 9 additions & 3 deletions native_locator/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use std::{collections::HashMap, path::PathBuf};

use python_finder::{known::Environment, messaging::MessageDispatcher};
use python_finder::{known::Environment, messaging::{EnvManager, MessageDispatcher, PythonEnvironment}};
use serde_json::Value;

#[allow(dead_code)]
Expand Down Expand Up @@ -32,8 +32,14 @@ pub trait TestMessages {
#[allow(dead_code)]
pub fn create_test_dispatcher() -> TestDispatcher {
impl MessageDispatcher for TestDispatcher {
fn send_message<T: serde::Serialize>(&mut self, message: T) -> () {
self.messages.push(serde_json::to_string(&message).unwrap());
fn report_environment_manager(&mut self, env: EnvManager) -> () {
self.messages.push(serde_json::to_string(&env).unwrap());
}
fn report_environment(&mut self, env: PythonEnvironment) -> () {
self.messages.push(serde_json::to_string(&env).unwrap());
}
fn exit(&mut self) -> () {
//
}
fn log_debug(&mut self, _message: &str) -> () {}
fn log_error(&mut self, _message: &str) -> () {}
Expand Down
8 changes: 4 additions & 4 deletions native_locator/tests/conda_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn find_conda_exe_and_empty_envs() {
conda::find_and_report(&mut dispatcher, &known);

let conda_exe = join_test_paths(&[conda_dir.clone().as_str(), "conda"]);
let expected_json = json!({"jsonrpc":"2.0","method":"envManager","params":{"executablePath":[conda_exe.clone()],"version":null}});
let expected_json = json!({"executablePath":[conda_exe.clone()],"version":null});
assert_messages(&[expected_json], &dispatcher)
}
#[test]
Expand Down Expand Up @@ -80,9 +80,9 @@ fn finds_two_conda_envs_from_txt() {
let conda_1_exe = join_test_paths(&[conda_1.clone().as_str(), "python"]);
let conda_2_exe = join_test_paths(&[conda_2.clone().as_str(), "python"]);

let expected_conda_env = json!({"jsonrpc":"2.0","method":"envManager","params":{"executablePath":[conda_exe.clone()],"version":null}});
let expected_conda_1 = json!({"jsonrpc":"2.0","method":"pythonEnvironment","params":{"name":"envs/one","pythonExecutablePath":[conda_1_exe.clone()],"category":"conda","version":"10.0.1","activatedRun":[conda_exe.clone(),"run","-n","envs/one","python"],"envPath":conda_1.clone()}});
let expected_conda_2 = json!({"jsonrpc":"2.0","method":"pythonEnvironment","params":{"name":"envs/two","pythonExecutablePath":[conda_2_exe.clone()],"category":"conda","version":null,"activatedRun":[conda_exe.clone(),"run","-n","envs/two","python"],"envPath":conda_2.clone()}});
let expected_conda_env = json!({"executablePath":[conda_exe.clone()],"version":null});
let expected_conda_1 = json!({"name":"envs/one","pythonExecutablePath":[conda_1_exe.clone()],"category":"conda","version":"10.0.1","activatedRun":[conda_exe.clone(),"run","-n","envs/one","python"],"envPath":conda_1.clone()});
let expected_conda_2 = json!({"name":"envs/two","pythonExecutablePath":[conda_2_exe.clone()],"category":"conda","version":null,"activatedRun":[conda_exe.clone(),"run","-n","envs/two","python"],"envPath":conda_2.clone()});
assert_messages(
&[expected_conda_env, expected_conda_1, expected_conda_2],
&dispatcher,
Expand Down
Loading