Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

languages: Fix Go test task cwd on Windows (#65722)

giocldc18e71c78281
diff --git a/crates/editor/src/code_lens.rs b/crates/editor/src/code_lens.rs
index 21c28e45fc3..c1e42961bd4 100644
--- a/crates/editor/src/code_lens.rs
+++ b/crates/editor/src/code_lens.rs
@@ -71,12 +71,17 @@ pub(super) fn try_handle_client_command(
let arguments = command.arguments.as_deref().unwrap_or_default();
let project = workspace.read(cx).project().clone();
+ let path_style = workspace.read(cx).path_style(cx);
let client_command = project
.read(cx)
.lsp_store()
.read(cx)
.language_server_adapter_for_id(action.server_id)
- .and_then(|adapter| adapter.adapter.client_command(&command.command, arguments))
+ .and_then(|adapter| {
+ adapter
+ .adapter
+ .client_command(&command.command, arguments, path_style)
+ })
.or_else(|| match command.command.as_str() {
"editor.action.showReferences"
| "editor.action.goToLocations"
diff --git a/crates/language/src/language.rs b/crates/language/src/language.rs
index 1ceb096fa1a..951d895a982 100644
--- a/crates/language/src/language.rs
+++ b/crates/language/src/language.rs
@@ -101,6 +101,7 @@ pub use toolchain::{
ToolchainMetadata, ToolchainScope,
};
use tree_sitter::{self, QueryCursor, WasmStore, wasmtime};
+use util::paths::PathStyle;
use util::rel_path::RelPath;
pub use available_languages::AvailableLanguage;
@@ -688,6 +689,7 @@ pub trait LspAdapter: 'static + Send + Sync + DynLspInstaller {
&self,
_command_name: &str,
_arguments: &[serde_json::Value],
+ _path_style: PathStyle,
) -> Option<ClientCommand> {
None
}
diff --git a/crates/languages/src/go.rs b/crates/languages/src/go.rs
index 91f081f73b5..0cf9bf42f71 100644
--- a/crates/languages/src/go.rs
+++ b/crates/languages/src/go.rs
@@ -30,7 +30,12 @@ use std::{
},
};
use task::{TaskTemplate, TaskTemplates, TaskVariables, VariableName};
-use util::{ResultExt, fs::remove_matching, maybe, merge_json_value_into};
+use util::{
+ ResultExt,
+ fs::remove_matching,
+ maybe, merge_json_value_into,
+ paths::{PathStyle, UrlExt as _},
+};
pub(crate) fn semantic_token_rules() -> SemanticTokenRules {
let content = grammars::get_file("go/semantic_token_rules.json")
@@ -452,9 +457,10 @@ impl LspAdapter for GoLspAdapter {
&self,
command_name: &str,
arguments: &[serde_json::Value],
+ path_style: PathStyle,
) -> Option<ClientCommand> {
if let "gopls.run_tests" = command_name {
- let template = go_test_task_template(arguments.first()?)?;
+ let template = go_test_task_template(arguments.first()?, path_style)?;
Some(ClientCommand::ScheduleTask(template))
} else {
None
@@ -480,7 +486,10 @@ fn json_string_array(value: &serde_json::Value, key: &str) -> Vec<String> {
.unwrap_or_default()
}
-fn go_test_task_template(arg: &serde_json::Value) -> Option<task::TaskTemplate> {
+fn go_test_task_template(
+ arg: &serde_json::Value,
+ path_style: PathStyle,
+) -> Option<task::TaskTemplate> {
let tests = json_string_array(arg, "Tests");
let benchmarks = json_string_array(arg, "Benchmarks");
if tests.is_empty() && benchmarks.is_empty() {
@@ -523,9 +532,10 @@ fn go_test_task_template(arg: &serde_json::Value) -> Option<task::TaskTemplate>
let cwd = arg
.get("URI")
.and_then(|v| v.as_str())
- .and_then(|uri| uri.strip_prefix("file://"))
- .and_then(|path| std::path::Path::new(path).parent())
- .map(|p| p.to_string_lossy().into_owned());
+ .and_then(|uri| uri.parse::<lsp::Uri>().ok())
+ .and_then(|uri| uri.to_file_path_ext(path_style).ok())
+ .and_then(|path| path_style.parent(&path).map(|parent| parent.to_owned()))
+ .map(|path| path.to_string_lossy().into_owned());
Some(task::TaskTemplate {
label,
@@ -963,6 +973,38 @@ mod tests {
)
}
+ #[test]
+ fn test_go_test_task_template_cwd() {
+ for (path_style, uri, expected_cwd) in [
+ (
+ PathStyle::Windows,
+ "file:///C:/my%20repo/main_test.go",
+ r"C:\my repo",
+ ),
+ (
+ PathStyle::Windows,
+ "file://server/share/pkg/main_test.go",
+ r"\\server\share\pkg",
+ ),
+ (PathStyle::Windows, "file:///C:/main_test.go", r"C:\"),
+ (PathStyle::Unix, "file:///C:/repo/main_test.go", "/C:/repo"),
+ (
+ PathStyle::Unix,
+ "file:///my%20repo/main_test.go",
+ "/my repo",
+ ),
+ ] {
+ let template =
+ go_test_task_template(&json!({ "URI": uri, "Tests": ["TestExample"] }), path_style)
+ .expect("test task should be generated");
+ assert_eq!(
+ template.cwd.as_deref(),
+ Some(expected_cwd),
+ "{path_style:?}: {uri}"
+ );
+ }
+ }
+
#[gpui::test]
async fn test_go_label_for_completion() {
let adapter = Arc::new(GoLspAdapter);
diff --git a/crates/languages/src/rust.rs b/crates/languages/src/rust.rs
index 57865f3df42..7d3b42e760d 100644
--- a/crates/languages/src/rust.rs
+++ b/crates/languages/src/rust.rs
@@ -30,6 +30,7 @@ use task::{TaskTemplate, TaskTemplates, TaskVariables, VariableName};
use util::command::{Stdio, new_command};
use util::fs::{make_file_executable, remove_matching};
use util::merge_json_value_into;
+use util::paths::PathStyle;
use util::rel_path::RelPath;
use util::{ResultExt, maybe};
@@ -731,6 +732,7 @@ impl LspAdapter for RustLspAdapter {
&self,
command_name: &str,
arguments: &[serde_json::Value],
+ _path_style: PathStyle,
) -> Option<ClientCommand> {
match command_name {
"rust-analyzer.showReferences" => Some(ClientCommand::ShowLocations),