Skip to content

Visual Studio Code, rebuilt in Rust

Rust
12.7 KB
Raw Blame
//! Running ripgrep (base/node/ripgrep.ts, search/node/ripgrepSearchUtils.ts) and the glob arguments both searches
//! share (search/node/ripgrepTextSearchEngine.ts).

use std::io::{BufRead, BufReader, Read};
#[cfg(windows)]
use std::os::windows::process::CommandExt;
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::sync::Mutex;
use std::sync::atomic::{AtomicBool, Ordering};

use files::glob;

/// `rgDiskPath`: the `@vscode/ripgrep-universal` binary for this machine.
pub fn rg_path(app_root: &Path) -> PathBuf {
    let arch = match std::env::consts::ARCH {
        "aarch64" => "arm64",
        "x86_64" => "x64",
        other => other,
    };
    let (platform, binary) = match std::env::consts::OS {
        "windows" => ("win32", "rg.exe"),
        "linux" => ("linux", "rg"),
        _ => ("darwin", "rg"),
    };
    app_root.join(format!("node_modules/@vscode/ripgrep-universal/bin/{platform}-{arch}/{binary}"))
}

/// The cancellation token of one search; cancelling kills the ripgrep it runs.
#[derive(Default)]
pub struct Cancellation {
    cancelled: AtomicBool,
    child: Mutex<Option<Child>>,
}

impl Cancellation {
    pub fn cancel(&self) {
        self.cancelled.store(true, Ordering::SeqCst);
        if let Some(child) = self.child.lock().unwrap().as_mut() {
            let _ = child.kill();
        }
    }

    pub fn is_cancelled(&self) -> bool {
        self.cancelled.load(Ordering::SeqCst)
    }
}

/// `MAX_RIPGREP_SYMLINK_LOOPS`: ripgrep only detects links back to an ancestor, so cross-linked folders make it list
/// exponentially many paths; past this many reported loops the run stops.
pub const MAX_SYMLINK_LOOPS: usize = 1000;

pub struct Finished {
    pub stderr: String,
    /// `RipgrepSymlinkLoopDetector`: ripgrep reported more than `MAX_SYMLINK_LOOPS` loops and was killed.
    pub too_many_symlink_loops: bool,
    /// Exit code 0: something matched (ripgrep exits with 1 when nothing did).
    pub success: bool,
    pub got_data: bool,
}

/// Runs ripgrep in `cwd` and hands each stdout line (without the newline) to `on_line` until it returns false, which
/// kills ripgrep. With `count_symlink_loops` (a run with `--follow`), too many reported loops kill it too.
pub fn run(rg: &Path, args: &[String], cwd: &Path, cancellation: &Cancellation, count_symlink_loops: bool, on_line: &mut dyn FnMut(&str) -> bool) -> std::io::Result<Finished> {
    let mut command = Command::new(rg);
    command.args(args).current_dir(cwd).stdin(Stdio::null()).stdout(Stdio::piped()).stderr(Stdio::piped());
    // CREATE_NO_WINDOW: rg.exe is a console program, each search would flash a console window on Windows.
    #[cfg(windows)]
    command.creation_flags(0x0800_0000);
    let mut child = command.spawn()?;
    let stdout = child.stdout.take().expect("stdout is piped");
    let stderr = child.stderr.take().expect("stderr is piped");
    *cancellation.child.lock().unwrap() = Some(child);
    if cancellation.is_cancelled() {
        cancellation.cancel();
    }
    std::thread::scope(|scope| {
        let stderr = scope.spawn(|| read_stderr(stderr, count_symlink_loops, cancellation));
        let mut reader = BufReader::new(stdout);
        let mut line = Vec::new();
        let mut got_data = false;
        while !cancellation.is_cancelled() && reader.read_until(b'\n', &mut line)? > 0 {
            got_data = true;
            let text = String::from_utf8_lossy(&line);
            if !on_line(text.strip_suffix('\n').unwrap_or(&text)) {
                break;
            }
            line.clear();
        }
        let mut child = cancellation.child.lock().unwrap().take().expect("set above");
        let _ = child.kill();
        let status = child.wait()?;
        let (stderr, too_many_symlink_loops) = stderr.join().unwrap_or_default();
        Ok(Finished { stderr, too_many_symlink_loops, success: status.success(), got_data })
    })
}

/// The whole stderr, and whether it reported more than `MAX_SYMLINK_LOOPS` loops, which kills ripgrep at once.
fn read_stderr(stderr: impl Read, count_symlink_loops: bool, cancellation: &Cancellation) -> (String, bool) {
    let mut text = String::new();
    let mut loops = 0;
    let mut reader = BufReader::new(stderr);
    let mut line = Vec::new();
    while reader.read_until(b'\n', &mut line).is_ok_and(|read| read > 0) {
        let line_text = String::from_utf8_lossy(&line);
        if count_symlink_loops && line_text.ends_with('\n') && line_text.contains("File system loop found") {
            loops += 1;
            if loops > MAX_SYMLINK_LOOPS {
                if let Some(child) = cancellation.child.lock().unwrap().as_mut() {
                    let _ = child.kill();
                }
                return (text, true);
            }
        }
        // stderr.length + message.length < 1e6: the text kept for the error message stays bounded.
        if text.len() + line_text.len() < 1_000_000 {
            text.push_str(&line_text);
        }
        line.clear();
    }
    (text, false)
}

/// `anchorGlob`: ripgrep globs without a leading `**` or `/` match at any depth, VS Code globs only at the root.
pub fn anchor_glob(glob: &str) -> String {
    if glob.starts_with("**") || glob.starts_with('/') { glob.to_string() } else { format!("/{glob}") }
}

/// `getExcludeExceptionGlobs`: the globs of a run that lists only what `!` exceptions re-include, since ripgrep does
/// not descend into excluded folders.
pub fn exclude_exception_globs(exclude_exceptions: &[String]) -> Vec<String> {
    let exceptions: Vec<String> = exclude_exceptions.iter().flat_map(|exception| perform_brace_expansion_for_ripgrep(exception)).collect();
    let mut globs: Vec<String> = Vec::new();
    let mut add = |glob: String| {
        if !globs.contains(&glob) {
            globs.push(glob);
        }
    };
    // Unless an exception can match at any depth, only the folders that lead to the exceptions are walked.
    if !exceptions.iter().any(|exception| exception.starts_with("**")) {
        add("!*".to_string());
        for exception in &exceptions {
            let segments = glob::split_glob_aware(exception, '/');
            for end in 1..segments.len() {
                add(format!("{}/", anchor_glob(&segments[..end].join("/"))));
            }
        }
    }
    for exception in &exceptions {
        add(anchor_glob(exception));
        add(anchor_glob(&format!("{exception}/**")));
    }
    for folder in glob::get_folders_skipped_by_exclude_exceptions(exclude_exceptions) {
        add(format!("!**/{folder}"));
    }
    globs
}

/// `performBraceExpansionForRipgrep`: ripgrep rejects nested braces, so one level of `{a,b}` becomes separate globs.
pub fn perform_brace_expansion_for_ripgrep(pattern: &str) -> Vec<String> {
    let Split { fixed_start, in_braces, fixed_end } = escape_aware_split(pattern);
    let (Some(fixed_start), Some(fixed_end)) = (fixed_start, fixed_end) else { return vec![in_braces] };
    let mut alternatives = glob::split_glob_aware(&in_braces, ',');
    if alternatives.is_empty() {
        alternatives.push(String::new());
    }
    let ends = perform_brace_expansion_for_ripgrep(&fixed_end);
    let mut expanded = Vec::new();
    for alternative in &alternatives {
        for end in &ends {
            expanded.push(format!("{fixed_start}{alternative}{end}"));
        }
    }
    expanded
}

struct Split {
    fixed_start: Option<String>,
    in_braces: String,
    fixed_end: Option<String>,
}

/// `getEscapeAwareSplitStringForRipgrep`: splits at the first unescaped `{...}`; a second `{` before the `}` is a
/// nesting ripgrep rejects, so the pattern stays whole.
fn escape_aware_split(pattern: &str) -> Split {
    let mut in_braces = false;
    let mut escaped = false;
    let mut fixed_start = String::new();
    let mut braces = String::new();
    for (index, char) in pattern.char_indices() {
        let target = if in_braces { &mut braces } else { &mut fixed_start };
        match char {
            '\\' if escaped => {
                target.push_str("\\\\");
                escaped = false;
            }
            '\\' => escaped = true,
            '{' | '}' if escaped => {
                target.push(char);
                escaped = false;
            }
            '{' if in_braces => return Split { fixed_start: None, in_braces: format!("{fixed_start}{{{braces}{{{}", &pattern[index + 1..]), fixed_end: None },
            '{' => in_braces = true,
            '}' if in_braces => return Split { fixed_start: Some(fixed_start), in_braces: braces, fixed_end: Some(pattern[index + 1..].to_string()) },
            _ => {
                if escaped {
                    target.push('\\');
                }
                target.push(char);
                escaped = false;
            }
        }
    }
    let rest = if in_braces { format!("{{{braces}") } else { String::new() };
    Split { fixed_start: None, in_braces: format!("{fixed_start}{rest}"), fixed_end: None }
}

/// A folder with an `rg` script standing in for ripgrep: with `--follow` it prints `followed`, then more than
/// `MAX_SYMLINK_LOOPS` loop errors, then hangs until killed; without it prints `unfollowed`. A `/bin/sh` script, so
/// Unix only.
#[cfg(all(test, unix))]
pub(crate) fn rg_with_symlink_loops(name: &str, followed: &[String], unfollowed: &[String]) -> PathBuf {
    use std::os::unix::fs::PermissionsExt;
    let folder = std::env::temp_dir().join(format!("search-{name}-{}", std::process::id()));
    std::fs::create_dir_all(&folder).unwrap();
    let print = |lines: &[String]| lines.iter().map(|line| format!("printf '%s\\n' '{line}'\n")).collect::<String>();
    let script = format!(
        "#!/bin/sh\ncase \" $* \" in\n*\" --follow \"*)\n{}i=0\nwhile [ $i -le {MAX_SYMLINK_LOOPS} ]; do echo 'rg: File system loop found: a/b points to an ancestor a' >&2; i=$((i+1)); done\nexec sleep 30;;\n*)\n{};;\nesac\n",
        print(followed),
        print(unfollowed)
    );
    let rg = folder.join("rg");
    std::fs::write(&rg, script).unwrap();
    std::fs::set_permissions(&rg, std::fs::Permissions::from_mode(0o755)).unwrap();
    folder
}

#[cfg(test)]
mod tests {
    use super::*;

    #[cfg(unix)]
    #[test]
    fn too_many_symlink_loops_kill_a_run_that_follows_them() {
        let folder = rg_with_symlink_loops("ripgrep-loops", &["a.txt".into()], &[]);
        let mut lines = Vec::new();
        let finished = run(&folder.join("rg"), &["--follow".into()], &folder, &Cancellation::default(), true, &mut |line| {
            lines.push(line.to_string());
            true
        })
        .unwrap();
        assert!(finished.too_many_symlink_loops);
        assert_eq!(lines, ["a.txt"]);
    }

    #[test]
    fn brace_expansion() {
        let cases: [(&str, &[&str]); 18] = [
            ("eep/{a,b}/test", &["eep/a/test", "eep/b/test"]),
            ("eep/{a,b}/{c,d,e}", &["eep/a/c", "eep/a/d", "eep/a/e", "eep/b/c", "eep/b/d", "eep/b/e"]),
            ("eep/{a,b}/\\{c,d,e}", &["eep/a/{c,d,e}", "eep/b/{c,d,e}"]),
            ("eep/{a,b\\}/test", &["eep/{a,b}/test"]),
            ("eep/{a,b\\\\}/test", &["eep/a/test", "eep/b\\\\/test"]),
            ("eep/{a,b\\\\\\}/test", &["eep/{a,b\\\\}/test"]),
            ("e\\{ep/{a,b}/test", &["e{ep/a/test", "e{ep/b/test"]),
            ("eep/{a,\\b}/test", &["eep/a/test", "eep/\\b/test"]),
            ("{a/*.*,b/*.*}", &["a/*.*", "b/*.*"]),
            ("{{}", &["{{}"]),
            ("aa{{}", &["aa{{}"]),
            ("{b{}", &["{b{}"]),
            ("{{}c", &["{{}c"]),
            ("{{}}", &["{{}}"]),
            ("\\{{}}", &["{}"]),
            ("{}foo", &["foo"]),
            ("bar{ }foo", &["bar foo"]),
            ("{}", &[""]),
        ];
        for (pattern, expected) in cases {
            assert_eq!(perform_brace_expansion_for_ripgrep(pattern), expected, "{pattern}");
        }
    }

    #[test]
    fn exclude_exception_globs_walk_only_the_folders_leading_to_anchored_exceptions() {
        let globs = |exceptions: &[&str]| exclude_exception_globs(&exceptions.iter().map(|exception| exception.to_string()).collect::<Vec<_>>());
        assert_eq!(
            globs(&["node_modules/{@types,typescript}", "src/**/keep.js", "gulpfile.js"]),
            [
                "!*", "/node_modules/", "/src/", "/src/**/", "/node_modules/@types", "/node_modules/@types/**", "/node_modules/typescript",
                "/node_modules/typescript/**", "/src/**/keep.js", "/src/**/keep.js/**", "/gulpfile.js", "/gulpfile.js/**", "!**/.git", "!**/.svn",
                "!**/.hg", "!**/.jj"
            ]
        );
        assert_eq!(
            globs(&["node_modules/@types", "**/Scripts"]),
            ["/node_modules/@types", "/node_modules/@types/**", "**/Scripts", "**/Scripts/**", "!**/.git", "!**/.svn", "!**/.hg", "!**/.jj"]
        );
        assert_eq!(
            globs(&["{.git,.hg}/hooks"]),
            ["!*", "/.git/", "/.hg/", "/.git/hooks", "/.git/hooks/**", "/.hg/hooks", "/.hg/hooks/**", "!**/.svn", "!**/.jj"]
        );
    }
}