Skip to content

Commit 11a9f66

Browse files
authored
fix: correctly resolve hooks entry file when directory exists (#13144)
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.
1 parent 33600ee commit 11a9f66

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

.changeset/brown-bats-mate.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
fix: correctly resolve hooks file when a similarly named directory exists

packages/kit/src/utils/filesystem.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,9 @@ export function from_fs(str) {
169169
export function resolve_entry(entry) {
170170
if (fs.existsSync(entry)) {
171171
const stats = fs.statSync(entry);
172-
if (stats.isDirectory()) {
173-
return resolve_entry(path.join(entry, 'index'));
172+
const index = path.join(entry, 'index');
173+
if (stats.isDirectory() && fs.existsSync(index)) {
174+
return resolve_entry(index);
174175
}
175176

176177
return entry;

packages/kit/src/utils/filesystem.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,10 @@ test('ignores hooks.server folder when resolving hooks', () => {
105105

106106
expect(resolve_entry(source_dir + '/hooks')).null;
107107
});
108+
109+
test('ignores hooks folder that has no index file when resolving hooks', () => {
110+
write('hooks/not-index.js', '');
111+
write('hooks.js', '');
112+
113+
expect(resolve_entry(source_dir + '/hooks')).toBe(source_dir + '/hooks');
114+
});

0 commit comments

Comments
 (0)