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

Unexpected token u in JSON #7301

Closed
MuTsunTsai opened this issue Apr 20, 2023 · 10 comments · Fixed by #7307
Closed

Unexpected token u in JSON #7301

MuTsunTsai opened this issue Apr 20, 2023 · 10 comments · Fixed by #7307
Assignees
Labels
Milestone

Comments

@MuTsunTsai
Copy link

Describe the bug

In my project I'm running unit tests with mocha + ts-node + @swc/core. Not sure if this is a bug of @swc/core or of ts-node, but ever since I updated to @swc/core 1.3.50+ it shows the following error when I try to run the test:

image

Input code

No response

Config

No response

Playground link

No response

Expected behavior

There shouldn't be an error.

Actual behavior

No response

Version

1.3.50,1.3.51,1.3.52

Additional context

Although the source of the error seems to be in ts-node, but downgrading to @swc/core 1.3.49 solves the issue.

@kdy1
Copy link
Member

kdy1 commented Apr 20, 2023

Please provide a repro

@kdy1 kdy1 closed this as completed Apr 20, 2023
@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale Apr 20, 2023
@MuTsunTsai
Copy link
Author

@kdy1 Well, if I must, the repo is https://github.com/bp-studio/box-pleating-studio in DEV branch. To reproduce the error:

  1. Use PNPM to perform pnpm install after cloning the repo and checking-out DEV branch.
  2. Run pnpm test. Mocha should run without problem.
  3. Run pnpm update -L to force update @swc/core to the latest version.
  4. Run pnpm test to see the error.

Please reopen the issue if that's not too much to ask for.

@kdy1 kdy1 reopened this Apr 20, 2023
@kdy1
Copy link
Member

kdy1 commented Apr 20, 2023

Thank you, but as it's not a minimal repro, I don't think this can be fixed shortly.

https://antfu.me/posts/why-reproductions-are-required

@MuTsunTsai
Copy link
Author

Maybe this will help. I've traced the issue down to this part:

swc/node-swc/src/index.ts

Lines 232 to 236 in d5c0dec

return bindings.transformSync(
isModule ? JSON.stringify(src) : src,
isModule,
toBuffer(newOptions),
)

I believe that this transformSync method is in Rust so that the debugger won't allow me to step in further. In version 1.3.49, this always returns an object with both code and map properties, where the map could be an empty map like this:

{"version":3,"sources":[],"names":[],"mappings":""}

But since version 1.3.50, it could return an object with only code but not map (i.e. the property is missing), if the file being processed essentially contains no executable code (for example, it contains only TypeScript definitions), and it is because of the missing map that results in later error in ts-node. This breaking change of behavior feels more like unplanned to me.

@MuTsunTsai
Copy link
Author

@kdy1 Following the idea above, I've created a minimal repo here:
https://github.com/MuTsunTsai/swc-bug-demo

After npm install, run npm run test to see the error. Downgrade to v1.3.49 and there's no error.

@kdy1 kdy1 added this to the Planned milestone Apr 20, 2023
@kdy1
Copy link
Member

kdy1 commented Apr 20, 2023

Thank you so much for the investigation! This is an easy issue and I'll fix this tomorrow

@kdy1
Copy link
Member

kdy1 commented Apr 20, 2023

Investigation takes an unknown amount of time, while fixing is very easy in most cases.

@MuTsunTsai
Copy link
Author

@kdy1 True, and glad that it is also the case here. I admit that it did take me a while to go this far. Hope to see it fixed in the next version.

@kdy1 kdy1 self-assigned this Apr 20, 2023
kdy1 added a commit that referenced this issue Apr 21, 2023
**Related issue:**

 - Reverts #7229.
 - Closes #7301.
@kdy1 kdy1 modified the milestones: Planned, v1.3.53 Apr 21, 2023
@MuTsunTsai
Copy link
Author

Confirm fixed in v1.3.53

@swc-bot
Copy link
Collaborator

swc-bot commented May 22, 2023

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators May 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

Successfully merging a pull request may close this issue.

3 participants