Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serving static files via hono on bun kills the prerenderer #28910

Closed
1 task
heddendorp opened this issue Nov 20, 2024 · 7 comments
Closed
1 task

Serving static files via hono on bun kills the prerenderer #28910

heddendorp opened this issue Nov 20, 2024 · 7 comments
Assignees
Labels
angular/build:application needs: more info Reporter must clarify the issue

Comments

@heddendorp
Copy link

heddendorp commented Nov 20, 2024

Command

build

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

I wanted to use the new hybrid rendering with hono and bun.
Everything seems to work mostly fine until I try to serve static files from my server. This kills my build process as the route extraction fails.
As long as the static serving is commented out, the build is fine and works with bun and the dev server.

Minimal Reproduction

I have a repro here https://github.com/heddendorp/ssr-bun-test
Cloning, running bun install and bun run build will give you the error.
You can get rid of this line
https://github.com/heddendorp/ssr-bun-test/blob/d55241783177d8a940fe713dc4371e80a0eccec6/src/server.ts#L28
to make the build work.

Exception or Error

Prerendered 0 static routes.
Application bundle generation failed. [3.477 seconds]

✘ [ERROR] An error occurred while extracting routes.

Error: No handler function exported from C:\Users\hedde\code\evorto\node_modules\@angular\build\src\utils\server-rendering\routes-extractor-worker.js
    at [...]node_modules\piscina\dist\worker.js:142:23

Your Environment

Angular CLI: 19.0.0
Node: 22.4.0
Package Manager: bun 1.1.36
OS: win32 x64

Angular: 19.0.0
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, platform-server
... router, ssr

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1900.0
@angular-devkit/build-angular   19.0.0
@angular-devkit/core            19.0.0
@angular-devkit/schematics      19.0.0
@schematics/angular             19.0.0
rxjs                            7.8.1
typescript                      5.6.3
zone.js                         0.15.0

Anything else relevant?

I'm happy to provide more context to clear this up

@alan-agius4
Copy link
Collaborator

alan-agius4 commented Nov 20, 2024

Are you using bun as your runtime, or are you using Node.js? If it's the latter, import { serveStatic } from 'hono/bun' will not be valid.

Unfortunately, piscina is intercepting all errors during the initialization phase. https://github.com/piscinajs/piscina/blob/b64747233446ca4271e6aed744a740f637e278a5/src/worker.ts#L57

@alan-agius4 alan-agius4 added needs: more info Reporter must clarify the issue and removed type: bug/fix labels Nov 20, 2024
@heddendorp
Copy link
Author

heddendorp commented Nov 21, 2024

I am importing import { serveStatic } from 'hono/bun' and would think that I am using bun as a runtime (running bun run build).
How do I make sure that bun is running the build, I was thinking that it is more nody.
Edit: sorry for taking down the repo again, I underestimated what else came in other branches there 😅
I can create a new one tomorrow.

@alan-agius4
Copy link
Collaborator

It seems that you're using Bun as a package manager rather than as a runtime, which are two distinct tools.

To learn more about installing Bun as a runtime, check out the official documentation here: Bun Installation Guide.

@heddendorp
Copy link
Author

Hey @alan-agius4 I did install bun and could also execute the built server with it.
The issue seems to only arise when running bun run build. I'm not sure how to influence the prerendering runtime that is used by angular during the build.
Sorry for my ignorance, I'll try to get a repo up that reproduces the issue, maybe that is more helpful.

@alan-agius4
Copy link
Collaborator

alan-agius4 commented Nov 21, 2024

Can you update please update node_modules/@angular/build/src/utils/server-rendering/routes-extractor-worker.js to below and report back with the error you are seeing?

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const worker_threads_1 = require("worker_threads");
const schema_1 = require("../../builders/application/schema");
const fetch_patch_1 = require("./fetch-patch");
const launch_server_1 = require("./launch-server");
const load_esm_from_memory_1 = require("./load-esm-from-memory");
/**
 * This is passed as workerData when setting up the worker via the `piscina` package.
 */
const { outputMode, hasSsrEntry } = worker_threads_1.workerData;
let serverURL = launch_server_1.DEFAULT_URL;
/** Renders an application based on a provided options. */
async function extractRoutes() {
    if (outputMode !== undefined && hasSsrEntry) {
      serverURL = await (0, launch_server_1.launchServer)();
    }
    (0, fetch_patch_1.patchFetchToLoadInMemoryAssets)(serverURL);

    const { ɵextractRoutesAndCreateRouteTree: extractRoutesAndCreateRouteTree } = await (0, load_esm_from_memory_1.loadEsmModuleFromMemory)('./main.server.mjs');
    const { routeTree, appShellRoute, errors } = await extractRoutesAndCreateRouteTree(serverURL, undefined /** manifest */, true /** invokeGetPrerenderParams */, outputMode === schema_1.OutputMode.Server /** includePrerenderFallbackRoutes */);
    return {
        errors,
        appShellRoute,
        serializedRouteTree: routeTree.toObject(),
    };
}
exports.default = extractRoutes;

@alan-agius4 alan-agius4 self-assigned this Nov 21, 2024
heddendorp added a commit to heddendorp/ssr-bun-test that referenced this issue Nov 21, 2024
@heddendorp
Copy link
Author

heddendorp commented Nov 21, 2024

I'm now getting a new error

Prerendered 0 static routes.
Application bundle generation failed. [4.350 seconds]

✘ [ERROR] An error occurred while extracting routes.

ReferenceError: Bun is not defined
    at file:///C:/Users/hedde/code/ssr-bun-test/.angular/prerender-root/09095c25-afdb-4a42-9070-e68c47fd62a3/server.mjs:1:23248
    at ModuleJob.run (node:internal/modules/esm/module_job:262:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:485:26)
    at async launchServer (C:\Users\hedde\code\ssr-bun-test\node_modules\@angular\build\src\utils\server-rendering\launch-server.js:27:28)
    at async extractRoutes (C:\Users\hedde\code\ssr-bun-test\node_modules\@angular\build\src\utils\server-rendering\routes-extractor-worker.js:16:17)
    at async C:\Users\hedde\code\ssr-bun-test\node_modules\piscina\dist\worker.js:144:26


error: script "build" exited with code 1

I also added both the patched and regular reproduction to https://github.com/heddendorp/ssr-bun-test

EDIT:
Some research showed https://bun.sh/docs/cli/run#bun (force the runtime to be bun). Curiously bun run --bun build just runs forever (more than two minutes). This happens with and without the patch applied.

@alan-agius4
Copy link
Collaborator

Thank you for the information. At this stage, it appears the issue lies with Bun, which is intended to serve as a drop-in replacement for Node.js. However, there seems to be a problem, possibly related to Workers or Atomics, that is causing unexpected behavior.

@alan-agius4 alan-agius4 closed this as not planned Won't fix, can't repro, duplicate, stale Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
angular/build:application needs: more info Reporter must clarify the issue
Projects
None yet
Development

No branches or pull requests

3 participants