Repository: sveltejs/kit (JavaScript)

Issue:
[bug] Build errors swallowed by `finalise is not a function`
### Describe the bug

After the latest update, all build-time errors are swallowed and replaced with:

```rust
[vite-plugin-sveltekit-compile] finalise is not a function
error during build:
TypeError: finalise is not a function
```

Seems to be coming from: https://github.com/sveltejs/kit/blame/089c405041b0267356281fc8ea5f19ecc331178b/packages/kit/src/exports/vite/index.js#L728

### Reproduction

I just changed an import path in `+page.svelte` to a non-existent one (`./ASDF/Counter.svelte`) to repro:

https://stackblitz.com/edit/sveltejs-kit-template-default-xnumuj?file=src/routes/+page.svelte


### Logs

```Shell
✓ 141 modules transformed.
transforming (145) src/styles/prism.css4:11:11 PM [vite-plugin-svelte] ssr compile done.
package                 files    time      avg
@REDACTED/REDACTED       40   0.53s   13.3ms
[vite-plugin-sveltekit-compile] finalise is not a function
error during build:
TypeError: finalise is not a function
    at Object.handler (file:///Users/REDACTED/dev/Projects/REDACTED/REDACTED/REDACTED/node_modules/.pnpm/@sveltejs+kit@1.3.7_svelte@3.55.1+vite@4.0.4/node_modules/@sveltejs/kit/src/exports/vite/index.js:722:11)
```


### System Info

```Shell
System:
    OS: macOS 13.2
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
    Memory: 27.92 GB / 64.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.19.0 - ~/.volta/tools/image/node/16.19.0/bin/node
    Yarn: 1.22.18 - ~/.volta/tools/image/yarn/1.22.18/bin/yarn
    npm: 8.19.3 - ~/.volta/tools/image/node/16.19.0/bin/npm
  Browsers:
    Brave Browser: 109.1.47.186
    Chrome: 111.0.5562.0
    Chrome Canary: 112.0.5572.1
    Edge: 83.0.478.44
    Firefox: 89.0
    Firefox Developer Edition: 106.0
    Firefox Nightly: 76.0a1
    Safari: 16.3
    Safari Technology Preview: 16.4
  npmPackages:
    @sveltejs/adapter-vercel: 1.0.6 => 1.0.6
    @sveltejs/kit: 1.3.7 => 1.3.7
    @sveltejs/package: 1.0.2 => 1.0.2
    svelte: 3.55.1 => 3.55.1
    vite: 4.0.4 => 4.0.4
```


### Severity

serious, but I can work around it

### Additional Information

_No response_


Discussion:
an unexpected result of https://github.com/sveltejs/kit/pull/8817. looks like reinstating the `?.` fixes it:

```diff
-await finalise();
+await finalise?.();
```
Yea was about to submit a pr with that change 👍



Patch:
diff --git a/.changeset/grumpy-islands-collect.md b/.changeset/grumpy-islands-collect.md
new file mode 100644
index 000000000000..25e3ab4c7740
--- /dev/null
+++ b/.changeset/grumpy-islands-collect.md
@@ -0,0 +1,5 @@
+---
+'@sveltejs/kit': patch
+---
+
+fix: preserve build error messages
diff --git a/packages/kit/src/exports/vite/index.js b/packages/kit/src/exports/vite/index.js
index 57fd9dd7dd03..6d7a639c0d2b 100644
--- a/packages/kit/src/exports/vite/index.js
+++ b/packages/kit/src/exports/vite/index.js
@@ -725,7 +725,7 @@ function kit({ svelte_config }) {
 			sequential: true,
 			async handler() {
 				if (!vite_config.build.ssr) return;
-				await finalise();
+				await finalise?.();
 			}
 		}
 	};
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 27a0542a8f24..f0be98413f85 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -574,6 +574,20 @@ importers:
       typescript: 4.9.4
       vite: 4.0.4
 
+  packages/kit/test/build-errors/apps/syntax-error:
+    specifiers:
+      '@sveltejs/kit': workspace:^
+      svelte: ^3.55.1
+      svelte-check: ^3.0.2
+      typescript: ^4.9.4
+      vite: ^4.0.4
+    devDependencies:
+      '@sveltejs/kit': link:../../../..
+      svelte: 3.55.1
+      svelte-check: 3.0.2_svelte@3.55.1
+      typescript: 4.9.4
+      vite: 4.0.4
+
   packages/kit/test/prerendering/basics:
     specifiers:
       '@sveltejs/kit': workspace:^


Test patch:
diff --git a/packages/kit/test/build-errors/apps/syntax-error/.gitignore b/packages/kit/test/build-errors/apps/syntax-error/.gitignore
new file mode 100644
index 000000000000..cbee71f70e92
--- /dev/null
+++ b/packages/kit/test/build-errors/apps/syntax-error/.gitignore
@@ -0,0 +1,7 @@
+.DS_Store
+node_modules
+/build
+/.svelte-kit
+/package
+.env.*
+!.env.example
diff --git a/packages/kit/test/build-errors/apps/syntax-error/.npmrc b/packages/kit/test/build-errors/apps/syntax-error/.npmrc
new file mode 100644
index 000000000000..b6f27f135954
--- /dev/null
+++ b/packages/kit/test/build-errors/apps/syntax-error/.npmrc
@@ -0,0 +1,1 @@
+engine-strict=true
diff --git a/packages/kit/test/build-errors/apps/syntax-error/package.json b/packages/kit/test/build-errors/apps/syntax-error/package.json
new file mode 100644
index 000000000000..0d57ac6c90b6
--- /dev/null
+++ b/packages/kit/test/build-errors/apps/syntax-error/package.json
@@ -0,0 +1,18 @@
+{
+	"name": "syntax-error",
+	"version": "0.0.1",
+	"private": true,
+	"scripts": {
+		"dev": "vite dev",
+		"build": "vite build",
+		"preview": "vite preview"
+	},
+	"devDependencies": {
+		"@sveltejs/kit": "workspace:^",
+		"svelte": "^3.55.1",
+		"svelte-check": "^3.0.2",
+		"typescript": "^4.9.4",
+		"vite": "^4.0.4"
+	},
+	"type": "module"
+}
diff --git a/packages/kit/test/build-errors/apps/syntax-error/src/app.html b/packages/kit/test/build-errors/apps/syntax-error/src/app.html
new file mode 100644
index 000000000000..5b53ef7e3ae7
--- /dev/null
+++ b/packages/kit/test/build-errors/apps/syntax-error/src/app.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html lang="en">
+	<head>
+		<meta charset="utf-8" />
+		<link rel="icon" href="%sveltekit.assets%/favicon.png" />
+		<meta name="viewport" content="width=device-width" />
+		%sveltekit.head%
+	</head>
+	<body>
+		<div>%sveltekit.body%</div>
+	</body>
+</html>
diff --git a/packages/kit/test/build-errors/apps/syntax-error/src/lib/test.server.js b/packages/kit/test/build-errors/apps/syntax-error/src/lib/test.server.js
new file mode 100644
index 000000000000..770268db581d
--- /dev/null
+++ b/packages/kit/test/build-errors/apps/syntax-error/src/lib/test.server.js
@@ -0,0 +1,1 @@
+export const should_explode = 'boom';
diff --git a/packages/kit/test/build-errors/apps/syntax-error/src/routes/+page.svelte b/packages/kit/test/build-errors/apps/syntax-error/src/routes/+page.svelte
new file mode 100644
index 000000000000..514b2627654e
--- /dev/null
+++ b/packages/kit/test/build-errors/apps/syntax-error/src/routes/+page.svelte
@@ -0,0 +1,1 @@
+<div
\ No newline at end of file
diff --git a/packages/kit/test/build-errors/apps/syntax-error/static/favicon.png b/packages/kit/test/build-errors/apps/syntax-error/static/favicon.png
new file mode 100644
index 000000000000..825b9e65af7c
Binary files /dev/null and b/packages/kit/test/build-errors/apps/syntax-error/static/favicon.png differ
diff --git a/packages/kit/test/build-errors/apps/syntax-error/svelte.config.js b/packages/kit/test/build-errors/apps/syntax-error/svelte.config.js
new file mode 100644
index 000000000000..bded48544036
--- /dev/null
+++ b/packages/kit/test/build-errors/apps/syntax-error/svelte.config.js
@@ -0,0 +1,4 @@
+/** @type {import('@sveltejs/kit').Config} */
+const config = {};
+
+export default config;
diff --git a/packages/kit/test/build-errors/apps/syntax-error/tsconfig.json b/packages/kit/test/build-errors/apps/syntax-error/tsconfig.json
new file mode 100644
index 000000000000..3688e97000b5
--- /dev/null
+++ b/packages/kit/test/build-errors/apps/syntax-error/tsconfig.json
@@ -0,0 +1,19 @@
+{
+	"extends": "./.svelte-kit/tsconfig.json",
+	"compilerOptions": {
+		"allowJs": true,
+		"checkJs": true,
+		"esModuleInterop": true,
+		"forceConsistentCasingInFileNames": true,
+		"resolveJsonModule": true,
+		"skipLibCheck": true,
+		"sourceMap": true,
+		"strict": true,
+		"noEmit": true,
+		"paths": {
+			"types": ["../../../../types/internal"],
+			"$lib": ["./src/lib"],
+			"$lib/*": ["./src/lib/*"]
+		}
+	}
+}
diff --git a/packages/kit/test/build-errors/apps/syntax-error/vite.config.js b/packages/kit/test/build-errors/apps/syntax-error/vite.config.js
new file mode 100644
index 000000000000..95a8fe5795c7
--- /dev/null
+++ b/packages/kit/test/build-errors/apps/syntax-error/vite.config.js
@@ -0,0 +1,14 @@
+import { sveltekit } from '@sveltejs/kit/vite';
+import path from 'path';
+
+/** @type {import('vite').UserConfig} */
+const config = {
+	plugins: [sveltekit()],
+	server: {
+		fs: {
+			allow: [path.resolve('../../../../src')]
+		}
+	}
+};
+
+export default config;
diff --git a/packages/kit/test/build-errors/syntax-error.js b/packages/kit/test/build-errors/syntax-error.js
new file mode 100644
index 000000000000..51e8eac11b72
--- /dev/null
+++ b/packages/kit/test/build-errors/syntax-error.js
@@ -0,0 +1,23 @@
+import { test } from 'uvu';
+import * as assert from 'uvu/assert';
+import { execSync } from 'node:child_process';
+import path from 'node:path';
+
+test('$lib/*.server.* is not statically importable from the client', () => {
+	try {
+		execSync('pnpm build', {
+			cwd: path.join(process.cwd(), 'apps/syntax-error'),
+			stdio: 'pipe',
+			timeout: 60000
+		});
+	} catch (err) {
+		assert.ok(
+			err.message.includes('Unexpected end of input'),
+			`received unexpected exception message ${err.message}`
+		);
+		return;
+	}
+	assert.unreachable();
+});
+
+test.run();