Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/empty-mugs-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: correctly resolve no hooks file when a similarly named directory exists
24 changes: 13 additions & 11 deletions packages/kit/src/utils/filesystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,24 @@ export function resolve_entry(entry) {
if (fs.existsSync(entry)) {
const stats = fs.statSync(entry);
const index = path.join(entry, 'index');
if (stats.isDirectory() && fs.existsSync(index)) {

if (stats.isFile()) {
return entry;
} else if (fs.existsSync(index)) {
return resolve_entry(index);
}
}

return entry;
} else {
const dir = path.dirname(entry);

if (fs.existsSync(dir)) {
const base = path.basename(entry);
const files = fs.readdirSync(dir);
const dir = path.dirname(entry);

const found = files.find((file) => file.replace(/\.(js|ts)$/, '') === base);
if (fs.existsSync(dir)) {
const base = path.basename(entry);
const files = fs.readdirSync(dir);
const found = files.find((file) => {
return file.replace(/\.(js|ts)$/, '') === base && fs.statSync(path.join(dir, file)).isFile();
});

if (found) return path.join(dir, found);
}
if (found) return path.join(dir, found);
}

return null;
Expand Down
18 changes: 15 additions & 3 deletions packages/kit/src/utils/filesystem.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,26 @@ test('replaces strings', () => {
});

test('ignores hooks.server folder when resolving hooks', () => {
write('hooks.server/index.js', '');
write(join('hooks.server', 'index.js'), '');

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(join('hooks', 'not-index.js'), '');
write('hooks.js', '');

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

test('ignores hooks folder when resolving universal hooks', () => {
write(join('hooks', 'hooks.server.js'), '');

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

test('resolves entries that have an extension', () => {
write('hooks.js', '');

expect(resolve_entry(join(source_dir, 'hooks.js'))).toBe(join(source_dir, 'hooks.js'));
});
Loading