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

Advise which file failed to be transformed when RollupError: Unexpected character occurs #5246

Closed
oscarhermoso opened this issue Nov 11, 2023 · 11 comments · Fixed by #5424
Closed

Comments

@oscarhermoso
Copy link

oscarhermoso commented Nov 11, 2023

Expected Behavior / Situation

When RollupError: Unexpected character '' (Note that you need plugins to import files that are not JavaScript) occurs, the file that failed to be transformed should be logged

.svelte-kit/output/server/chunks/intro.js                                                                                             140.97 kB
.svelte-kit/output/server/entries/pages/room/_room_/_page@room.svelte.js                                                              142.75 kB
.svelte-kit/output/server/chunks/email-styles.js                                                                                      365.06 kB

Run npm run preview to preview your production build locally.

> Using @sveltejs/adapter-node
Failed to transform module: /home/oscarhermoso/Git/kedyou-frontend/node_modules/.pnpm/canvas@2.11.2/node_modules/canvas/build/Release/canvas.node
Failed to transform module: /home/oscarhermoso/Git/kedyou-frontend/node_modules/.pnpm/canvas@2.11.2/node_modules/canvas/lib/bindings.js
Failed to transform module: /home/oscarhermoso/Git/kedyou-frontend/node_modules/.pnpm/canvas@2.11.2/node_modules/canvas/index.js
Failed to transform module: /home/oscarhermoso/Git/kedyou-frontend/node_modules/.pnpm/canvas@2.11.2/node_modules/canvas/lib/canvas.js
Failed to transform module: /home/oscarhermoso/Git/kedyou-frontend/node_modules/.pnpm/canvas@2.11.2/node_modules/canvas/lib/context2d.js
Failed to transform module: /home/oscarhermoso/Git/kedyou-frontend/node_modules/.pnpm/canvas@2.11.2/node_modules/canvas/lib/image.js
Failed to transform module: /home/oscarhermoso/Git/kedyou-frontend/node_modules/.pnpm/canvas@2.11.2/node_modules/canvas/lib/pattern.js
Failed to transform module: /home/oscarhermoso/Git/kedyou-frontend/node_modules/.pnpm/pdfjs-dist@3.11.174/node_modules/pdfjs-dist/build/pdf.js
error during build:
RollupError: Unexpected character '' (Note that you need plugins to import files that are not JavaScript)
    at error (file:///home/oscarhermoso/Git/kedyou-frontend/node_modules/.pnpm/rollup@3.29.4/node_modules/rollup/dist/es/shared/node-entry.js:2287:30)
    at Module.error (file:///home/oscarhermoso/Git/kedyou-frontend/node_modules/.pnpm/rollup@3.29.4/node_modules/rollup/dist/es/shared/node-entry.js:13745:16)
    at Module.tryParse (file:///home/oscarhermoso/Git/kedyou-frontend/node_modules/.pnpm/rollup@3.29.4/node_modules/rollup/dist/es/shared/node-entry.js:14476:25)
    at Module.setSource (file:///home/oscarhermoso/Git/kedyou-frontend/node_modules/.pnpm/rollup@3.29.4/node_modules/rollup/dist/es/shared/node-entry.js:14077:39)
    at ModuleLoader.addModuleSource (file:///home/oscarhermoso/Git/kedyou-frontend/node_modules/.pnpm/rollup@3.29.4/node_modules/rollup/dist/es/shared/node-entry.js:24650:24)
 ELIFECYCLE  Command failed with exit code 1.

Actual Behavior / Situation

Logs currently look like below - notice that there is no indication as to which file caused the problem:

...
.svelte-kit/output/server/chunks/intro.js                                                                                             140.97 kB
.svelte-kit/output/server/entries/pages/room/_room_/_page@room.svelte.js                                                              142.75 kB
.svelte-kit/output/server/chunks/email-styles.js                                                                                      365.06 kB

Run npm run preview to preview your production build locally.

> Using @sveltejs/adapter-node

error during build:
RollupError: Unexpected character '' (Note that you need plugins to import files that are not JavaScript)
    at error (file:///home/oscarhermoso/Git/kedyou-frontend/node_modules/.pnpm/rollup@3.29.4/node_modules/rollup/dist/es/shared/node-entry.js:2287:30)
    at Module.error (file:///home/oscarhermoso/Git/kedyou-frontend/node_modules/.pnpm/rollup@3.29.4/node_modules/rollup/dist/es/shared/node-entry.js:13745:16)
    at Module.tryParse (file:///home/oscarhermoso/Git/kedyou-frontend/node_modules/.pnpm/rollup@3.29.4/node_modules/rollup/dist/es/shared/node-entry.js:14476:25)
    at Module.setSource (file:///home/oscarhermoso/Git/kedyou-frontend/node_modules/.pnpm/rollup@3.29.4/node_modules/rollup/dist/es/shared/node-entry.js:14077:39)
    at ModuleLoader.addModuleSource (file:///home/oscarhermoso/Git/kedyou-frontend/node_modules/.pnpm/rollup@3.29.4/node_modules/rollup/dist/es/shared/node-entry.js:24650:20)
 ELIFECYCLE  Command failed with exit code 1.

Modification Proposal

At the location where files are transformed, provide additional logging output when errors occur.

rollup/src/ModuleLoader.ts

Lines 322 to 325 in 52c55bb

module.updateOptions(sourceDescription);
await module.setSource(
await transform(sourceDescription, module, this.pluginDriver, this.options.onLog)
);

+    try {
         module.updateOptions(sourceDescription);
         await module.setSource(
            await transform(sourceDescription, module, this.pluginDriver, this.options.onLog)
         );
+     } catch (err) {
+       console.log("Failed to transform module:", id);
+       throw err;
+    }
@oscarhermoso
Copy link
Author

Would be happy to raise a PR, but I'm not familiar the rollup codebase & logging implementation

@oscarhermoso oscarhermoso changed the title Advise which file failed to be import when RollupError: Unexpected character occurs Advise which file failed to be transformed when RollupError: Unexpected character occurs Nov 11, 2023
@oscarhermoso
Copy link
Author

Looks like there was a regression at some point in time, previous logging informed where the error occurred:

#1030

@lukastaegert
Copy link
Member

Rollup error objects contain all the information and I am quite sure Rollup CLI would print that information, but we are not the maintainers of SvelteKit. Also, it looks like you are not using a current version of Rollup as I think the error looks slightly different now.

@oscarhermoso
Copy link
Author

@lukastaegert You are correct, with the latest version of rollup, the problematic file is logged

https://stackblitz.com/edit/stackblitz-starters-pwd7ao?file=README.md

src/index.js → output...
[!] RollupError: Unexpected character '�' (Note that you need plugins to import files that are not JavaScript)
src/asd.png (1:0) <------------ (problematic file is now logged)
1: �PNG
   ^
2:
IHDR��  �ad�tEXtSoftwaregnome-screenshot��>&tEXtCreation TimeThu 20 Jul 2023 1...
RollupError: Unexpected character '�'
    at error (/home/projects/stackblitz-starters-pwd7ao/node_modules/rollup/dist/shared/parseAst.js:279:30)
    at nodeConverters (/home/projects/stackblitz-starters-pwd7ao/node_modules/rollup/dist/shared/parseAst.js:2135:9)
    at convertNode (/home/projects/stackblitz-starters-pwd7ao/node_modules/rollup/dist/shared/parseAst.js:1020:12)
    at convertProgram (/home/projects/stackblitz-starters-pwd7ao/node_modules/rollup/dist/shared/parseAst.js:1011:48)
    at Object.parseAstAsync (/home/projects/stackblitz-starters-pwd7ao/node_modules/rollup/dist/shared/parseAst.js:2201:20)
    at async Module.tryParseAsync (/home/projects/stackblitz-starters-pwd7ao/node_modules/rollup/dist/shared/rollup.js:14785:21)
    at async Module.setSource (/home/projects/stackblitz-starters-pwd7ao/node_modules/rollup/dist/shared/rollup.js:14381:35)
    at async ModuleLoader.addModuleSource (/home/projects/stackblitz-starters-pwd7ao/node_modules/rollup/dist/shared/rollup.js:18655:13)

I'll close this issue, thank you for looking into it

@jorisw
Copy link

jorisw commented Mar 6, 2024

I'm on Rollup 4.12.1, and all I see is

transforming...
transforming...
✓ 76 modules transformed.
x Build failed in 248ms
error during build:
RollupError: Unexpected character '�'
    at getRollupError (file:node_modules/rollup/dist/es/shared/parseAst.js:375:41)

@lukastaegert
Copy link
Member

This is not the output of Rollup, this looks to me like the output of Vite. But you are right, maybe we should rewrite the error messages to include the location as several tools seem to be swallowing it

@lukastaegert
Copy link
Member

Fix here #5424

Copy link

This issue has been resolved via #5424 as part of rollup@4.13.0. You can test it via npm install rollup.

@jorisw
Copy link

jorisw commented Mar 18, 2024

Still getting an unhelpful error when building with Vite + Rollup 4.13.0:

       x Build failed in 294ms
       error during build:
       RollupError: Unexpected character '�'
           at getRollupError (node_modules/rollup/dist/es/shared/parseAst.js:376:41)
           at ParseError.initialise (node_modules/rollup/dist/es/shared/node-entry.js:11158:28)
           at convertNode (node_modules/rollup/dist/es/shared/node-entry.js:12898:10)
           at convertProgram (node_modules/rollup/dist/es/shared/node-entry.js:12218:12)
           at Module.setSource (node_modules/rollup/dist/es/shared/node-entry.js:14042:24)
           at async ModuleLoader.addModuleSource (node_modules/rollup/dist/es/shared/node-entry.js:18681:13)
# yarn why rollup
├─ @storybook/builder-vite@npm:7.6.10
│  └─ rollup@npm:4.13.0 (via npm:4.13.0)
│
├─ @storybook/builder-vite@npm:7.6.10 [d8439]
│  └─ rollup@npm:4.13.0 (via npm:4.13.0)
│
├─ vite-plugin-eslint@npm:1.8.1
│  └─ rollup@npm:4.13.0 (via npm:4.13.0)
│
├─ vite-plugin-eslint@npm:1.8.1 [0f07f]
│  └─ rollup@npm:4.13.0 (via npm:4.13.0)
│
├─ vite@npm:5.0.10
│  └─ rollup@npm:4.13.0 (via npm:4.13.0)
│
├─ vite@npm:5.1.4
│  └─ rollup@npm:4.13.0 (via npm:4.13.0)
│
├─ vite@npm:5.0.10 [08abd]
│  └─ rollup@npm:4.13.0 (via npm:4.13.0)
│
├─ vite@npm:5.1.4 [0f07f]
│  └─ rollup@npm:4.13.0 (via npm:4.13.0)
│
├─ vite@npm:5.1.4 [5880a]
│  └─ rollup@npm:4.13.0 (via npm:4.13.0)
│
├─ vite@npm:5.0.10 [685e3]
│  └─ rollup@npm:4.13.0 (via npm:4.13.0)
│
├─ vite@npm:5.1.4 [7b2d7]
│  └─ rollup@npm:4.13.0 (via npm:4.13.0)
│
├─ vite@npm:5.1.4 [cd567]
│  └─ rollup@npm:4.13.0 (via npm:4.13.0)
│
├─ vite@npm:5.0.10 [d8d3f]
│  └─ rollup@npm:4.13.0 (via npm:4.13.0)
│
└─ vite@npm:5.1.4 [e809a]
   └─ rollup@npm:4.13.0 (via npm:4.13.0)

@lukastaegert
Copy link
Member

Then please provide a working reproduction.

@jorisw
Copy link

jorisw commented Mar 18, 2024

Thanks, I will if I can (large project).

Until then FYI I get the path to the offending file printed in my terminal if I add to node_modules/rollup/dist/es/shared/parseAst.js:374:

function getRollupError(base) {
+ console.log(base.id)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants