Skip to content

fix(file_tree): keep file and directory nodes with the same name apart - #380

Open
rainerborene wants to merge 1 commit into
dlyongemallo:mainfrom
rainerborene:fix/file-tree-file-dir-name-collision
Open

rainerborene wants to merge 1 commit into
dlyongemallo:mainfrom
rainerborene:fix/file-tree-file-dir-name-collision

Conversation

@rainerborene

Copy link
Copy Markdown

Problem

:DiffviewOpen crashes when the diff contains both a path and entries below it. The common way to hit this is replacing a directory with a symlink while the files it used to contain are deleted in the same change:

A  .claude/commands
D  .claude/commands/commit.md
render.lua:36: attempt to concatenate local 'name' (a function value)
stack traceback:
        render.lua: in function 'format_folder_name'
        render.lua:297: in function 'render_file_tree_recurse'
        ...

Same root cause as upstream sindrets#523 ("Diffview cannot be shown when file is changed into folder").

Cause

FileTree:add_file_entry() looks intermediate directories up in node.children by name only. When the commands file entry is added first, the later commands/commit.md entry reuses that file node as its parent. The renderer then receives a FileEntry where it expects DirData, and ctx.name resolves to the inherited Object:name() class method instead of a string. With the opposite insertion order the file entry is silently dropped instead.

Fix

  • Node gets a key field used for sibling lookup in add_child(). It defaults to name, so existing callers are unaffected.
  • FileTree keys directory nodes as name .. "/". A path component can never contain the separator, so a file and a directory of the same name can now coexist under one parent.
  • Sorting, rendering and collapsed-state tracking keep using name and path, so output for existing trees is unchanged.

Tests

New file_tree_spec.lua covers both insertion orders: the tree keeps a separate leaf and directory node, create_comp_schema() yields string names for every directory context, statuses are derived from children only, and flatten_dirs still behaves. It also pins the default key == name behaviour and the existing directory dedup.

Locally: make test passes apart from the hg_* / jj_* specs, which fail identically on main here because those binaries are not installed. stylua --check and make type-check are clean.

Verified against the real repo that triggered the crash. The panel now shows the commands/ folder with its deleted file and the new commands symlink side by side:

  .claude/
    commands/
D       commit.md 0, 1
    skills/
A       agent-browser 1, 0
A     commands 1, 0

A diff can contain both a path and entries below it, e.g. when a
directory is replaced by a symlink while the files it used to contain
are deleted in the same change:

    A  .claude/commands
    D  .claude/commands/commit.md

`FileTree:add_file_entry()` looked intermediate directories up by name
only, so the `commands` file node was reused as the parent of
`commit.md`. The renderer then received a `FileEntry` where it expected
`DirData`, and `ctx.name` resolved to the inherited `Object:name()`
class method:

    render.lua:36: attempt to concatenate local 'name' (a function value)

Give `Node` a lookup key separate from its display name and key
directory nodes with a trailing separator, so a file and a directory of
the same name can coexist under one parent. Sorting and rendering keep
using `name`, so the panel output is unchanged for existing trees.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant