diff --git a/.changeset/brown-bats-mate.md b/.changeset/brown-bats-mate.md new file mode 100644 index 000000000000..3b0c4b70dabe --- /dev/null +++ b/.changeset/brown-bats-mate.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: correctly resolve hooks file when a similarly named directory exists diff --git a/packages/kit/src/utils/filesystem.js b/packages/kit/src/utils/filesystem.js index ff83568e0358..58bcce22c7e3 100644 --- a/packages/kit/src/utils/filesystem.js +++ b/packages/kit/src/utils/filesystem.js @@ -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; diff --git a/packages/kit/src/utils/filesystem.spec.js b/packages/kit/src/utils/filesystem.spec.js index 443268314d50..e9ccf8d832a9 100644 --- a/packages/kit/src/utils/filesystem.spec.js +++ b/packages/kit/src/utils/filesystem.spec.js @@ -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'); +});