ardatan / graphql-tools

:wrench: Utility library for GraphQL to build, stitch and mock GraphQL schemas in the SDL-first approach
https://www.graphql-tools.com
MIT License
5.35k stars 814 forks source link

@graphql-tools/mock 9.0.1 causes panic: Internal errors #5843

Open EvHaus opened 9 months ago

EvHaus commented 9 months ago

Issue workflow progress

Progress of the issue based on the Contributor Workflow


Describe the bug

After upgrading from @graphql-tools/mock@9.0.0 to @graphql-tools/mock@9.0.1 my app builds are failing with:

error: panic: Internal error (while parsing "/srv/node_modules/@graphql-tools/mock/node_modules/@graphql-tools/utils/esm/debugTimer.js")
file: /srv/node_modules/@graphql-tools/mock/node_modules/@graphql-tools/utils/esm/debugTimer.js
=> Failed to build the preview
Error: Transform failed with 1 error:
error: panic: Internal error (while parsing "/srv/node_modules/@graphql-tools/mock/node_modules/@graphql-tools/utils/esm/debugTimer.js")
    at failureErrorWithLog (/srv/node_modules/vite/node_modules/esbuild/lib/main.js:1650:15)
    at /srv/node_modules/vite/node_modules/esbuild/lib/main.js:848:29
    at responseCallbacks.<computed> (/srv/node_modules/vite/node_modules/esbuild/lib/main.js:703:9)
    at handleIncomingPacket (/srv/node_modules/vite/node_modules/esbuild/lib/main.js:763:9)
    at Socket.readFromStdout (/srv/node_modules/vite/node_modules/esbuild/lib/main.js:679:7)
    at Socket.emit (node:events:518:28)
    at addChunk (node:internal/streams/readable:559:12)
    at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
    at Readable.push (node:internal/streams/readable:390:5)
    at Pipe.onStreamRead (node:internal/stream_base_commons:190:23)

Rolling back to 9.0.0 makes the issue go away.

To Reproduce Steps to reproduce the behavior:

Working on it...

Expected behavior

No crash.

Environment:

Additional context

My app is Storybook, built via Vite.

ardatan commented 9 months ago

It looks like a bug in ESBuild on your compile time not a runtime issue https://github.com/ardatan/graphql-tools/blob/master/packages/utils/src/debugTimer.ts#L4 If you think it is a runtime issue, could you create a reproduction on CodeSandbox? Thanks!

wand2016 commented 9 months ago

In my project, the error is reproduced.

Editing node_modules/.pnpm/@graphql-tools+utils@10.0.13_graphql@16.8.1/node_modules/@graphql-tools/utils/esm/debugTimer.js fixed the error:

const debugNamesOngoing = new Set();
export function debugTimerStart(name) {
-   const debugEnvVar = globalThis?.process.env['DEBUG'] || globalThis.DEBUG;
+   // ad hoc
+   const debugEnvVar = globalThis.process.env['DEBUG'] || globalThis.DEBUG;
    if (debugEnvVar === '1' || debugEnvVar?.includes(name)) {
        debugNamesOngoing.add(name);
        console.time(name);
    }
}
export function debugTimerEnd(name) {
    if (debugNamesOngoing.has(name)) {
        console.timeEnd(name);
    }
}

UPDATE:

Install vite-plugin-replace, then

vite.config.ts

import { replaceCodePlugin } from 'vite-plugin-replace';

// ...

export default defineConfig(({ mode, command }) => ({
  plugins: [
    replaceCodePlugin({
      replacements: [
        {
          from: 'globalThis?.process.env',
          to: 'globalThis.process.env',
        },
      ],
    }),
  ...
})

This solved the problem...