next.js icon indicating copy to clipboard operation
next.js copied to clipboard

`swcMinify: true` does not produce an usable build with v12.1.5

Open ribeaud opened this issue 2 years ago • 38 comments

Verify canary release

  • [X] I verified that the issue exists in Next.js canary release

Provide environment information

 $ npx --no-install next info

    Operating System:
      Platform: darwin
      Arch: x64
      Version: Darwin Kernel Version 21.4.0: Fri Mar 18 00:45:05 PDT 2022; root:xnu-8020.101.4~15/RELEASE_X86_64
    Binaries:
      Node: 16.13.1
      npm: 8.1.2
      Yarn: 1.22.18
      pnpm: N/A
    Relevant packages:
      next: 12.1.5
      react: 17.0.2
      react-dom: 17.0.2

What browser are you using? (if relevant)

Not relevant

How are you deploying your application? (if relevant)

next start after a next build

Describe the Bug

Locally (running the application locally, in my IDE), everything looks good. Our tests are green as well. Only the PROD artefact is somehow rubbish.

When accessing the homepage, this is what we get in the console:

console

When clicking on one of the links displayed, this is what I get:

error_decoder

Expected Behavior

No error messages in the console, application is running properly and swcMinify is set to true, like it used to be for v12.1.0 (application was NOT working with the releases from v12.1.1 to v12.1.4).

The expected behavior could be achieved if I set swcMinify to false in the configuration file.

To Reproduce

Kindly follow instructions given in the README.md, Production-like local deployment.

More info on the problem could be found here and there.

Please, let me know if you have questions, or whether I should deeply investigate in a given direction, or if I can do anything here to help.

Thanks in advance for your time and investigation.

ribeaud avatar Apr 13 '22 16:04 ribeaud

same here.

bonesoul avatar Apr 14 '22 00:04 bonesoul

same here

/**
 * Create a code check from a regex.
 *
 * @param {RegExp} regex
 * @returns {(code: Code) => code is number}
 */
function regexCheck(regex) {
  return check

  /**
   * Check whether a code matches the bound regex.
   *
   * @param {Code} code Character code
   * @returns {code is number} Whether the character code matches the bound regex
   */
  function check(code) {
    return code !== null && regex.test(String.fromCharCode(code))
  }
}

Code above will not work fine. Throw ReferenceError: check is not defined

powerfulyang avatar Apr 15 '22 02:04 powerfulyang

I fixed the issue reported by @powerfulyang (https://github.com/swc-project/swc/pull/4338), but not sure if this issue is related to this issue.

kdy1 avatar Apr 15 '22 10:04 kdy1

I'm not sure how can I test the fix

Seems like you are invoking next.js in docker, right? If so, yarn link will not help. Is there a way to run the webapp without docker?

kdy1 avatar Apr 15 '22 10:04 kdy1

@kdy1 Can you add this change https://github.com/swc-project/swc/pull/4250 in nextjs to fix infinite loops?

smitpatel1010 avatar Apr 15 '22 12:04 smitpatel1010

@smitpatel1010 Of course, I included it in https://github.com/vercel/next.js/pull/36187

kdy1 avatar Apr 15 '22 12:04 kdy1

Oh great. Thanks 🙂

smitpatel1010 avatar Apr 15 '22 12:04 smitpatel1010

@ribeaud looking at your reproduction, it seems a bit complicated. Could you please try to create a more minimal one that's focusing on the issue?

I recommend removing any pages/components/technologies that do not contribute to the bug you are experiencing (eg. I'm sure Docker and most of the stuff in the repo are not contributing to the issue here). This will help us verify and fix any bugs much faster. :+1:

balazsorban44 avatar Apr 15 '22 13:04 balazsorban44

@balazsorban44 makes perfectly sense. However I am not sure being able to do it before coming week-end. What I definitely can do is to test any canary release you want.

ribeaud avatar Apr 20 '22 14:04 ribeaud

@ribeaud no rush on providing the code but we can't investigate it further for your particular case without it. Can you try the latest canary version? A bunch of changes to the minifier were applied.

timneutkens avatar Apr 20 '22 15:04 timneutkens

swcMinify: true is causing the exact same problem for me. After removing it from next.config the production build is starting smoothly

arnovanstaden avatar Apr 21 '22 13:04 arnovanstaden

@arnovanstaden can you provide a reproduction for your case? We've generally found that comments on these issues tend to be completely different minifier bugs that have to be investigated separately.

timneutkens avatar Apr 21 '22 13:04 timneutkens

Same issue here with swcMinify: true. Was working fine on Next.js 12.1.4 and broke on 12.1.5. Also tried v12.1.6-canary.4 which didn't fix it either.

The issue can be reproduced by running the few simple commands listed for production on https://github.com/Luukuton/shingetsu-blog/tree/main/frontend with the following as .env.local:

NEXT_PUBLIC_STRAPI_API_URL=https://strapi.luukuton.fi
NEXTAUTH_URL=http://localhost:3000
SECRET=1234
JWT_SIGNING_PRIVATE_KEY={"kty":"oct","kid":"1234","alg":"HS256","k":"1234"}

Remember the change the swcMinify to true in next.config.mjs as well.

Errors: Application error: a client-side exception has occurred (see the browser console for more information).

ReferenceError: check is not defined
    at Object.7011 (75-8b0d76e33d9d9da7.js:1:226386)
    at a (webpack-dab0b2f9bb1be3f3.js:1:157)
    at Module.334 (index-94cc969871e15009.js:1:289)
    at a (webpack-dab0b2f9bb1be3f3.js:1:157)
    at index-94cc969871e15009.js:1:151
    at main-fd09345e2a348c4f.js:1:25750

CrescentKohana avatar Apr 21 '22 14:04 CrescentKohana

@Luukuton Seems like it's fixed by https://github.com/vercel/next.js/commit/9456fc6b3b31a3291c69600866e1be3a9eac8835

image

kdy1 avatar Apr 21 '22 14:04 kdy1

https://nextjs.org/docs/advanced-features/source-maps

!!! Set productionBrowserSourceMaps true in next.config.js can get full error trace.

powerfulyang avatar Apr 21 '22 15:04 powerfulyang

@Luukuton Seems like it's fixed by https://github.com/vercel/next.js/commit/9456fc6b3b31a3291c69600866e1be3a9eac8835

Nice! Looks good.

!!! Set productionBrowserSourceMaps true in next.config.js can get full error trace.

Thanks 😄. I'll remember this in the future if I have to debug something.

CrescentKohana avatar Apr 21 '22 15:04 CrescentKohana

The latest canary (12.1.6-canary.5) does a much better job. Still NOT working on my end, but now getting following error in the console:

console

ribeaud avatar Apr 21 '22 16:04 ribeaud

We have been running into this issue in one of our apps as well (even on the latest canary). In our case, the following error is thrown:

TypeError: j is not a function
    at r.exports (util.js:1:46666)
    at Object.112 (util.js:1:46666)
    at __nccwpck_require__ (util.js:1:46666)
    at Object.749 (util.js:1:46666)
    at __nccwpck_require__ (util.js:1:46666)
    at Object.715 (util.js:1:46666)
    at __nccwpck_require__ (util.js:1:46666)
    at Object.3 (util.js:1:46666)
    at __nccwpck_require__ (util.js:1:46666)
    at Object.650 (util.js:1:46666)

After some process of elimination, the culprit turned out to be some code that was using

import { isString } from 'util';

And removing that code makes the error goes away.

I pushed up a simple reproduction here in hopes it helps get to the root cause of the issue (this form of it anyway): https://github.com/seanparmelee/nextjs-36127-reproduction

  • Pull it down
  • Run yarn build && yarn start
  • Navigate to http://localhost:3000

seanparmelee avatar Apr 22 '22 04:04 seanparmelee

@seanparmelee image

It will be fixed by https://github.com/vercel/next.js/pull/36351

(I used my own script to replace next-swc binary)

kdy1 avatar Apr 22 '22 04:04 kdy1

I tried release https://github.com/vercel/next.js/releases/tag/v12.1.6-canary.13 that should contain fix from https://github.com/vercel/next.js/pull/36351 but still doesn't work зображення

VeronikaSergiyenko avatar Apr 28 '22 13:04 VeronikaSergiyenko

@VeronikaSergiyenko I think it will be fixed by https://github.com/vercel/next.js/pull/36535

React now uses google closure compiler as their minifier, and it triggered a bug of swc minifier. So I disabled the problematic pass for now and I'm working on a full fix for alias analyzer

kdy1 avatar Apr 28 '22 13:04 kdy1

I just want to add to this, that I ran into a similar issue whereupon deployment I receive invariant 138 saying "Maximum depth exceeded", and disabling swcMinify solves the issue for now. https://reactjs.org/docs/error-decoder.html/?invariant=185

Rafcin avatar May 26 '22 04:05 Rafcin

@Rafcin Did you try the latest canary? If so, can you provide a repro?

kdy1 avatar May 26 '22 05:05 kdy1

@kdy1 I just tested a deployment with 12.1.7-canary.16 and it threw the same issue. The repo is private, but if you need anything specific let me know!

Rafcin avatar May 26 '22 05:05 Rafcin

Is the solution just to downgrade to 12.10.0? Our build was working with 12.1.6. Migrated to Material UI 5 . And part of the codebase that's unrelated to the MUI started breaking after minify

terryli0095 avatar Jun 08 '22 08:06 terryli0095

@terryli0095 You can provide more information, like repro case, so I can fix it

kdy1 avatar Jun 08 '22 08:06 kdy1

@kdy1 I can reproduce this error using a dependency as example:

next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
  swcMinify: true,
};

module.exports = nextConfig;
package.json
{
  "name": "swc-test",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@codemirror/text": "^0.19.6",
    "next": "12.1.6",
    "react": "18.2.0",
    "react-dom": "18.2.0"
  },
  "devDependencies": {
    "@types/node": "18.0.0",
    "@types/react": "18.0.14",
    "@types/react-dom": "18.0.5",
    "eslint": "8.18.0",
    "eslint-config-next": "12.1.6",
    "typescript": "4.7.4"
  }
}

index.tsx
import type { NextPage } from "next";
import { Text } from "@codemirror/text";

const Home: NextPage = () => {
  const length = Text.of(["Hello World"]).length;

  return <p>{length}</p>;
};

export default Home;

The variable value should always return 11, but if you set the swcMinify to true it returns -1. As far I could understand, this option is generating a different output of the following piece of code.

// Wrong
swcMinify: true,

// @codemirror/text
constructor(a, b=function(a) {
  for (let b of a)
    b.length;
  return -1
} 

// Correct
swcMinify: false,

// @codemirror/text
constructor(a, b=function(a) {
  let e = -1
  for (let b of a)
    e += b.length + 1;
  return e
}

Hope it helps


Edit: it has been fixed on Canary

danilowoz avatar Jun 20 '22 13:06 danilowoz


  let e = -1
  for (let b of a)
    e + b.length + 1;
  return e

Is this typo?

kdy1 avatar Jun 20 '22 13:06 kdy1

Sorry, yes. The right output is: Screenshot 2022-06-20 at 11 34 32

danilowoz avatar Jun 20 '22 13:06 danilowoz

@danilowoz https://github.com/swc-project/swc/pull/5013

I checked it and it was already fixed long time ago. What's your next.js version?

kdy1 avatar Jun 20 '22 13:06 kdy1

It's 12.1.6, I just created this project from scratch, and other users also reported this problem recently.

danilowoz avatar Jun 20 '22 13:06 danilowoz

@danilowoz Can you try canary?

kdy1 avatar Jun 20 '22 13:06 kdy1

Yep, I can confirm that it has been fixed on canary. Thanks, @kdy1

danilowoz avatar Jun 20 '22 13:06 danilowoz

Any progress on this? I want to disable it to debug a bug that doesn't happen in local development mode for some reason

OskarD avatar Aug 03 '22 11:08 OskarD

@OskarD All debuggable issues are fixed. Others did not provide any information.

kdy1 avatar Aug 03 '22 11:08 kdy1

@OskarD All debuggable issues are fixed. Others did not provide any information.

I'm probably doing something wrong, but with 12.2.3 and this config:

    swcMinify: false,
    compress: false

I'm getting this kind of error:

Error: No credentials
    at e.<anonymous> (70-0c006eaa97bdd322.js:1:193655)
    at 70-0c006eaa97bdd322.js:1:185254
    at Object.next (70-0c006eaa97bdd322.js:1:185359)
    at a (70-0c006eaa97bdd322.js:1:184095)

Is there something I'm missing here?

OskarD avatar Aug 03 '22 11:08 OskarD

Is it really related to swc minifier?

    swcMinify: false,

Is this typo?

kdy1 avatar Aug 03 '22 12:08 kdy1

Is it really related to swc minifier?

    swcMinify: false,

Is this typo?

Sorry, I thought this thread was for disabling minifying. I'm using Webpack so my problem is not related, my apologies

OskarD avatar Aug 03 '22 12:08 OskarD