vercel / next.js

The React Framework
https://nextjs.org
MIT License
127.47k stars 27.04k forks source link

Relay compiler configuration "exclude" option is not respected #69890

Open mjfaga opened 2 months ago

mjfaga commented 2 months ago

Link to the code that reproduces this issue

https://github.com/mjfaga/nextjs-relay-swc-excludes

To Reproduce

Install dependencies and build:

yarn install
yarn build

Current vs. Expected behavior

Current Behavior

When adding a library (in the MVP, @stigg/react-sdk) that uses GraphQL under the hood, the relay compiler breaks consumption of that library and the app no longer properly builds.

 ± yarn build
yarn run v1.22.21
$ relay-compiler --validate && next build
[INFO] Querying files to compile...
[INFO] [default] compiling...
[INFO] [default] compiled documents: 0 reader, 0 normalization, 0 operation text
[INFO] Compilation completed.
[INFO] Done.
  ▲ Next.js 14.2.8

   Creating an optimized production build ...
Failed to compile.

./node_modules/@stigg/js-client-sdk/dist/index.js
Module not found: Can't resolve '/Users/markfaga/projects/nextjs-relay-swc-excludes/./__generated__/SlimSubscriptionFragmentV2.graphql.ts'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./node_modules/@stigg/react-sdk/dist/react-sdk.esm.js
./src/app/page.tsx

Expected Behavior

No errors because node_modules is excluded from being targeted during relay GraphQL compilation (see below for additional context).

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.2.0: Wed Nov 15 21:55:06 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T6020
  Available memory (MB): 98304
  Available CPU cores: 12
Binaries:
  Node: 22.7.0
  npm: 10.8.2
  Yarn: 1.22.21
  pnpm: N/A
Relevant Packages:
  next: 14.2.8 // Latest available version is detected (14.2.8).
  eslint-config-next: 14.2.8
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.6.2
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Turbopack

Which stage(s) are affected? (Select all that apply)

next build (local)

Additional context

The current SWC relay compiler implementation only supports 3 key options:

exclude is another critical option that is not currently supported. In the direct relay implementation, this is optional BUT ships with a default value of ["**/node_modules/**", "**/mocks/**", "**/generated/**"] to ensure things like packages in node_modules aren't targeted when they also happen to use GraphQL under the hood. In these cases, node_module source is recompiled, breaking those libraries.

JesseKoldewijn commented 2 months ago

Out of curiosity, what makes you think this issue would be related to turbopack?

And a question more directly towards your issue at hand. Do you mind trying if this issue still persists if you strip the starting './' from the relay config -> output

finn-orsini commented 2 months ago

@JesseKoldewijn

what makes you think this issue would be related to turbopack?

This is marked as related to Turbopack because as far as we can tell, the relay compiler option in question executes a Turbopack swc plugin. Could have optionally said this was related to SWC instead, but it is a turbopack plugin so 🤷

Do you mind trying if this issue still persists if you strip the starting './' from the relay config -> output

This has no impact - the relay compiler is still attempting to run on the package in node_modules because excludes is not respected. (Although perhaps related to your question - the generated directory does need to exist to run the build, will update the mvr repo shortly)