Skip to content

Commit

Permalink
lsp: Ensure filepaths are properly escaped
Browse files Browse the repository at this point in the history
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 "<stdin>", line 1, in <module>
  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'
>>>
```
  • Loading branch information
alcarney committed Sep 25, 2024
1 parent 1bbf418 commit c04c26f
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/esbonio/esbonio/sphinx_agent/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c04c26f

Please sign in to comment.