monoripple 0.0.6

Symbol-aware affected target detection for JavaScript and TypeScript monorepos
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//! Build script for monoripple - handles building tsgo from source.
//!
//! When the `bundled-tsgo` feature is enabled (default), this script:
//! 1. Clones/updates the typescript-go repository from GitHub
//! 2. Applies any custom patches from patches/tsgo/
//! 3. Cross-compiles tsgo using Go's native cross-compilation
//! 4. Extracts TypeScript lib files needed for type resolution
//! 5. Compresses everything with zstd for smaller binary size
//! 6. Writes to OUT_DIR for inclusion via include_bytes!

use std::collections::hash_map::DefaultHasher;
use std::env;
use std::fs::{self, File};
use std::hash::{Hash, Hasher};
use std::io::{self, Read};
use std::path::{Path, PathBuf};
use std::process::Command;

/// Git repository URL for typescript-go
const TSGO_REPO: &str = "https://github.com/microsoft/typescript-go";

/// Pinned git commit SHA for reproducible builds.
/// The typescript-go repo doesn't use tags, so we pin to a specific commit.
/// Update this when upgrading tsgo.
const TSGO_COMMIT: &str = "89d5d5b2849a0db0957065889ca58536fa6d2e4a";

/// Path to patches relative to crate root
const PATCHES_DIR: &str = "patches/tsgo";

fn main() {
    // Only run for bundled-tsgo feature
    if env::var("CARGO_FEATURE_BUNDLED_TSGO").is_err() {
        println!("cargo:warning=Building without bundled tsgo (feature disabled)");
        create_empty_marker();
        return;
    }

    let target = env::var("TARGET").expect("TARGET env var not set");
    let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR env var not set"));
    let manifest_dir =
        PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set"));

    // Set up rerun triggers
    println!("cargo:rerun-if-env-changed=CARGO_FEATURE_BUNDLED_TSGO");
    println!("cargo:rerun-if-env-changed=TARGET");
    println!("cargo:rerun-if-changed=patches/tsgo");

    // Map Rust target triple to Go GOOS/GOARCH
    let (goos, goarch) = match target.as_str() {
        "x86_64-unknown-linux-gnu" | "x86_64-unknown-linux-musl" => ("linux", "amd64"),
        "aarch64-unknown-linux-gnu" | "aarch64-unknown-linux-musl" => ("linux", "arm64"),
        "arm-unknown-linux-gnueabihf" | "armv7-unknown-linux-gnueabihf" => ("linux", "arm"),
        "x86_64-apple-darwin" => ("darwin", "amd64"),
        "aarch64-apple-darwin" => ("darwin", "arm64"),
        "x86_64-pc-windows-msvc" | "x86_64-pc-windows-gnu" => ("windows", "amd64"),
        "aarch64-pc-windows-msvc" => ("windows", "arm64"),
        _ => {
            println!("cargo:warning=Unsupported target for bundled tsgo: {target}");
            println!(
                "cargo:warning=Build will succeed but typed registry precision will require an external tsgo installation"
            );
            create_empty_marker();
            return;
        }
    };

    // Determine binary name based on target OS
    let binary_name = if target.contains("windows") {
        "tsgo.exe"
    } else {
        "tsgo"
    };

    // Check for required tools
    if let Err(e) = check_required_tools() {
        println!("cargo:warning={e}");
        println!(
            "cargo:warning=Build will succeed but typed registry precision will require an external tsgo installation"
        );
        create_empty_marker();
        return;
    }

    // Get source directory in target/tsgo-source
    let target_dir = manifest_dir.join("target");
    let source_dir = target_dir.join("tsgo-source");

    // Collect patches
    let patches_dir = manifest_dir.join(PATCHES_DIR);
    let patches = match collect_patches(&patches_dir) {
        Ok(p) => p,
        Err(e) => {
            println!("cargo:warning=Failed to collect patches: {e}");
            create_empty_marker();
            return;
        }
    };

    // Compute cache key from tag + patches content
    let cache_key = compute_cache_key(&patches);
    let cache_marker = out_dir.join(format!("tsgo_cache_{}.marker", cache_key));
    let cached_binary = out_dir.join("tsgo.zst");
    let cached_libs = out_dir.join("tsgo_libs.tar.zst");

    // Check if we can use cached build
    if cache_marker.exists() && cached_binary.exists() && cached_libs.exists() {
        println!("cargo:warning=Using cached tsgo build (cache key: {cache_key})");
        // Write the embedded marker
        let marker_path = out_dir.join("tsgo_embedded_marker");
        fs::write(&marker_path, "1").expect("Failed to write marker file");
        return;
    }

    // Clone or update the repository
    if let Err(e) = setup_source_repo(&source_dir) {
        println!("cargo:warning=Failed to setup typescript-go source: {e}");
        create_empty_marker();
        return;
    }

    // Reset to clean state and apply patches
    if let Err(e) = prepare_source(&source_dir, &patches) {
        println!("cargo:warning=Failed to prepare source: {e}");
        create_empty_marker();
        return;
    }

    // Build tsgo
    let built_binary = source_dir.join("tsgo").join(binary_name);
    if let Err(e) = build_tsgo(&source_dir, &built_binary, goos, goarch) {
        println!("cargo:warning=Failed to build tsgo: {e}");
        create_empty_marker();
        return;
    }

    // Read and compress the tsgo binary
    let tsgo_binary = match fs::read(&built_binary) {
        Ok(data) => data,
        Err(e) => {
            println!("cargo:warning=Failed to read built tsgo binary: {e}");
            create_empty_marker();
            return;
        }
    };

    println!(
        "cargo:warning=Compressing tsgo binary ({} bytes)",
        tsgo_binary.len()
    );

    let compressed = match zstd::encode_all(tsgo_binary.as_slice(), 19) {
        Ok(data) => data,
        Err(e) => {
            println!("cargo:warning=Failed to compress tsgo binary: {e}");
            create_empty_marker();
            return;
        }
    };

    println!(
        "cargo:warning=Compressed tsgo: {} -> {} bytes ({:.1}% reduction)",
        tsgo_binary.len(),
        compressed.len(),
        (1.0 - compressed.len() as f64 / tsgo_binary.len() as f64) * 100.0
    );

    // Write compressed binary to OUT_DIR
    if let Err(e) = fs::write(&cached_binary, &compressed) {
        println!("cargo:warning=Failed to write compressed tsgo: {e}");
        create_empty_marker();
        return;
    }

    // Collect and compress TypeScript lib files from source
    let lib_dir = source_dir.join("internal").join("bundled").join("libs");
    let libs_data = match collect_lib_files(&lib_dir) {
        Ok(data) => data,
        Err(e) => {
            println!("cargo:warning=Failed to collect lib files: {e}");
            Vec::new()
        }
    };

    if !libs_data.is_empty() {
        let compressed_libs = match zstd::encode_all(libs_data.as_slice(), 19) {
            Ok(data) => data,
            Err(e) => {
                println!("cargo:warning=Failed to compress lib files: {e}");
                Vec::new()
            }
        };

        if !compressed_libs.is_empty() {
            if let Err(e) = fs::write(&cached_libs, &compressed_libs) {
                println!("cargo:warning=Failed to write lib files: {e}");
            } else {
                println!(
                    "cargo:warning=Embedded {} bytes of TypeScript lib files (compressed)",
                    compressed_libs.len()
                );
            }
        }
    }

    // Write cache marker
    if let Err(e) = fs::write(&cache_marker, &cache_key) {
        println!("cargo:warning=Failed to write cache marker: {e}");
    }

    // Clean up old cache markers
    cleanup_old_cache_markers(&out_dir, &cache_key);

    // Write the build version info file for embedded.rs to use
    // This includes the cache key so the runtime cache is invalidated when patches change
    let version_info = format!(
        "/// Auto-generated by build.rs - do not edit\n\
         pub const TSGO_COMMIT: &str = \"{}\";\n\
         pub const TSGO_BUILD_HASH: &str = \"{}\";\n",
        TSGO_COMMIT, cache_key
    );
    let version_path = out_dir.join("tsgo_version.rs");
    fs::write(&version_path, version_info).expect("Failed to write version info");

    // Write marker file indicating successful embedding
    let marker_path = out_dir.join("tsgo_embedded_marker");
    fs::write(&marker_path, "1").expect("Failed to write marker file");

    println!(
        "cargo:warning=Successfully built and embedded tsgo from source (commit: {TSGO_COMMIT}, hash: {cache_key})"
    );
}

/// Check that required tools (git, go) are installed.
fn check_required_tools() -> Result<(), String> {
    // Check for git
    let git_check = Command::new("git").arg("--version").output();

    match git_check {
        Ok(output) if output.status.success() => {}
        _ => {
            return Err(
                "git is required to build tsgo from source. Please install git.".to_string(),
            );
        }
    }

    // Check for go
    let go_check = Command::new("go").arg("version").output();

    match go_check {
        Ok(output) if output.status.success() => {
            let version = String::from_utf8_lossy(&output.stdout);
            println!("cargo:warning=Found Go: {}", version.trim());
        }
        _ => {
            return Err(
                "Go 1.24+ is required to build tsgo from source. Install from https://go.dev/dl/"
                    .to_string(),
            );
        }
    }

    Ok(())
}

/// Collect patch files from the patches directory.
fn collect_patches(patches_dir: &Path) -> io::Result<Vec<PathBuf>> {
    let mut patches = Vec::new();

    if !patches_dir.exists() {
        return Ok(patches);
    }

    for entry in fs::read_dir(patches_dir)? {
        let entry = entry?;
        let path = entry.path();

        if path.is_file()
            && path
                .extension()
                .is_some_and(|extension| extension == "patch")
        {
            patches.push(path);
        }
    }

    // Sort patches by filename for deterministic order
    patches.sort();

    Ok(patches)
}

/// Compute a cache key based on the commit and patch contents.
fn compute_cache_key(patches: &[PathBuf]) -> String {
    let mut hasher = DefaultHasher::new();

    // Hash the commit SHA
    TSGO_COMMIT.hash(&mut hasher);

    // Hash each patch file's contents
    for patch in patches {
        if let Ok(contents) = fs::read(patch) {
            contents.hash(&mut hasher);
        }
        // Also hash the filename
        if let Some(name) = patch.file_name() {
            name.to_string_lossy().hash(&mut hasher);
        }
    }

    format!("{:016x}", hasher.finish())
}

/// Clone or update the typescript-go repository.
fn setup_source_repo(source_dir: &Path) -> Result<(), String> {
    if source_dir.exists() {
        // Repository exists, fetch updates
        println!("cargo:warning=Updating typescript-go repository...");

        let fetch = Command::new("git")
            .args(["fetch", "origin"])
            .current_dir(source_dir)
            .output()
            .map_err(|e| format!("Failed to run git fetch: {e}"))?;

        if !fetch.status.success() {
            let stderr = String::from_utf8_lossy(&fetch.stderr);
            return Err(format!("git fetch failed: {stderr}"));
        }
    } else {
        // Clone the repository (without submodules - we only need to build)
        println!("cargo:warning=Cloning typescript-go repository (this may take a moment)...");

        // Ensure parent directory exists
        if let Some(parent) = source_dir.parent() {
            fs::create_dir_all(parent).map_err(|e| format!("Failed to create target dir: {e}"))?;
        }

        // Clone without depth limit since we need to checkout a specific commit
        // that might not be the latest
        let clone = Command::new("git")
            .args(["clone", TSGO_REPO, &source_dir.to_string_lossy()])
            .output()
            .map_err(|e| format!("Failed to run git clone: {e}"))?;

        if !clone.status.success() {
            let stderr = String::from_utf8_lossy(&clone.stderr);
            return Err(format!("git clone failed: {stderr}"));
        }
    }

    Ok(())
}

/// Prepare the source directory: checkout the correct commit and apply patches.
fn prepare_source(source_dir: &Path, patches: &[PathBuf]) -> Result<(), String> {
    // Reset to clean state at the specified commit
    println!("cargo:warning=Checking out commit {TSGO_COMMIT}...");

    let checkout = Command::new("git")
        .args(["checkout", "--force", TSGO_COMMIT])
        .current_dir(source_dir)
        .output()
        .map_err(|e| format!("Failed to run git checkout: {e}"))?;

    if !checkout.status.success() {
        let stderr = String::from_utf8_lossy(&checkout.stderr);
        return Err(format!("git checkout failed: {stderr}"));
    }

    // Clean any untracked files
    let clean = Command::new("git")
        .args(["clean", "-fdx"])
        .current_dir(source_dir)
        .output()
        .map_err(|e| format!("Failed to run git clean: {e}"))?;

    if !clean.status.success() {
        let stderr = String::from_utf8_lossy(&clean.stderr);
        println!("cargo:warning=git clean warning: {stderr}");
    }

    // Apply patches
    for patch in patches {
        let patch_name = patch.file_name().unwrap_or_default().to_string_lossy();
        println!("cargo:warning=Applying patch: {patch_name}");

        let apply = Command::new("git")
            .args(["apply", "--verbose", &patch.to_string_lossy()])
            .current_dir(source_dir)
            .output()
            .map_err(|e| format!("Failed to run git apply for {patch_name}: {e}"))?;

        if !apply.status.success() {
            let stderr = String::from_utf8_lossy(&apply.stderr);
            return Err(format!("Failed to apply patch {patch_name}: {stderr}"));
        }
    }

    if !patches.is_empty() {
        println!(
            "cargo:warning=Applied {} patch(es) successfully",
            patches.len()
        );
    }

    Ok(())
}

/// Build tsgo using Go cross-compilation.
fn build_tsgo(
    source_dir: &Path,
    output_path: &Path,
    goos: &str,
    goarch: &str,
) -> Result<(), String> {
    println!("cargo:warning=Building tsgo for {goos}/{goarch}...");

    // Ensure output directory exists
    if let Some(parent) = output_path.parent() {
        fs::create_dir_all(parent).map_err(|e| format!("Failed to create output dir: {e}"))?;
    }

    let build = Command::new("go")
        .args([
            "build",
            "-trimpath",
            "-ldflags=-s -w",
            "-o",
            &output_path.to_string_lossy(),
            "./cmd/tsgo",
        ])
        .env("GOOS", goos)
        .env("GOARCH", goarch)
        .env("CGO_ENABLED", "0")
        .current_dir(source_dir)
        .output()
        .map_err(|e| format!("Failed to run go build: {e}"))?;

    if !build.status.success() {
        let stderr = String::from_utf8_lossy(&build.stderr);
        let stdout = String::from_utf8_lossy(&build.stdout);
        return Err(format!(
            "go build failed:\nstdout: {stdout}\nstderr: {stderr}"
        ));
    }

    // Verify the binary was created
    if !output_path.exists() {
        return Err(format!(
            "go build completed but binary not found at {}",
            output_path.display()
        ));
    }

    let size = fs::metadata(output_path).map(|m| m.len()).unwrap_or(0);

    println!("cargo:warning=Built tsgo binary: {} bytes", size);

    Ok(())
}

/// Collect TypeScript lib.*.d.ts files into a tar archive (in memory).
fn collect_lib_files(lib_dir: &Path) -> io::Result<Vec<u8>> {
    let mut builder = tar::Builder::new(Vec::new());

    if !lib_dir.exists() {
        println!(
            "cargo:warning=Lib directory not found: {}",
            lib_dir.display()
        );
        return Ok(Vec::new());
    }

    let mut count = 0;
    for entry in fs::read_dir(lib_dir)? {
        let entry = entry?;
        let path = entry.path();
        let file_name = path.file_name().unwrap().to_string_lossy();

        // Only include lib.*.d.ts files
        if file_name.starts_with("lib.") && file_name.ends_with(".d.ts") {
            let mut file = File::open(&path)?;
            let mut contents = Vec::new();
            file.read_to_end(&mut contents)?;

            let mut header = tar::Header::new_gnu();
            header.set_path(file_name.as_ref())?;
            header.set_size(contents.len() as u64);
            header.set_mode(0o644);
            header.set_cksum();

            builder.append(&header, contents.as_slice())?;
            count += 1;
        }
    }

    println!("cargo:warning=Collected {count} TypeScript lib files");
    builder.into_inner()
}

/// Clean up old cache marker files.
fn cleanup_old_cache_markers(out_dir: &Path, current_key: &str) {
    if let Ok(entries) = fs::read_dir(out_dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if let Some(name) = path.file_name() {
                let name = name.to_string_lossy();
                if name.starts_with("tsgo_cache_")
                    && name.ends_with(".marker")
                    && !name.contains(current_key)
                {
                    let _ = fs::remove_file(&path);
                }
            }
        }
    }
}

/// Create an empty marker indicating no embedded tsgo.
fn create_empty_marker() {
    let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR env var not set"));

    // Create empty placeholder files so include_bytes! doesn't fail
    let compressed_path = out_dir.join("tsgo.zst");
    fs::write(&compressed_path, &[] as &[u8]).ok();

    let libs_path = out_dir.join("tsgo_libs.tar.zst");
    fs::write(&libs_path, &[] as &[u8]).ok();

    // Do NOT write the marker file - its absence indicates no embedding
}