From c04c26fab6a265c0a708ddd405b0dccf3f358e25 Mon Sep 17 00:00:00 2001 From: Alex Carney Date: Wed, 25 Sep 2024 19:43:46 +0100 Subject: [PATCH] lsp: Ensure filepaths are properly escaped Backslashes in Windows filepaths can cause issues ```python >>> import re >>> VARIABLE = re.compile(r"\$\{(\w+)\}") >>> VARIABLE.sub('\\path\\to\\cache', '${defaultBuildDir}/doctrees') Traceback (most recent call last): File "", line 1, in File "/usr/lib64/python3.12/re/__init__.py", line 334, in _compile_template return _sre.template(pattern, _parser.parse_template(repl, pattern)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib64/python3.12/re/_parser.py", line 1075, in parse_template raise s.error('bad escape %s' % this, len(this)) from None re.error: bad escape \p at position 0 ``` Calling `re.escape` on the replacement ensures that characters like backslashes are escaped correctly ```python >>> VARIABLE.sub(re.escape('\\path\\to\\cache'), '${defaultBuildDir}/doctrees') '\\path\\to\\cache/doctrees' >>> ``` --- lib/esbonio/esbonio/sphinx_agent/config.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/esbonio/esbonio/sphinx_agent/config.py b/lib/esbonio/esbonio/sphinx_agent/config.py index 1bb6de227..e491dcd4f 100644 --- a/lib/esbonio/esbonio/sphinx_agent/config.py +++ b/lib/esbonio/esbonio/sphinx_agent/config.py @@ -160,7 +160,9 @@ def to_application_args(self, context: dict[str, Any]) -> dict[str, Any]: continue replacement = self.resolve_config_variable(match.group(1), context) - setattr(self, name, VARIABLE.sub(replacement, value)) + result = VARIABLE.sub(re.escape(replacement), value) + + setattr(self, name, result) build_dir = pathlib.Path(self.build_dir).resolve() doctree_dir = pathlib.Path(self.doctree_dir).resolve()