History

vue: Install a global version of TypeScript if not present in the project (#14747)

This PR updates the Vue extension with support for installing and using its own copy of TypeScript if it can't find one in the project. The way we resolve `typescript` is as follows: - We check the project's `package.json` for `typescript` in either the `devDependencies` or `dependencies` - If found, we set the `typescript.tsdk` to `node_modules/typescript/lib` to use the project's copy of TypeScript - If not found, we install the latest version of `typescript` (if not already downloaded) to the extension's `package.json` and use that version for `typescript.tsdk` This should resolve instances where Vue projects that do not have an explicit `typescript` dependency—such as those using Vue with plain JavaScript—fail to load the language server due to TypeScript not being found. Release Notes: - N/A

Marshall Bowers committed 2 years ago

Commit be1ff8a1 parent 3a83fec3 files · +84 −12Browse files

Unified · Split

3 files
  1. Cargo.lock +1 −0
  2. extensions/vue/Cargo.toml +1 −0
  3. extensions/vue/src/vue.rs +82 −12
Cargo.lock+1−0
@@ -13946,6 +13946,7 @@ dependencies = [ · 13945 unmodified lines
1394613946 name = "zed_vue"
1394713947 version = "0.0.3"
1394813948 dependencies = [
13949+ "serde",
1394913950 "zed_extension_api 0.0.6",
1395013951 ]
1395113952
extensions/vue/Cargo.toml+1−0
@@ -13,4 +13,5 @@ path = "src/vue.rs" · 12 unmodified lines
1313 crate-type = ["cdylib"]
1414
1515 [dependencies]
16+serde = { version = "1.0", features = ["derive"] }
1617 zed_extension_api = "0.0.6"
extensions/vue/src/vue.rs+82−12
@@ -1,28 +1,51 @@
1+use std::collections::HashMap;
12 use std::{env, fs};
3+
4+use serde::Deserialize;
25 use zed::lsp::{Completion, CompletionKind};
36 use zed::CodeLabelSpan;
47 use zed_extension_api::{self as zed, serde_json, Result};
58
9+const SERVER_PATH: &str = "node_modules/@vue/language-server/bin/vue-language-server.js";
10+const PACKAGE_NAME: &str = "@vue/language-server";
11+
12+const TYPESCRIPT_PACKAGE_NAME: &str = "typescript";
13+
14+/// The relative path to TypeScript's SDK.
15+const TYPESCRIPT_TSDK_PATH: &str = "node_modules/typescript/lib";
16+
17+#[derive(Debug, Deserialize)]
18+#[serde(rename_all = "camelCase")]
19+struct PackageJson {
20+ #[serde(default)]
21+ dependencies: HashMap<String, String>,
22+ #[serde(default)]
23+ dev_dependencies: HashMap<String, String>,
24+}
25+
626 struct VueExtension {
727 did_find_server: bool,
28+ typescript_tsdk_path: String,
829 }
930
10−const SERVER_PATH: &str = "node_modules/@vue/language-server/bin/vue-language-server.js";
11−const PACKAGE_NAME: &str = "@vue/language-server";
12−
1331 impl VueExtension {
1432 fn server_exists(&self) -> bool {
1533 fs::metadata(SERVER_PATH).map_or(false, |stat| stat.is_file())
1634 }
1735
18− fn server_script_path(&mut self, id: &zed::LanguageServerId) -> Result<String> {
36+ fn server_script_path(
37+ &mut self,
38+ language_server_id: &zed::LanguageServerId,
39+ worktree: &zed::Worktree,
40+ ) -> Result<String> {
1941 let server_exists = self.server_exists();
2042 if self.did_find_server && server_exists {
43+ self.install_typescript_if_needed(worktree)?;
2144 return Ok(SERVER_PATH.to_string());
2245 }
2346
2447 zed::set_language_server_installation_status(
25− id,
48+ language_server_id,
2649 &zed::LanguageServerInstallationStatus::CheckingForUpdate,
2750 );
2851 // We hardcode the version to 1.8 since we do not support @vue/language-server 2.0 yet.
@@ -32,7 +55,7 @@ impl VueExtension { · 3 unmodified lines
3255 || zed::npm_package_installed_version(PACKAGE_NAME)?.as_ref() != Some(&version)
3356 {
3457 zed::set_language_server_installation_status(
35− id,
58+ language_server_id,
3659 &zed::LanguageServerInstallationStatus::Downloading,
3760 );
3861 let result = zed::npm_install_package(PACKAGE_NAME, &version);
@@ -52,24 +75,71 @@ impl VueExtension { · 13 unmodified lines
5275 }
5376 }
5477
78+ self.install_typescript_if_needed(worktree)?;
5579 self.did_find_server = true;
5680 Ok(SERVER_PATH.to_string())
5781 }
82+
83+ /// Returns whether a local copy of TypeScript exists in the worktree.
84+ fn typescript_exists_for_worktree(&self, worktree: &zed::Worktree) -> Result<bool> {
85+ let package_json = worktree.read_text_file("package.json")?;
86+ let package_json: PackageJson = serde_json::from_str(&package_json)
87+ .map_err(|err| format!("failed to parse package.json: {err}"))?;
88+
89+ let dev_dependencies = &package_json.dev_dependencies;
90+ let dependencies = &package_json.dependencies;
91+
92+ // Since the extension is not allowed to read the filesystem within the project
93+ // except through the worktree (which does not contains `node_modules`), we check
94+ // the `package.json` to see if `typescript` is listed in the dependencies.
95+ Ok(dev_dependencies.contains_key(TYPESCRIPT_PACKAGE_NAME)
96+ || dependencies.contains_key(TYPESCRIPT_PACKAGE_NAME))
97+ }
98+
99+ fn install_typescript_if_needed(&mut self, worktree: &zed::Worktree) -> Result<()> {
100+ if self
101+ .typescript_exists_for_worktree(worktree)
102+ .unwrap_or_default()
103+ {
104+ println!("found local TypeScript installation at '{TYPESCRIPT_TSDK_PATH}'");
105+ return Ok(());
106+ }
107+
108+ let installed_typescript_version =
109+ zed::npm_package_installed_version(TYPESCRIPT_PACKAGE_NAME)?;
110+ let latest_typescript_version = zed::npm_package_latest_version(TYPESCRIPT_PACKAGE_NAME)?;
111+
112+ if installed_typescript_version.as_ref() != Some(&latest_typescript_version) {
113+ println!("installing {TYPESCRIPT_PACKAGE_NAME}@{latest_typescript_version}");
114+ zed::npm_install_package(TYPESCRIPT_PACKAGE_NAME, &latest_typescript_version)?;
115+ } else {
116+ println!("typescript already installed");
117+ }
118+
119+ self.typescript_tsdk_path = env::current_dir()
120+ .unwrap()
121+ .join(TYPESCRIPT_TSDK_PATH)
122+ .to_string_lossy()
123+ .to_string();
124+
125+ Ok(())
126+ }
58127 }
59128
60129 impl zed::Extension for VueExtension {
61130 fn new() -> Self {
62131 Self {
63132 did_find_server: false,
133+ typescript_tsdk_path: TYPESCRIPT_TSDK_PATH.to_owned(),
64134 }
65135 }
66136
67137 fn language_server_command(
68138 &mut self,
69− id: &zed::LanguageServerId,
70− _: &zed::Worktree,
139+ language_server_id: &zed::LanguageServerId,
140+ worktree: &zed::Worktree,
71141 ) -> Result<zed::Command> {
72− let server_path = self.server_script_path(id)?;
142+ let server_path = self.server_script_path(language_server_id, worktree)?;
73143 Ok(zed::Command {
74144 command: zed::node_binary_path()?,
75145 args: vec![
@@ -86,12 +156,12 @@ impl zed::Extension for VueExtension { · 10 unmodified lines
86156
87157 fn language_server_initialization_options(
88158 &mut self,
89− _: &zed::LanguageServerId,
90− _: &zed::Worktree,
159+ _language_server_id: &zed::LanguageServerId,
160+ _worktree: &zed::Worktree,
91161 ) -> Result<Option<serde_json::Value>> {
92162 Ok(Some(serde_json::json!({
93163 "typescript": {
94− "tsdk": "node_modules/typescript/lib"
164+ "tsdk": self.typescript_tsdk_path
95165 }
96166 })))
97167 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t