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

next build fails on importing a packaging via its conditional export #45452

Open
1 task done
marcoreni opened this issue Jan 31, 2023 · 0 comments
Open
1 task done

next build fails on importing a packaging via its conditional export #45452

marcoreni opened this issue Jan 31, 2023 · 0 comments
Labels
bug Issue was opened via the bug report template. TypeScript Related to types with Next.js.

Comments

@marcoreni
Copy link

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 22.3.0: Thu Jan  5 20:48:54 PST 2023; root:xnu-8792.81.2~2/RELEASE_ARM64_T6000
Binaries:
  Node: 18.12.0
  npm: 8.19.2
  Yarn: 1.22.19
  pnpm: 7.26.1
Relevant packages:
  next: 13.1.7-canary.0
  eslint-config-next: 13.1.5
  react: 18.2.0
  react-dom: 18.2.0

Which area(s) of Next.js are affected? (leave empty if unsure)

TypeScript

Link to the code that reproduces this issue

https://stackblitz.com/edit/vercel-next-js-fkhtk7?file=pages/index.tsx

To Reproduce

  • Add a dependency on a package which has a conditional export entrypoint.
  • Try and import via its conditional entrypoint path, while using "moduleResolution: node16"
  • Run next dev and see it succeeds
  • Run tsc --noEmit and see it succeeds
  • Run next build and see it fails with Type error: Cannot find module '...' or its corresponding type declarations.

Describe the Bug

This is probably a perfect clone of #43698 , however I think the issue is NOT on Typescript side.

The main difference with the other issue is that running tsc --noEmit works perfectly, while using moduleResolution: node16. It (correctly) fails while using moduleResolution: node, since exports only work with node16/nodenext. next build, however, always fails.

I saw #44177 merged in the latest canary, but while dev works perfectly (and I'm able to use the components), build compiles correctly but fails in type checking. Maybe something else is missing?

Expected Behavior

The build should work

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

@marcoreni marcoreni added the bug Issue was opened via the bug report template. label Jan 31, 2023
@github-actions github-actions bot added the TypeScript Related to types with Next.js. label Jan 31, 2023
shuding added a commit that referenced this issue Feb 15, 2023
#45670)

## Bug

The `next build` command is silently overriding the user's tsconfig when
it shouldn't be; this results in mismatched behavior between `tsc
--noEmit` and `yarn build` and user confusion.

For example, a configuration option like `"moduleResolution":
"nodenext"`, which is preserved and respected by `next dev`, will be
silently overridden to `"moduleResolution": "node"` during `next build`.

This change:
- Fixes #38854
- (probably fixes) #45452 (I have not verified)
- (probably fixes) #41189 (I have not verified)

## Details

Next has a concept of both _defaults_ and _permitted options_ when
modifying/validating the user's tsconfig. The user's config is only
modified if it does not match the _permitted options_. This means that
if the user has specified a permitted value like `"moduleResolution":
"nodenext"`, it will not be overwritten in the user's config file.

However, there was some logic in `runTypeCheck.ts` that did not
adequately capture this nuance – instead, it spread all of the defaults
into the tsconfig it was building before running typecheck, which meant
that if a user had specified an option that was _permitted_ but
_non-default_, it would be overwritten, silently, during `yarn build`
only.

Because Next is already (1) rewriting the TSconfig in
`writeConfigurationDefaults` when the user's config doesn't line up with
what we're expecting and (2) verifying the user's TSConfig remains
correct (in `verifyTypeScriptSetup`) during a `next build`, I believe
that it is safe to remove this config-steamrolling behavior.

## Documentation / Examples

I believe this is strictly a bugfix; it updates the behavior of `next
build` to conform to the same configuration behavior exhibited by `tsc
--noEmit` and `next dev`. Since this is already the user expectation, it
should not require documentation changes.

---------

Co-authored-by: Shu Ding <g@shud.in>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. TypeScript Related to types with Next.js.
Projects
None yet
Development

No branches or pull requests

1 participant