From 11a9f66922199ee5925cc71c0efc513376753754 Mon Sep 17 00:00:00 2001 From: Tee Ming Date: Wed, 11 Dec 2024 21:57:31 +0800 Subject: [PATCH] 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. --- .changeset/brown-bats-mate.md | 5 +++++ packages/kit/src/utils/filesystem.js | 5 +++-- packages/kit/src/utils/filesystem.spec.js | 7 +++++++ 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 .changeset/brown-bats-mate.md 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'); +});