Unverified Commit 3235c6f8 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

[turbopack] Fix a buggy serializer (#79520)

Because regexes aren't naively serializable, we use a simple alternate representation and then just re-parse them when deserializing.  A trivial bug was introduced in #78251  that has broken deserialization.  Fix that and add a unit test.

My only defense for such a silly mistake is that this was my first PR 🤦‍♂️
parent ed2db11e
......@@ -9508,6 +9508,7 @@ dependencies = [
"regex",
"regress",
"serde",
"serde_json",
"turbo-tasks",
"turbo-tasks-build",
]
......
......@@ -9,6 +9,7 @@ anyhow = { workspace = true }
regex = { workspace = true }
regress = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
turbo-tasks = { workspace = true }
[build-dependencies]
......
......@@ -43,7 +43,7 @@ impl TryFrom<RegexForm> for EsRegex {
type Error = anyhow::Error;
fn try_from(value: RegexForm) -> std::result::Result<Self, Self::Error> {
EsRegex::new(&value.pattern, &value.pattern)
EsRegex::new(&value.pattern, &value.flags)
}
}
......@@ -87,7 +87,7 @@ impl EsRegex {
'u' => applied_flags.push('u'),
// sticky search: not relevant for the regex itself
'y' => {}
_ => bail!("unsupported flag `{}` in regex", flag),
_ => bail!("unsupported flag `{flag}` in regex: `{pattern}` with flags: `{flags}`"),
}
}
......@@ -129,6 +129,14 @@ impl EsRegex {
mod tests {
use super::{EsRegex, EsRegexImpl};
#[test]
fn round_trip_serialize() {
let regex = EsRegex::new("[a-z]", "i").unwrap();
let serialized = serde_json::to_string(&regex).unwrap();
let parsed = serde_json::from_str::<EsRegex>(&serialized).unwrap();
assert_eq!(regex, parsed);
}
#[test]
fn es_regex_matches_simple() {
let regex = EsRegex::new("a", "").unwrap();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment