trpc / trpc

πŸ§™β€β™€οΈ Move Fast and Break Nothing. End-to-end typesafe APIs made easy.
https://tRPC.io
MIT License
34.98k stars 1.25k forks source link

bug: using createServerSideHelpers cause jest worker error in github CI and Netlify CI #5504

Closed oskarhertzman closed 8 months ago

oskarhertzman commented 8 months ago

Provide environment information

  System:
    OS: macOS 14.2
    CPU: (11) arm64 Apple M3 Pro
    Memory: 65.69 MB / 18.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 21.6.2 - /opt/homebrew/bin/node
    Yarn: 1.22.21 - /opt/homebrew/bin/yarn
    npm: 10.2.4 - /opt/homebrew/bin/npm
    Watchman: 2024.01.22.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 121.0.6167.184
    Edge: 121.0.2277.128
    Safari: 17.2
  npmPackages:
    @tanstack/react-query: ^4.36.1 => 4.36.1 
    @trpc/client: ^10.43.0 => 10.44.1 
    @trpc/next: ^10.43.0 => 10.44.1 
    @trpc/react-query: ^10.43.0 => 10.44.1 
    @trpc/server: ^10.43.0 => 10.44.1 
    next: 13.4.8 => 13.4.8 
    react: ^18.2.0 => 18.2.0 
    typescript: ^5.2.2 => 5.3.3 

Describe the bug

when i try to use createServerSideHelpers in my project i always get jest-worker error in my ci build:

yarn build

Static worker unexpectedly exited with code: 1 and signal: null

 Build error occurred
Error: Call retries were exceeded
    at ChildProcessWorker.initialize (/home/runner/work/dirma/dirma/node_modules/next/dist/compiled/jest-worker/index.js:1:11661)
    at ChildProcessWorker._onExit (/home/runner/work/dirma/dirma/node_modules/next/dist/compiled/jest-worker/index.js:1:12599)
    at ChildProcess.emit (node:events:514:28)
    at ChildProcess.emit (node:domain:488:12)
    at ChildProcess._handle.onexit (node:internal/child_process:294:12) {
  type: 'WorkerError'
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
ERROR: "build" exited with 1.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Error: Process completed with exit code 1.

it works again if i comment createServerSideHelpers out.

This is my GSSP:

export async function getServerSideProps(context: CreateNextContextOptions) {
  const helpers = createServerSideHelpers({
    router: appRouter,
    ctx: await createContext(context),
    transformer: superjson,
  });
  await helpers.users.sessionUser.prefetch();
  return {
    props: {
      trpcState: helpers.dehydrate(),
    },
  };
}

createContext:

import { type CreateNextContextOptions } from "@trpc/server/adapters/next";

import { getAuth } from "@clerk/nextjs/server";
import { createContextInner } from "./context_inner";

/**
 * Outer context. Used in the routers and will e.g. bring `req` & `res` to the context as "not `undefined`".
 *
 * @see https://trpc.io/docs/context#inner-and-outer-context
 */
export async function createContext(opts: CreateNextContextOptions) {
  // for API-response caching see https://trpc.io/docs/caching
  const auth = getAuth(opts.req);
  const contextInner = await createContextInner({ auth });
  return {
    ...contextInner,
    req: opts.req,
    res: opts.res,
  };
}

export type { Context } from "./context_inner";

appRouter:


import type { inferRouterInputs, inferRouterOutputs } from "@trpc/server";
import { router } from "@/server/trpc";
import { advertisersRouter } from "./advertisers";
import { beneficiariesRouter } from "./beneficiaries/beneficiaries";
import { campaignsRouter } from "./campaigns/campaigns";
import { choicesRouter } from "./choices";
import { controlQuestionsRouter } from "./campaigns/control_questions";
import { invoicesRouter } from "./invoices";
import { loginsRouter } from "./logins";
import { paymentsRouter } from "./payments";
import { typesRouter } from "./types";
import { usersRouter } from "./users/users";

export const appRouter = router({
  types: typesRouter,
  choices: choicesRouter,

  users: usersRouter,
  logins: loginsRouter,

  beneficiaries: beneficiariesRouter,
  payments: paymentsRouter,

  advertisers: advertisersRouter,
  campaigns: campaignsRouter,
  controlQuestions: controlQuestionsRouter,
  invoices: invoicesRouter,
});

// export type definition of API
export type AppRouter = typeof appRouter;

export type RouterInput = inferRouterInputs<AppRouter>;
export type RouterOutput = inferRouterOutputs<AppRouter>;

What could be the cause? Any help would be GREATLY appreciated

Link to reproduction

.

To reproduce

create serverside helpers in GSSP like so:

export async function getServerSideProps(context: CreateNextContextOptions) {
  const helpers = createServerSideHelpers({
    router: appRouter,
    ctx: await createContext(context),
    transformer: superjson,
  });
  await helpers.users.sessionUser.prefetch();
  return {
    props: {
      trpcState: helpers.dehydrate(),
    },
  };
}

Additional information

No response

πŸ‘¨β€πŸ‘§β€πŸ‘¦ Contributing

Funding

Fund with Polar

KATT commented 8 months ago

Hey there! :wave:

We appreciate the time you've taken to write this issue.

Unfortunately, we don't have the capacity to assist with issues without a reproduction repository and this issue does not seem to be related to tRPC itself. We test createServerSideProps using Vitest in the tRPC repo itself and I have other projects where I use Jest.

Please feel free to re-open with a solid reproduction repository where we can help you further or post in our help channels on Discord or in our GitHub Discussions.

github-actions[bot] commented 8 months ago

This issue has been locked because we are very unlikely to see comments on closed issues. If you are running into a similar issue, please create a new issue. Thank you.