Skip to content

Commit

Permalink
fix: prevent crash on errors in watch mode
Browse files Browse the repository at this point in the history
  • Loading branch information
gtm-nayan committed Aug 26, 2023
1 parent 45fd223 commit 140135c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-poems-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/package': patch
---

fix: prevent crash on errors in watch mode
25 changes: 20 additions & 5 deletions packages/package/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ export async function watch(options) {
const events = pending.slice();
pending.length = 0;

let errored = false;

for (const { file, type } of events) {
if (type === 'unlink') {
for (const candidate of [
Expand All @@ -116,14 +118,27 @@ export async function watch(options) {

if (type === 'add' || type === 'change') {
console.log(`Processing ${file.name}`);
await process_file(input, output, file, options.config.preprocess, alias, analyse_code);
validate();
try {
await process_file(input, output, file, options.config.preprocess, alias, analyse_code);
} catch (e) {
errored = true;
console.error(e);
}
}
}

if (!errored && options.types) {
try {
await emit_dts(input, output, options.cwd, alias, files);
console.log('Updated .d.ts files');
} catch (e) {
errored = true;
console.error(e);
}
}

if (options.types) {
await emit_dts(input, output, options.cwd, alias, files);
console.log('Updated .d.ts files');
if (!errored) {
validate();
}

console.log(message);
Expand Down
10 changes: 10 additions & 0 deletions packages/package/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,22 @@ if (!process.env.CI) {
await settled();
compare('Test.svelte');
compare('Test.svelte.d.ts');

// doesn't crash on an error
write('src/lib/post-error.svelte', '<button on:={foo}>click me</button>');
await settled();

// recovers on subsequent change
write('src/lib/post-error.svelte', '<button on:click={foo}>click me</button>');
await settled();
compare('post-error.svelte');
} finally {
watcher.close();

remove('src/lib/Test.svelte');
remove('src/lib/a.js');
remove('src/lib/b.ts');
remove('src/lib/post-error.svelte');
}
});
}
Expand Down
1 change: 1 addition & 0 deletions packages/package/test/watch/expected/post-error.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<button on:click={foo}>click me</button>

0 comments on commit 140135c

Please sign in to comment.