From 6782b9daa35e500f160523460573d2c130b94da5 Mon Sep 17 00:00:00 2001 From: Rebecca Turner Date: Thu, 30 Nov 2023 10:15:18 -0800 Subject: [PATCH] lib.escapeRegex: fix with `boost::regex` implementation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/NixOS/nix/pull/7762 switched `nix` to use the `boost::regex` implementation, which expects that all literal braces are escaped. That is, the regex `\{crate}` no longer parses. We modify `lib.escapeRegex` to accommodate this change. Fortunately, the old regex implementation doesn't mind if closing braces are escaped as well, so this is backwards compatible and we don't need to worry about version-gating it. Without this patch: Old regex implementation: $ nix repl nixpkgs Welcome to Nix 2.18.1. Type :? for help. Loading installable 'flake:nixpkgs#'... Added 5 variables. nix-repl> builtins.match (lib.escapeRegex "{}") "{}" [ ] $ ~/nix/result/bin/nix repl nixpkgs Welcome to Nix 2.20.0pre20231130_dirty. Type :? for help. `boost::regex` implementation: $ ~/nix/result/bin/nix repl nixpkgs Welcome to Nix 2.20.0pre20231130_dirty. Type :? for help. Loading installable 'flake:nixpkgs#'... Added 5 variables. nix-repl> builtins.match (lib.escapeRegex "{}") "{}" error: … while calling the 'match' builtin at «string»:1:1: 1| builtins.match (lib.escapeRegex "{}") "{}" | ^ error: invalid regular expression '\{}' --- lib/strings.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/strings.nix b/lib/strings.nix index 695aaaacd3488..a3a1321aec346 100644 --- a/lib/strings.nix +++ b/lib/strings.nix @@ -530,7 +530,7 @@ rec { escapeRegex "[^a-z]*" => "\\[\\^a-z]\\*" */ - escapeRegex = escape (stringToCharacters "\\[{()^$?*+|."); + escapeRegex = escape (stringToCharacters "\\[{}()^$?*+|."); /* Quotes a string if it can't be used as an identifier directly.