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

JSON.stringify of builder aggregate error returns empty object #16587

Open
nobkd opened this issue Jan 22, 2025 · 3 comments
Open

JSON.stringify of builder aggregate error returns empty object #16587

nobkd opened this issue Jan 22, 2025 · 3 comments
Labels
bug Something isn't working bundler Something to do with the bundler

Comments

@nobkd
Copy link

nobkd commented Jan 22, 2025

What version of Bun is running?

1.2.0-canary.43+5819fe49a

What platform is your computer?

Linux 6.12.9-200.fc41.x86_64 x86_64 unknown

What steps can reproduce the bug?

Run first with 1.1.45+196621f25, then again with e.g. 1.2.0-canary.43+5819fe49a and compare the error object:

import { promises as fs } from 'node:fs'

// create erroneous css file
const filename = 'test.css'
await fs.writeFile(filename, 'body margin: 0 }')

try {
    // build
    const result = await Bun.build({ entrypoints: [filename], minify: true, experimentalCss: true })

    // on build failure: is run until at least 1.1.45+196621f25, not run on 1.2.0-canary.43+5819fe49a
    if (!result.success) {
        console.log('Manual throw:')
        throw result.logs[0]
    }

    // do sth. with result, not relevant here

} catch(e) {
    console.error(e)
    console.log('---')
    console.error('Error Object:', JSON.stringify(e, null, 2))

    // cleanup
    await fs.rm(filename, { force: true })
}

This errors with the following error in both versions (as expected):

error: Unexpected end of input
    at /.../test.css:1:17

Which is correct and expected, but the error data I get is different.

What is the expected behavior?

Having an error object with details about the build error

(looks like the following in 1.1.45+196621f25)

something like:

{
  "name": "BuildMessage",
  "position": {
    "lineText": "body margin: 0 }",
    "file": "/.../test.css",
    "namespace": "file",
    "line": 1,
    "column": 17,
    "length": 0,
    "offset": 0
  },
  "message": "Unexpected end of input",
  "level": "error"
}

What do you see instead?

no error object

(looks like the following in 1.2.0-canary.43+5819fe49a)

{}

Additional information

In previous versions, the Bun.build command returned a success: false key on the result object, and the logs property contained error information.

The new variant throws an error directly, but has no error information attached.

I don't really care, how this gets handled, as long, as I get appropriate error data, that can be processed in JS :)

@nobkd nobkd added bug Something isn't working needs triage labels Jan 22, 2025
@RiskyMH RiskyMH added bundler Something to do with the bundler css CSS parser, bundler-related and removed needs triage labels Jan 22, 2025
@nobkd
Copy link
Author

nobkd commented Jan 22, 2025

Oh, I haven't checked, if its the same for non-css building. I just tested it only with css.

@RiskyMH RiskyMH removed the css CSS parser, bundler-related label Jan 22, 2025
@paperclover
Copy link
Member

the new behavior is intentionally throwing an AggregateError, see https://bun.sh/docs/bundler#logs-and-errors. This change was made after seeing most users forget to handle success: false, or worse forget to await the promise at all.

However, I did not check that JSON.stringify actually works, to which it seems to not work. We should fix that.

Maybe you can iterate and read errors individually with e.errors (array of message)

@nobkd
Copy link
Author

nobkd commented Jan 22, 2025

Ah, I didn't think about e.errors. Using that works fine. Thank you. Good luck with the stringification of the aggregate error :)

@nobkd nobkd changed the title css builder error has no error object in canary JSON.stringify of builder aggregate error returns empty object Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working bundler Something to do with the bundler
Projects
None yet
Development

No branches or pull requests

3 participants