Skip to content

Commit

Permalink
fix: correctly resolve hooks entry file when directory exists (#13144)
Browse files Browse the repository at this point in the history
fixes #13100

This PR adds a check to see if an index file exists in the related directory before resolving to that path. Directories without an index file should correctly fallback to the non-directory path.
  • Loading branch information
eltigerchino authored Dec 11, 2024
1 parent 33600ee commit 11a9f66
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-bats-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: correctly resolve hooks file when a similarly named directory exists
5 changes: 3 additions & 2 deletions packages/kit/src/utils/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,9 @@ export function from_fs(str) {
export function resolve_entry(entry) {
if (fs.existsSync(entry)) {
const stats = fs.statSync(entry);
if (stats.isDirectory()) {
return resolve_entry(path.join(entry, 'index'));
const index = path.join(entry, 'index');
if (stats.isDirectory() && fs.existsSync(index)) {
return resolve_entry(index);
}

return entry;
Expand Down
7 changes: 7 additions & 0 deletions packages/kit/src/utils/filesystem.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,10 @@ test('ignores hooks.server folder when resolving hooks', () => {

expect(resolve_entry(source_dir + '/hooks')).null;
});

test('ignores hooks folder that has no index file when resolving hooks', () => {
write('hooks/not-index.js', '');
write('hooks.js', '');

expect(resolve_entry(source_dir + '/hooks')).toBe(source_dir + '/hooks');
});

0 comments on commit 11a9f66

Please sign in to comment.