vize_canon 0.29.0

Canon - The standard of correctness for Vize type checking
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! LSP Client for tsgo.
//!
//! Communicates with tsgo LSP server to perform type checking on virtual files
//! without writing them to disk.
//!
//! ## Submodules
//!
//! - `requests`: JSON-RPC request/response protocol and document management
//! - `handlers`: Notification handling, message draining, and diagnostics collection

mod handlers;
mod requests;

use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::{
    io::BufReader,
    process::{Child, ChildStdin, ChildStdout, Command, Stdio},
    sync::atomic::{AtomicI64, Ordering},
    thread,
    time::Duration,
};

#[cfg(unix)]
use std::os::unix::io::AsRawFd;
use vize_carton::cstr;
use vize_carton::FxHashMap;
use vize_carton::String;
use vize_carton::ToCompactString;

/// LSP Client for tsgo
pub struct TsgoLspClient {
    process: Child,
    pub(crate) stdin: ChildStdin,
    pub(crate) stdout: BufReader<ChildStdout>,
    pub(crate) request_id: AtomicI64,
    /// Pending diagnostics received via publishDiagnostics
    pub(crate) diagnostics: FxHashMap<String, Vec<LspDiagnostic>>,
    /// Temporary directory for tsconfig.json (cleaned up on drop)
    temp_dir: Option<std::path::PathBuf>,
}

/// LSP Diagnostic
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LspDiagnostic {
    pub range: LspRange,
    pub severity: Option<i32>,
    pub code: Option<Value>,
    pub source: Option<String>,
    pub message: String,
}

/// LSP Range
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LspRange {
    pub start: LspPosition,
    pub end: LspPosition,
}

/// LSP Position
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LspPosition {
    pub line: u32,
    pub character: u32,
}

impl TsgoLspClient {
    /// Start tsgo LSP server
    ///
    /// tsgo path resolution order:
    /// 1. Explicit tsgo_path argument
    /// 2. TSGO_PATH environment variable
    /// 3. Local node_modules (relative to working_dir or cwd)
    /// 4. Common npm global install locations
    /// 5. "tsgo" in PATH
    pub fn new(tsgo_path: Option<&str>, working_dir: Option<&str>) -> Result<Self, String> {
        let tsgo: String = tsgo_path
            .map(String::from)
            .or_else(|| std::env::var("TSGO_PATH").ok().map(String::from))
            .or_else(|| Self::find_tsgo_in_local_node_modules(working_dir))
            .or_else(Self::find_tsgo_in_common_locations)
            .unwrap_or_else(|| "tsgo".into());

        eprintln!("\x1b[90m[tsgo] Using: {tsgo}\x1b[0m");

        // Determine project root (for node_modules resolution)
        let project_root = working_dir
            .map(std::path::PathBuf::from)
            .or_else(|| std::env::current_dir().ok())
            .and_then(|p| p.canonicalize().ok());

        // Create a temporary directory with a proper tsconfig.json.
        // This ensures tsgo uses ES module mode (import.meta, dynamic import, etc.)
        // regardless of the project's tsconfig.json state.
        let temp_dir_path = std::env::temp_dir().join(&*cstr!("vize-tsgo-{}", std::process::id()));
        std::fs::create_dir_all(&temp_dir_path)
            .map_err(|e| cstr!("Failed to create temp directory: {e}"))?;

        // Find and symlink node_modules so tsgo can resolve packages (e.g., 'vue').
        // Walk up from project root to find node_modules that contains 'vue'.
        let node_modules_path = project_root.as_ref().and_then(|root| {
            let mut dir = root.as_path();
            loop {
                let nm = dir.join("node_modules");
                if nm.join("vue").is_dir() {
                    return Some(nm);
                }
                dir = dir.parent()?;
            }
        });
        if let Some(ref nm_path) = node_modules_path {
            let symlink_target = temp_dir_path.join("node_modules");
            // Remove stale symlink if exists
            let _ = std::fs::remove_file(&symlink_target);
            #[cfg(unix)]
            {
                let _ = std::os::unix::fs::symlink(nm_path, &symlink_target);
            }
            #[cfg(windows)]
            {
                let _ = std::os::windows::fs::symlink_dir(nm_path, &symlink_target);
            }
        }

        let tsconfig_content = serde_json::json!({
            "compilerOptions": {
                "target": "ES2022",
                "module": "ESNext",
                "moduleResolution": "bundler",
                "lib": ["ES2022", "DOM", "DOM.Iterable"],
                "strict": true,
                "noEmit": true,
                "skipLibCheck": true
            }
        });
        std::fs::write(
            temp_dir_path.join("tsconfig.json"),
            tsconfig_content.to_compact_string(),
        )
        .map_err(|e| cstr!("Failed to write temp tsconfig.json: {e}"))?;

        let mut cmd = Command::new(tsgo.as_str());
        cmd.arg("--lsp")
            .arg("--stdio")
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());

        // Use temp directory as working directory so tsgo picks up our tsconfig.json
        cmd.current_dir(&temp_dir_path);

        let mut process = cmd
            .spawn()
            .map_err(|e| cstr!("Failed to start tsgo lsp: {e}"))?;

        let stdin = process
            .stdin
            .take()
            .ok_or("Failed to get stdin of tsgo lsp")?;
        let stdout = process
            .stdout
            .take()
            .ok_or("Failed to get stdout of tsgo lsp")?;

        // Set stdout to non-blocking mode on Unix
        #[cfg(unix)]
        {
            use libc::{fcntl, F_GETFL, F_SETFL, O_NONBLOCK};
            let fd = stdout.as_raw_fd();
            unsafe {
                let flags = fcntl(fd, F_GETFL);
                fcntl(fd, F_SETFL, flags | O_NONBLOCK);
            }
        }

        // Use temp dir path as rootUri for LSP initialization
        let temp_root = temp_dir_path.canonicalize().ok();
        let mut client = Self {
            process,
            stdin,
            stdout: BufReader::new(stdout),
            request_id: AtomicI64::new(1),
            diagnostics: FxHashMap::default(),
            temp_dir: Some(temp_dir_path),
        };

        // Initialize LSP with temp directory for tsconfig resolution
        client.initialize(temp_root.as_ref())?;

        Ok(client)
    }

    /// Initialize LSP connection
    fn initialize(&mut self, project_root: Option<&std::path::PathBuf>) -> Result<(), String> {
        // Convert project root to file:// URI
        let root_uri = project_root.map(|p| cstr!("file://{}", p.display()));

        let workspace_folders = root_uri.as_ref().map(|uri| {
            serde_json::json!([{
                "uri": uri,
                "name": "workspace"
            }])
        });

        let params = serde_json::json!({
            "processId": std::process::id(),
            "capabilities": {
                "textDocument": {
                    "publishDiagnostics": {
                        "relatedInformation": true
                    },
                    "diagnostic": {
                        "dynamicRegistration": false
                    }
                },
                "workspace": {
                    "workspaceFolders": true,
                    "configuration": true
                }
            },
            "rootUri": root_uri,
            "workspaceFolders": workspace_folders
        });

        let _response = self.send_request("initialize", params)?;

        // Send initialized notification
        self.send_notification("initialized", serde_json::json!({}))?;

        Ok(())
    }

    /// Shutdown the LSP server
    pub fn shutdown(&mut self) -> Result<(), String> {
        // Send shutdown request but don't wait for response (server may exit immediately)
        let shutdown_req = serde_json::json!({
            "jsonrpc": "2.0",
            "id": self.request_id.fetch_add(1, Ordering::SeqCst),
            "method": "shutdown",
            "params": Value::Null
        });
        let _ = self.send_message(&shutdown_req);

        // Send exit notification
        let _ = self.send_notification("exit", Value::Null);

        // Give server a moment to exit gracefully, then kill if needed
        thread::sleep(Duration::from_millis(10));
        let _ = self.process.kill();
        let _ = self.process.wait();
        Ok(())
    }

    /// Find tsgo in local node_modules
    fn find_tsgo_in_local_node_modules(working_dir: Option<&str>) -> Option<String> {
        let base_dir = working_dir
            .map(std::path::PathBuf::from)
            .or_else(|| std::env::current_dir().ok())?;

        // Platform-specific path for @typescript/native-preview
        let platform_suffix = if cfg!(target_os = "macos") {
            if cfg!(target_arch = "aarch64") {
                "darwin-arm64"
            } else {
                "darwin-x64"
            }
        } else if cfg!(target_os = "linux") {
            if cfg!(target_arch = "aarch64") {
                "linux-arm64"
            } else {
                "linux-x64"
            }
        } else if cfg!(target_os = "windows") {
            "win32-x64"
        } else {
            ""
        };

        // Helper to search for tsgo in a directory
        let search_in_dir = |dir: &std::path::Path| -> Option<String> {
            // Try pnpm structure first
            let pnpm_pattern = dir.join("node_modules/.pnpm");
            if pnpm_pattern.exists() {
                if let Ok(entries) = std::fs::read_dir(&pnpm_pattern) {
                    for entry in entries.flatten() {
                        let name = entry.file_name();
                        let name_str = name.to_string_lossy();
                        if name_str.starts_with("@typescript+native-preview-")
                            && name_str.contains(platform_suffix)
                        {
                            let native_path = entry.path().join(&*cstr!(
                                "node_modules/@typescript/native-preview-{}/lib/tsgo",
                                platform_suffix
                            ));
                            if native_path.exists() {
                                return Some(native_path.to_string_lossy().into());
                            }
                        }
                    }
                }
            }

            // Try npm/yarn structure
            let native_candidates = [
                dir.join(&*cstr!(
                    "node_modules/@typescript/native-preview-{}/lib/tsgo",
                    platform_suffix
                )),
                dir.join("node_modules/@typescript/native-preview/lib/tsgo"),
            ];

            for candidate in &native_candidates {
                if candidate.exists() {
                    return Some(candidate.to_string_lossy().into());
                }
            }

            // Fallback to .bin/tsgo (requires Node.js in PATH)
            let candidates = [
                dir.join("node_modules/.bin/tsgo"),
                dir.join("node_modules/@typescript/native-preview/bin/tsgo"),
            ];

            for candidate in &candidates {
                if candidate.exists() {
                    return Some(candidate.to_string_lossy().into());
                }
            }

            None
        };

        // Search in base_dir first
        if let Some(path) = search_in_dir(&base_dir) {
            return Some(path);
        }

        // Walk up parent directories to find workspace root's node_modules
        let mut current = base_dir.as_path();
        while let Some(parent) = current.parent() {
            if let Some(path) = search_in_dir(parent) {
                return Some(path);
            }
            current = parent;
        }

        None
    }

    /// Find tsgo in common npm global install locations
    fn find_tsgo_in_common_locations() -> Option<String> {
        let home = std::env::var("HOME").ok()?;

        // Common npm global binary locations
        let candidates: [String; 10] = [
            // npm global (custom prefix)
            cstr!("{home}/.npm-global/bin/tsgo"),
            // npm global (default)
            cstr!("{home}/.npm/bin/tsgo"),
            // pnpm global
            cstr!("{home}/.local/share/pnpm/tsgo"),
            // volta
            cstr!("{home}/.volta/bin/tsgo"),
            // mise/asdf shims
            cstr!("{home}/.local/share/mise/shims/tsgo"),
            cstr!("{home}/.asdf/shims/tsgo"),
            // fnm
            cstr!("{home}/.local/share/fnm/node-versions/current/bin/tsgo"),
            // nvm (check current version)
            cstr!("{home}/.nvm/versions/node/current/bin/tsgo"),
            // Homebrew (macOS)
            "/opt/homebrew/bin/tsgo".into(),
            "/usr/local/bin/tsgo".into(),
        ];

        for path in candidates {
            if std::path::Path::new(path.as_str()).exists() {
                return Some(path);
            }
        }

        // Also try to get from npm root -g
        if let Ok(output) = std::process::Command::new("npm")
            .args(["root", "-g"])
            .output()
        {
            if output.status.success() {
                #[allow(clippy::disallowed_types)]
                let npm_root = std::string::String::from_utf8_lossy(&output.stdout);
                let npm_root = npm_root.trim();
                // npm root -g returns lib path, bin is sibling
                if let Some(lib_parent) = std::path::Path::new(npm_root).parent() {
                    let tsgo_path = lib_parent.join("bin/tsgo");
                    if tsgo_path.exists() {
                        return Some(tsgo_path.to_string_lossy().into());
                    }
                }
            }
        }

        None
    }
}

impl Drop for TsgoLspClient {
    fn drop(&mut self) {
        let _ = self.shutdown();
        // Clean up temporary tsconfig directory
        if let Some(ref dir) = self.temp_dir {
            let _ = std::fs::remove_dir_all(dir);
        }
    }
}