RSpec icon indicating copy to clipboard operation
RSpec copied to clipboard

Toggle Spec/Source crashes on Windows with `re.error: bad escape \l` when the fast-path guess misses

Open dpc00 opened this issue 2 months ago • 1 comments

What happens

RSpecToggleSourceOrSpec.py's code_paths:

def code_paths(self, file_path: str):
    file_path = re.sub(r"\b(\w+)_spec\.rb$", r"\1.rb", file_path)
    return [
        re.sub(r"\bspec\b", "app", file_path),
        re.sub(r"\bspec\b", "lib", file_path),
        re.sub(r"\b{}\b".format(os.path.join("spec", "lib")), "lib", file_path),
    ]

The third pattern builds part of a regex with os.path.join("spec", "lib"), which on Windows joins with a backslash, producing the text spec\lib. Embedded directly into a regex pattern, \l is not a valid Python regex escape sequence:

re.sub(r"\b{}\b".format(os.path.join("spec", "lib")), "lib", file_path)
re.error: bad escape \l at position 6

This is reached whenever the toggle command's fast-guess locations don't exist on disk and it falls back to this third guess — a normal, common case, not an edge case.

Repro steps (Windows)

  1. Open a Ruby file whose matching spec (or vice versa) isn't at one of the two fast-guess locations.
  2. Run "Toggle Spec/Source".
  3. The traceback above appears as soon as the third path guess is evaluated.

Confirmed live on Windows against the real installed code by calling code_paths() directly on an ordinary file path.

Suggested fix

Build that particular pattern with a literal / instead of os.path.join (regex patterns don't care about OS path separators, only real filesystem calls do), matching how the other two guesses in the same function are already written.

dpc00 avatar Sep 14 '26 21:09 dpc00

Thanks for the report!

I've just merged a fix, it will go out in 3.1.1. It takes Package Control a few hours to list the new version.

guille avatar Sep 15 '26 12:09 guille