Skip to main content

turbo_tasks_fs/
glob.rs

1use std::fmt::Display;
2
3use anyhow::{Result, bail};
4use bincode::{
5    Decode, Encode,
6    de::Decoder,
7    enc::Encoder,
8    error::{DecodeError, EncodeError},
9    impl_borrow_decode,
10};
11use regex::bytes::{Regex, RegexBuilder};
12use turbo_rcstr::{RcStr, rcstr};
13use turbo_tasks::{TaskInput, Vc, trace::TraceRawVcs};
14
15use crate::globset::parse;
16
17// Examples:
18// - file.js = File(file.js)
19// - *.js = AnyFile, File(.js)
20// - file*.js = File(file), AnyFile, File(.js)
21// - dir/file.js = File(dir), PathSeparator, File(file.js)
22// - **/*.js = AnyDirectories, PathSeparator, AnyFile, File(.js)
23// - {a/**,*}/file = Alternatives([File(a), PathSeparator, AnyDirectories], [AnyFile]),
24//   PathSeparator, File(file)
25
26// Note: a/**/b does match a/b, so we need some special logic about path
27// separators
28
29#[turbo_tasks::value(eq = "manual", serialization = "custom")]
30#[derive(Debug, Clone)]
31pub struct Glob {
32    glob: RcStr,
33    #[turbo_tasks(trace_ignore)]
34    opts: GlobOptions,
35    #[turbo_tasks(trace_ignore)]
36    regex: Regex,
37    #[turbo_tasks(trace_ignore)]
38    directory_match_regex: Regex,
39}
40
41impl PartialEq for Glob {
42    fn eq(&self, other: &Self) -> bool {
43        self.glob == other.glob
44    }
45}
46
47impl Eq for Glob {}
48
49impl Display for Glob {
50    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51        write!(f, "Glob({})", self.glob)
52    }
53}
54
55impl Encode for Glob {
56    fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
57        self.glob.encode(encoder)?;
58        self.opts.encode(encoder)?;
59        Ok(())
60    }
61}
62
63impl<Context> Decode<Context> for Glob {
64    fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> {
65        let glob = RcStr::decode(decoder)?;
66        let opts = GlobOptions::decode(decoder)?;
67        Glob::parse(glob, opts).map_err(|err| DecodeError::OtherString(err.to_string()))
68    }
69}
70
71impl_borrow_decode!(Glob);
72
73#[derive(
74    Copy, Clone, PartialEq, Eq, Hash, Default, TaskInput, TraceRawVcs, Debug, Encode, Decode,
75)]
76
77pub struct GlobOptions {
78    /// Whether the glob is a partial match.
79    /// Allows glob to match any part of the given string(s).
80    /// NOTE: this means that a pattern like `node_modules/package_name` with `contains:true` will
81    /// match `foo_node_modules/package_name_bar` If you want to match a _directory_ named
82    /// `node_modules/package_name` you should use `**/node_modules/package_name/**`
83    pub contains: bool,
84}
85
86impl Glob {
87    // Returns true if the glob matches the given path.
88    pub fn matches(&self, path: &str) -> bool {
89        self.regex.is_match(path.as_bytes())
90    }
91
92    // Returns true if the glob might match a filename underneath this `path` where the
93    // path represents a directory.
94    pub fn can_match_in_directory(&self, path: &str) -> bool {
95        debug_assert!(
96            !path.ends_with('/'),
97            "Path should be a directory name and not end with /"
98        );
99        self.directory_match_regex.is_match(path.as_bytes())
100    }
101
102    pub fn parse(input: RcStr, opts: GlobOptions) -> Result<Glob> {
103        let (glob_re, directory_match_re) = parse(&input, opts)?;
104        let regex = new_regex(glob_re.as_str());
105        let directory_match_regex = new_regex(directory_match_re.as_str());
106
107        Ok(Glob {
108            glob: input,
109            opts,
110            regex,
111            directory_match_regex,
112        })
113    }
114}
115
116#[turbo_tasks::value_impl]
117impl Glob {
118    #[turbo_tasks::function]
119    pub fn new(glob: RcStr, opts: GlobOptions) -> Result<Vc<Self>> {
120        Ok(Self::cell(Glob::parse(glob, opts)?))
121    }
122
123    #[turbo_tasks::function]
124    pub async fn alternatives(globs: Vec<Vc<Glob>>) -> Result<Vc<Self>> {
125        match globs.len() {
126            0 => Ok(Glob::new(rcstr!(""), GlobOptions::default())),
127            1 => Ok(globs.into_iter().next().unwrap()),
128            _ => {
129                let mut new_glob = String::new();
130                new_glob.push('{');
131                let mut opts = None;
132                for (index, glob) in globs.iter().enumerate() {
133                    if index > 0 {
134                        new_glob.push(',');
135                    }
136                    let glob = &*glob.await?;
137                    if let Some(old_opts) = opts {
138                        if old_opts != glob.opts {
139                            bail!(
140                                "Cannot compose globs with different options via the \
141                                 `alternatives` function."
142                            )
143                        }
144                    } else {
145                        opts = Some(glob.opts);
146                    }
147                    new_glob.push_str(&glob.glob);
148                }
149                new_glob.push('}');
150                // The loop must have iterated at least once, so the options must be initialized.
151                Ok(Glob::new(new_glob.into(), opts.unwrap()))
152            }
153        }
154    }
155}
156
157fn new_regex(pattern: &str) -> Regex {
158    RegexBuilder::new(pattern)
159        .dot_matches_new_line(true)
160        .build()
161        .expect("A successfully parsed glob should produce a valid regex")
162}
163
164#[cfg(test)]
165mod tests {
166    use rstest::*;
167
168    use super::*;
169
170    #[rstest]
171    #[case::file("file.js", "file.js")]
172    #[case::dir_and_file("../public/äöüščří.png", "../public/äöüščří.png")]
173    #[case::dir_and_file("dir/file.js", "dir/file.js")]
174    #[case::file_braces("file.{ts,js}", "file.js")]
175    #[case::dir_and_file_braces("dir/file.{ts,js}", "dir/file.js")]
176    #[case::dir_and_file_dir_braces("{dir,other}/file.{ts,js}", "dir/file.js")]
177    #[case::star("*.js", "file.js")]
178    #[case::dir_star("dir/*.js", "dir/file.js")]
179    #[case::globstar("**/*.js", "file.js")]
180    #[case::globstar("**/*.js", "dir/file.js")]
181    #[case::globstar("**/*.js", "dir/sub/file.js")]
182    #[case::globstar("**/**/*.js", "file.js")]
183    #[case::globstar("**/**/*.js", "dir/sub/file.js")]
184    #[case::globstar("**", "/foo")]
185    #[case::globstar("**", "foo")]
186    #[case::star("*", "foo")]
187    #[case::globstar_in_dir("dir/**/sub/file.js", "dir/sub/file.js")]
188    #[case::globstar_in_dir("dir/**/sub/file.js", "dir/a/sub/file.js")]
189    #[case::globstar_in_dir("dir/**/sub/file.js", "dir/a/b/sub/file.js")]
190    #[case::globstar_in_dir(
191        "**/next/dist/**/*.shared-runtime.js",
192        "next/dist/shared/lib/app-router-context.shared-runtime.js"
193    )]
194    #[case::star_dir(
195        "**/*/next/dist/server/next.js",
196        "node_modules/next/dist/server/next.js"
197    )]
198    #[case::node_modules_root("**/node_modules/**", "node_modules/next/dist/server/next.js")]
199    #[case::node_modules_root_package(
200        "**/node_modules/next/**",
201        "node_modules/next/dist/server/next.js"
202    )]
203    #[case::node_modules_nested(
204        "**/node_modules/**",
205        "apps/some-app/node_modules/regenerate-unicode-properties/Script_Extensions/Osage.js"
206    )]
207    #[case::node_modules_nested_package(
208        "**/node_modules/regenerate-unicode-properties/**",
209        "apps/some-app/node_modules/regenerate-unicode-properties/Script_Extensions/Osage.js"
210    )]
211    #[case::node_modules_pnpm(
212        "**/node_modules/**",
213        "node_modules/.pnpm/regenerate-unicode-properties@9.0.0/node_modules/\
214         regenerate-unicode-properties/Script_Extensions/Osage.js"
215    )]
216    #[case::node_modules_pnpm_package(
217        "**/node_modules/{regenerate,regenerate-unicode-properties}/**",
218        "node_modules/.pnpm/regenerate-unicode-properties@9.0.0/node_modules/\
219         regenerate-unicode-properties/Script_Extensions/Osage.js"
220    )]
221    #[case::node_modules_pnpm_prefixed_package(
222        "**/node_modules/{@blockfrost/blockfrost-js,@highlight-run/node,@libsql/client,@jpg-store/\
223         lucid-cardano,@mikro-orm/core,@mikro-orm/knex,@prisma/client,@sentry/nextjs,@sentry/node,\
224         @swc/core,argon2,autoprefixer,bcrypt,better-sqlite3,canvas,cpu-features,cypress,eslint,\
225         express,next-seo,node-pty,payload,pg,playwright,postcss,prettier,prisma,puppeteer,rimraf,\
226         sharp,shiki,sqlite3,tailwindcss,ts-node,typescript,vscode-oniguruma,webpack,websocket,@\
227         aws-sdk/client-dynamodb,@aws-sdk/lib-dynamodb}/**",
228        "node_modules/.pnpm/@aws-sdk+lib-dynamodb@3.445.0_@aws-sdk+client-dynamodb@3.445.0/\
229         node_modules/@aws-sdk/lib-dynamodb/dist-es/index.js"
230    )]
231    #[case::alternatives_nested1("{a,b/c,d/e/{f,g/h}}", "a")]
232    #[case::alternatives_nested2("{a,b/c,d/e/{f,g/h}}", "b/c")]
233    #[case::alternatives_nested3("{a,b/c,d/e/{f,g/h}}", "d/e/f")]
234    #[case::alternatives_nested4("{a,b/c,d/e/{f,g/h}}", "d/e/g/h")]
235    #[case::alternatives_empty1("react{,-dom}", "react")]
236    #[case::alternatives_empty2("react{,-dom}", "react-dom")]
237    #[case::alternatives_chars("[abc]", "b")]
238    fn glob_match(#[case] glob: &str, #[case] path: &str) {
239        let glob = Glob::parse(RcStr::from(glob), GlobOptions::default()).unwrap();
240
241        println!("{glob:?} {path}");
242
243        assert!(glob.matches(path));
244    }
245
246    #[rstest]
247    #[case::early_end("*.raw", "hello.raw.js")]
248    #[case::early_end(
249        "**/next/dist/esm/*.shared-runtime.js",
250        "next/dist/shared/lib/app-router-context.shared-runtime.js"
251    )]
252    #[case::star("*", "/foo")]
253    fn glob_not_matching(#[case] glob: &str, #[case] path: &str) {
254        let glob = Glob::parse(RcStr::from(glob), GlobOptions::default()).unwrap();
255
256        println!("{glob:?} {path}");
257
258        assert!(!glob.matches(path));
259    }
260
261    #[rstest]
262    #[case::dir_and_file_partial("dir/file.js", "dir")]
263    #[case::dir_star_partial("dir/*.js", "dir")]
264    #[case::globstar_partial("**/**/*.js", "dir")]
265    #[case::globstar_partial("**/**/*.js", "dir/sub")]
266    #[case::globstar_partial("**/**/*.js", "dir/sub/file.js")] // This demonstrates some ambiguity in naming. `file.js` might be a directory name.
267    #[case::globstar_in_dir_partial("dir/**/sub/file.js", "dir")]
268    #[case::globstar_in_dir_partial("dir/**/sub/file.js", "dir/a")]
269    #[case::globstar_in_dir_partial("dir/**/sub/file.js", "dir/a/b")]
270    #[case::globstar_in_dir_partial("dir/**/sub/file.js", "dir/a/b/sub")]
271    #[case::globstar_in_dir_partial("dir/**/sub/file.js", "dir/a/b/sub/file.js")]
272    fn glob_can_match_directory(#[case] glob: &str, #[case] path: &str) {
273        let glob = Glob::parse(RcStr::from(glob), GlobOptions::default()).unwrap();
274
275        println!("{glob:?} {path}");
276
277        assert!(glob.can_match_in_directory(path));
278    }
279    #[rstest]
280    #[case::dir_and_file_partial("dir/file.js", "dir/file.js")] // even if there was a dir, named `file.js` we know the glob wasn't intended to match it.
281    #[case::alternatives_chars("[abc]", "b")]
282    fn glob_not_can_match_directory(#[case] glob: &str, #[case] path: &str) {
283        let glob = Glob::parse(RcStr::from(glob), GlobOptions::default()).unwrap();
284
285        println!("{glob:?} {path}");
286
287        assert!(!glob.can_match_in_directory(path));
288    }
289
290    #[rstest]
291    #[case::star("*", "/foo")]
292    #[case::star("*", "foo")]
293    #[case::star("*", "foo/bar")]
294    #[case::prefix("foo/*", "bar/foo/baz")]
295    // This is a possibly surprising case.
296    #[case::dir_match("node_modules/foo", "my_node_modules/foobar")]
297    fn partial_glob_match(#[case] glob: &str, #[case] path: &str) {
298        let glob = Glob::parse(RcStr::from(glob), GlobOptions { contains: true }).unwrap();
299
300        println!("{glob:?} {path}");
301
302        assert!(glob.matches(path));
303    }
304
305    #[rstest]
306    #[case::literal("foo", "bar")]
307    #[case::suffix("*.js", "foo.ts")]
308    #[case::prefix("foo/*", "bar")]
309    // This is a possibly surprising case
310    #[case::dir_match("/node_modules/", "node_modules/")]
311    fn partial_glob_not_matching(#[case] glob: &str, #[case] path: &str) {
312        let glob = Glob::parse(RcStr::from(glob), GlobOptions { contains: true }).unwrap();
313
314        println!("{glob:?} {path}");
315
316        assert!(!glob.matches(path));
317    }
318}