fix(dashboard): recent_files section not displaying files without cwd parameter (#2284)

## Problem

The `recent_files` section in the dashboard was not displaying any files
when called without a `cwd` parameter, which is the default usage in
most dashboard examples and documentation.

```lua
-- This would show an empty section after the bug was introduced
{ section = "recent_files", limit = 8 }
```

## Root Cause

When `opts.cwd` was not provided, the code set `root = ""` (empty
string) and passed it to the oldfiles filter:

```lua
local root = opts.cwd and svim.fs.normalize(...) or ""
for file in M.oldfiles({ filter = { [root] = true } }) do
```

The filter logic in `M.oldfiles` checks if a file path starts with the
filter path **AND** if the character immediately after that prefix is a
directory separator (`/` or `\`). With an empty string as the filter
path:

1. `file:sub(1, 0) == ""` is always `true`
2. But `file:sub(1, 1):find("[/\\]")` is `false` for most files (unless
they start with `/` or `\`)
3. This results in `matches = false`, causing all files to be filtered
out

## Solution

Changed the logic to use `nil` instead of an empty string when no `cwd`
is specified:

```lua
local root = opts.cwd and svim.fs.normalize(opts.cwd == true and vim.fn.getcwd() or opts.cwd) or nil
-- Only filter by directory when root is specified. If nil, M.oldfiles will use default filters only (excludes stdpath data/cache/state).
local oldfiles_opts = root and { filter = { [root] = true } } or nil
local ret = {} ---@type snacks.dashboard.Section
for file in M.oldfiles(oldfiles_opts) do
```

When no filter is passed, `M.oldfiles()` uses only its default filters
(excluding stdpath data/cache/state directories), which is the intended
behavior for showing all recent files.

## Testing

All usage scenarios now work correctly:

| Scenario | Code | Behavior |
|----------|------|----------|
| No cwd | `{ section = "recent_files" }` |  Shows all recent files
(except stdpath) |
| Current dir | `{ section = "recent_files", cwd = true }` |  Shows
files in cwd |
| Specific dir | `{ section = "recent_files", cwd = "/path" }` |  Shows
files in specified path |

## Impact

This is a minimal, surgical fix (4 lines changed) that restores the
expected behavior documented in all dashboard examples without affecting
any other functionality.

Fixes the issue where users reported empty recent_files sections after
updating to the latest version.

<!-- START COPILOT CODING AGENT SUFFIX -->



<details>

<summary>Original prompt</summary>

> 
> ----
> 
> *This section details on the original issue you should resolve*
> 
> <issue_title>bug: dashboard does not display "recent_files"
section</issue_title>
> <issue_description>### Did you check docs and existing issues?
> 
> - [x] I have read all the snacks.nvim docs
> - [x] I have updated the plugin to the latest version before
submitting this issue
> - [x] I have searched the existing issues of snacks.nvim
> - [x] I have searched the existing issues of plugins related to this
issue
> 
> ### Neovim version (nvim -v)
> 
> 0.11 
> 
> ### Operating system/version
> 
> arch linux
> 
> ### Describe the bug
> 
> after updating, snacks.dashboard stopped displaying the "recent_files"
section.
> Looking through the commit history, I found that commit `5c4365e` is
relevant to that section. Upon rolling the plugin back to one commit
prior to it (commit `a4de830`), the section displayed as normal.
> 
> ### Steps To Reproduce
> 
> update to latest commit
> 
> ### Expected Behavior
> 
> section "recent files" displayed as per the documentations
> 
> ### Repro
> 
> ```lua
> vim.env.LAZY_STDPATH = ".repro"
> load(vim.fn.system("curl -s
https://raw.githubusercontent.com/folke/lazy.nvim/main/bootstrap.lua"))()
> 
> require("lazy.minit").repro({
>   spec = {
>     { "folke/snacks.nvim", opts = {} },
>     -- add any other plugins here
>   },
> })
> ```</issue_description>
> 
> ## Comments on the Issue (you are @copilot in this section)
> 
> <comments>
> </comments>
> 


</details>

Fixes folke/snacks.nvim#2283

<!-- START COPILOT CODING AGENT TIPS -->
---

💬 Share your feedback on Copilot coding agent for the chance to win a
$200 gift card! Click
[here](https://survey3.medallia.com/?EAHeSx-AP01bZqG0Ld9QLQ) to start
the survey.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: folke <292349+folke@users.noreply.github.com>
This commit is contained in:
Copilot 2025-10-19 22:17:03 +02:00 committed by GitHub
parent d6e34b158d
commit 1ed737e465
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -855,9 +855,11 @@ function M.sections.recent_files(opts)
return function()
opts = opts or {}
local limit = opts.limit or 5
local root = opts.cwd and svim.fs.normalize(opts.cwd == true and vim.fn.getcwd() or opts.cwd) or ""
local root = opts.cwd and svim.fs.normalize(opts.cwd == true and vim.fn.getcwd() or opts.cwd) or nil
-- Only filter by directory when root is specified. If nil, M.oldfiles will use default filters only (excludes stdpath data/cache/state).
local oldfiles_opts = root and { filter = { [root] = true } } or nil
local ret = {} ---@type snacks.dashboard.Section
for file in M.oldfiles({ filter = { [root] = true } }) do
for file in M.oldfiles(oldfiles_opts) do
if not opts.filter or opts.filter(file) then
ret[#ret + 1] = {
file = file,