[Bug]: C#: a Windows-1252 file with any `#` directive is parsed as raw bytes again (regression of #4145 since 0.9.103)
Pre-flight checks
- [x] I have checked the Troubleshooting section in the README
What happened?
#4146 made every source read go through _read_source_bytes, so a Windows-1252 file is decoded before tree-sitter sees it. Since 0.9.103 (#5003) this no longer holds for C# files that contain a preprocessor directive. A lone #region is enough.
extract_csharp (graphify/extract.py) reads the file with path.read_bytes() and passes the result of _blank_csharp_preprocessor as source_override. When the file has a directive, that override is not None, so _extract_generic parses it and never calls _read_source_bytes. tree-sitter gets the raw Windows-1252 bytes:
- an identifier with a non-ASCII letter is cut at that letter (
Pr\u00fcfebecomesPr), as before #4146; - if such an identifier is used inside a method body, the parse can fail from that line on. The rest of the type is lost: in the reproducer,
Pr\u00fcfe()disappears completely, and graphify reports a syntax error; - the "is not valid UTF-8; read it as cp1252" warning is not printed for these files.
Files without a directive are not affected. In my tests UTF-16 files with directives were not affected either: the directive scan does not find # in UTF-16 bytes, so no override is created.
Expected: the file with #region gives the same four methods (Last, Next, Pr\u00fcfe, Run) as the file without it, and the warning is printed for both.
Scale. Over 8 private C# code bases: 265 C# files are not UTF-8 (Windows-1252), 193 of them contain a directive. 3 files lose names. In one file, 33 of 67 methods are missing; the parse error starts at the line that calls a method with an umlaut and runs for about 1,700 lines. In the other two files, method names are cut short. The remaining files have non-ASCII text only in comments or strings, where the raw bytes do no visible harm.
Steps to reproduce
mkdir -p cs-ansi && cd cs-ansi
cat > make.py <<'EOF'
# Writes the same C# file twice: Windows-1252 ("ANSI") with and without a #region.
src = """namespace Shop
{
public class Order
{
#region checks
public bool Run() { return Pr\u00fcfe() && Next(); }
public bool Pr\u00fcfe() { return Next(); }
public bool Next() { return Last(); }
public bool Last() { return true; }
#endregion
}
}
"""
import os
os.makedirs("with-region", exist_ok=True)
os.makedirs("without-region", exist_ok=True)
open("with-region/Order.cs", "wb").write(src.encode("cp1252"))
plain = "".join(l for l in src.splitlines(True) if "#" not in l)
open("without-region/Order.cs", "wb").write(plain.encode("cp1252"))
EOF
cat > show.py <<'EOF'
import sys
from pathlib import Path
from graphify.extract import extract
r = extract([Path(sys.argv[1]) / "Order.cs"])
labels = {n["id"]: n["label"] for n in r["nodes"]}
print(sys.argv[1], ascii(sorted(l for l in labels.values() if l.endswith("()"))))
EOF
python make.py
python show.py without-region
python show.py with-region
Error output or graph output
0.9.108:
[graphify] WARNING: without-region\Order.cs is not valid UTF-8; read it as cp1252. Re-save it as UTF-8 if names in the graph look wrong.
without-region ['.Last()', '.Next()', '.Pr\xfcfe()', '.Run()']
warning: 1 file(s) had syntax errors and may be partially extracted: Order.cs (error region at line 6, 5 symbol(s) extracted)
with-region ['.Last()', '.Next()', '.Run()']
0.9.102 (before #5003), same files:
[graphify] WARNING: without-region\Order.cs is not valid UTF-8; read it as cp1252. Re-save it as UTF-8 if names in the graph look wrong.
without-region ['.Last()', '.Next()', '.Pr\xfcfe()', '.Run()']
[graphify] WARNING: with-region\Order.cs is not valid UTF-8; read it as cp1252. Re-save it as UTF-8 if names in the graph look wrong.
with-region ['.Last()', '.Next()', '.Pr\xfcfe()', '.Run()']
Graphify version
0.9.108
Operating System
Windows
Python Version
3.13
Installation Method
pip
Additional Environment Details
Reproduced in a fresh virtual environment with only graphifyy==0.9.108 from PyPI. 0.9.102 and 0.9.103 were checked the same way. The bytes are written explicitly by make.py, so the result does not depend on the platform.
Additional context
The other extractors that pass a source_override (JS, Svelte, Astro, EJS, Vue, C++, PHP) already read through _read_source_bytes / _read_source_text. extract_csharp is the only one that reads with path.read_bytes(). Before #5003 this only mattered in the rare case where _normalize_csharp_verbatim_interpolation returned an override (a verbatim interpolated string that ends with a hole and an escaped quote, #3376); otherwise the override was None and _extract_generic decoded the file itself.
The linked pull request changes that one read to _read_source_bytes(path). The blanking is byte-length preserving on the decoded UTF-8 bytes, so line numbers stay correct.
Thanks for opening this issue, @JensD-git. A maintainer will take a look soon.
If you would like to discuss it in real time, come say hi on our Discord server. For longer-form questions and ideas there is also GitHub Discussions.
To help us triage, please make sure the report includes what you expected, what actually happened, and the steps (and a small sample) to reproduce it.