graphify icon indicating copy to clipboard operation
graphify copied to clipboard

Python rationale extractor doesn't strip \r — breaks Obsidian export on Windows/WSL

Open luidpivetta opened this issue 7 months ago • 1 comments

Bug: Python rationale extractor doesn't strip \r (carriage return) — breaks Obsidian export on Windows/WSL

Environment

  • Platform: WSL2 (Linux) processing Windows files mounted at /mnt/c/
  • Python files in the corpus have CRLF line endings (\r\n)
  • graphify v0.3.27 (latest)

Bug location

graphify/extract.py line 1024:

def _add_rationale(text: str, line: int, parent_nid: str) -> None:
    label = text[:80].replace("\n", " ").strip()

The code only replaces \n but not \r. On Windows/WSL, Python docstrings have \r\n line endings. After replace("\n", " "), the \r characters remain in the label.

Impact

On a 1187-file corpus (Windows/WSL):

  • 401 nodes ended up with \r characters in their labels
  • 233 nodes had multi-character labels with embedded carriage returns

This breaks the Obsidian vault export (to_obsidian):

  • to_obsidian.safe_name() removes \\ / * ? : " < > | # ^ [ ] but does NOT strip \r
  • Filenames containing \r cause:
    • Filesystem write inconsistencies on case-insensitive filesystems
    • Wikilinks like [[Inicializa atualizador\r ...]] that don't resolve to any file
    • When Obsidian's "Only existing files" filter is enabled, hundreds of nodes appear missing

Reproduction

from graphify.extract import extract
from pathlib import Path

# Any Python file with CRLF line endings and a multi-line docstring
result = extract([Path("file_with_crlf.py")])
for node in result["nodes"]:
    if "\r" in node.get("label", ""):
        print(repr(node["label"]))

Suggested fix

def _add_rationale(text: str, line: int, parent_nid: str) -> None:
    label = text[:80].replace("\r\n", " ").replace("\n", " ").replace("\r", " ").strip()
    # OR using regex
    # label = re.sub(r'[\r\n\t]+', ' ', text[:80]).strip()

Same fix should be applied in to_obsidian.safe_name() for defense in depth, since user-provided labels (from semantic extraction) may also contain \r.

Workaround

Manually sanitize labels in graph.json after extraction:

import re, json
from networkx.readwrite import json_graph
data = json.load(open("graphify-out/graph.json"))
G = json_graph.node_link_graph(data, edges="links")
for n, d in G.nodes(data=True):
    d["label"] = re.sub(r"[\r\n\t]+", " ", d.get("label", "")).strip()
json.dump(json_graph.node_link_data(G, edges="links"), open("graphify-out/graph.json", "w"))

Severity

Medium - affects all Windows/WSL users running graphify against repos with CRLF files. Silent failure mode (nodes appear in graph.json but break Obsidian view).

luidpivetta avatar Apr 10 '26 01:04 luidpivetta

Fixed in 0.3.28. Rationale labels now strip carriage returns before storing, and safe_name in the Obsidian exporter also strips them as a second layer. Upgrade with pip install --upgrade graphifyy

safishamsi avatar Apr 10 '26 09:04 safishamsi