Skip to content

fix: keep the first root index when wildcard is false - #622

Closed
eknathdj wants to merge 1 commit into
fastify:mainfrom
eknathdj:fix/index-root-priority-wildcard-false
Closed

eknathdj wants to merge 1 commit into
fastify:mainfrom
eknathdj:fix/index-root-priority-wildcard-false

Conversation

@eknathdj

Copy link
Copy Markdown

Fixes #617

With wildcard: false and more than one root directory, a request for a directory was served from
the lowest-priority root instead of the highest — the opposite of the documented "first found,
first served" order.

indexDirs is keyed by directory path, but the guard looked up the route's basename:

const key = path.posix.basename(route)
if (indexes.has(key) && !indexDirs.has(key)) {
  indexDirs.set(path.posix.dirname(route), rootPath)
}

A filename is never a key in indexDirs, so !indexDirs.has(key) was always true and each later
root overwrote the entry for the same directory.

Change

Look up the directory the entry is actually stored under:

const key = path.posix.basename(route)
const dirname = path.posix.dirname(route)
if (indexes.has(key) && !indexDirs.has(dirname)) {
  indexDirs.set(dirname, rootPath)
}

Test

Added a test with two roots — root1/sub/index.htm and root2/sub/index.html,
index: ['index.htm', 'index.html'], wildcard: false — asserting that GET /sub/ serves root1's
file. It returns from root2 before the change and from root1 after.

Note on the local run

npm run test:unit reports 99.7% line coverage here, all of it from lib/dirList.js. That comes
from two symlink tests that cannot create symlinks without elevated privileges on Windows, and it
reproduces identically on an unmodified checkout. index.js is at 100% in both cases.

indexDirs is keyed by directory path, but the guard checked the route
basename, so it never matched and every later root overwrote the earlier
one for the same directory. With wildcard: false and multiple roots, a
directory request was served from the lowest-priority root instead of the
highest, against the documented first found, first served order.

Fixes fastify#617
SameRainbows

This comment was marked as spam.

@Tony133

Tony133 commented Nov 13, 2026

Copy link
Copy Markdown
Member

Thanks for your contribution! Closing as a duplicate: #617 is fixed by #618.

@Tony133 Tony133 closed this Nov 13, 2026
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.

bug: directory index route root priority is overwritten when wildcard is false

3 participants