Skip to content

fix(app): handle empty path in file list to avoid leading slash#233

Closed
shuv1337 wants to merge 0 commit intodevfrom
fix/root-file-view
Closed

fix(app): handle empty path in file list to avoid leading slash#233
shuv1337 wants to merge 0 commit intodevfrom
fix/root-file-view

Conversation

@shuv1337
Copy link
Collaborator

@shuv1337 shuv1337 commented Dec 31, 2025

Summary

  • Fix file list API call to handle empty/root path correctly, avoiding leading slash in path
  • Always call list for parent directory in file fetch, even when parent is empty string (root)

These changes ensure proper file listing at the project root level.

Greptile Summary

Fixed file listing at project root by handling empty path correctly. The changes ensure that when listing files in the root directory, an empty string is passed to the API instead of a leading slash ("/"), and the fetch function always calls list for the parent directory, even when the parent is the root (empty string).

Key changes:

  • Modified list function to conditionally add trailing slash: uses empty string for root, adds "/" suffix for non-empty paths
  • Removed conditional check in fetch function that prevented listing root directory when parent was empty
  • Prevents malformed paths like "/" from being sent to the file listing API

The fix aligns with the backend File.list implementation which expects an optional directory parameter that can be empty for root listings.

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The changes are minimal, focused, and correctly address a specific path-handling bug. The fix properly handles the edge case of empty/root paths by conditionally formatting the path parameter. The logic aligns with the backend File.list implementation which accepts an optional dir parameter. No security issues, breaking changes, or unintended side effects identified.
  • No files require special attention

Important Files Changed

Filename Overview
packages/app/src/context/local.tsx Fixed root directory file listing by handling empty path correctly (avoiding leading slash) and removing unnecessary parent check in fetch function

Sequence Diagram

sequenceDiagram
    participant User
    participant FileContext as File Context
    participant SDK as SDK Client
    participant Server as File Server

    Note over User,Server: Scenario: Fetching a file at root level

    User->>FileContext: fetch(path)
    FileContext->>FileContext: relativePath = relative(path)
    FileContext->>FileContext: parent = relativePath.split("/").slice(0, -1).join("/")
    Note over FileContext: parent is empty string "" for root files
    FileContext->>FileContext: list(parent)
    
    alt Before Fix: parent was ""
        FileContext->>FileContext: Check if (parent) - FALSE
        Note over FileContext: list() not called, directory not loaded!
    else After Fix: parent is ""
        FileContext->>SDK: file.list({ path: "" })
        Note over SDK: listPath = "" ? "" + "/" : "" = ""
        SDK->>Server: GET /file?path=""
        Server->>SDK: Returns root directory contents
        SDK->>FileContext: Update store with file nodes
    end

    Note over User,Server: Scenario: Listing root directory

    User->>FileContext: list("")
    
    alt Before Fix
        FileContext->>SDK: file.list({ path: "" + "/" })
        Note over SDK: Results in path: "/" (leading slash issue)
        SDK->>Server: GET /file?path="/"
        Note over Server: May cause incorrect path resolution
    else After Fix
        FileContext->>FileContext: listPath = "" ? "" : "" + "/"
        Note over FileContext: listPath = ""
        FileContext->>SDK: file.list({ path: "" })
        SDK->>Server: GET /file?path=""
        Server->>FileContext: Returns correct root listing
    end
Loading

@shuv1337
Copy link
Collaborator Author

Closing this PR as the changes were exposing a pre-existing upstream bug. The root cause needs to be investigated separately - it appears to be related to running opencode serve from a deeply nested directory within a project.

@shuv1337 shuv1337 closed this Dec 31, 2025
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