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

Core: Fix fail to load main.ts error message #26035

Merged
merged 11 commits into from Feb 20, 2024

Conversation

ndelangen
Copy link
Member

@ndelangen ndelangen commented Feb 14, 2024

Closes: #23972
Closes: #25814

What I did

Catch the error in loadMainConfig, and transform the error to be more helpful.

The user was able to do something before https://github.com/storybookjs/storybook/pull/23018/files but after that change, users can have a broken main.ts file simply by importing (with a declarative import) a module that has no commonjs variant.

This was an unnoticed breaking change in SB 7.1

Turning back the change is possible, but very much not preferable, because it would be a performance regression.

I hope that the message will be clear enough for users to act upon it.

Checklist for Contributors

Testing

The changes in this PR are covered in the following automated tests:

  • stories
  • unit tests
  • integration tests
  • end-to-end tests

Manual testing

  • Create a sandbox (doesn't matter if linked or not)
  • Add "@nuxt/kit": "3.10.1" as a dependency & install
  • Add the following to the sandbox's main.ts:
    import { buildNuxt, loadNuxt, tryUseNuxt } from '@nuxt/kit'
    console.log({ buildNuxt, loadNuxt, tryUseNuxt });
  • start the sandbox

Before this change the command output looked like:
Screenshot 2024-02-14 at 18 17 42

After this change it should look like:
Screenshot 2024-02-14 at 18 19 45

Documentation

  • Add or update documentation reflecting your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Checklist for Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli/src/sandbox-templates.ts

  • Make sure this PR contains one of the labels below:

    Available labels
    • bug: Internal changes that fixes incorrect behavior.
    • maintenance: User-facing maintenance tasks.
    • dependencies: Upgrading (sometimes downgrading) dependencies.
    • build: Internal-facing build tooling & test updates. Will not show up in release changelog.
    • cleanup: Minor cleanup style change. Will not show up in release changelog.
    • documentation: Documentation only changes. Will not show up in release changelog.
    • feature request: Introducing a new feature.
    • BREAKING CHANGE: Changes that break compatibility in some way with current major version.
    • other: Changes that don't fit in the above categories.

🦋 Canary release

This pull request has been released as version 0.0.0-pr-26035-sha-a321a58d. Try it out in a new sandbox by running npx storybook@0.0.0-pr-26035-sha-a321a58d sandbox or in an existing project with npx storybook@0.0.0-pr-26035-sha-a321a58d upgrade.

More information
Published version 0.0.0-pr-26035-sha-a321a58d
Triggered by @valentinpalkovic
Repository storybookjs/storybook
Branch norbert/handle-esm-main-error
Commit a321a58d
Datetime Tue Feb 20 10:53:48 UTC 2024 (1708426428)
Workflow run 7972315424

To request a new release of this pull request, mention the @storybookjs/core team.

core team members can create a new canary release here or locally with gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=26035

@ndelangen ndelangen added the bug label Feb 14, 2024
@ndelangen ndelangen self-assigned this Feb 14, 2024
@ndelangen ndelangen added patch:yes Bugfix & documentation PR that need to be picked to main branch core ci:normal labels Feb 14, 2024
@ndelangen ndelangen changed the title Core: Catch fail to lead main.ts over https://github.com/storybookjs/storybook/issues/23972 Core: Catch fail to lead main.ts over #23972 Feb 14, 2024
@valentinpalkovic
Copy link
Contributor

I personally haven’t ever heard the term „imperative import“ so far. I think the term „dynamic import" is far more common and is also used on docs sites like https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import or https://nodejs.org/api/esm.html#import-expressions

@vanessayuenn
Copy link
Contributor

@shilman actually suggested to add a config for this. is that still a viable option (in addition to catching the error)?

@valentinpalkovic
Copy link
Contributor

@ndelangen People might switch to dynamic imports on the top level. And AFAIK, top-level awaits aren't supported since esbuild transforms the main.ts file into a CommonJS one, where top-level awaits don't work. It may make sense to clarify this additionally.

@ndelangen
Copy link
Member Author

@valentinpalkovic yes, hence the " where they are used.`-part.

When they made this change, and run into the "top level await" error, they'll know what to do?

@ndelangen
Copy link
Member Author

@vanessayuenn @shilman where would this config exist?
In the main.ts file that we're failing to load? 😅

The only place I can imagine would be inside a CLI flag.
And I'm not a fan of adding a CLI flag for this.

@ndelangen ndelangen marked this pull request as ready for review February 15, 2024 12:55
@ndelangen
Copy link
Member Author

Screenshot 2024-02-15 at 14 31 23

@@ -81,6 +81,7 @@
"ts-dedent": "^2.0.0"
},
"devDependencies": {
"chalk": "^4.1.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this bring potential problems for the core-events package? as chalk would be prebundled in something that goes to the browser as well

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How, could it get pre-bundled into something that goes into the browser, due to this change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK this doesn't have an affect on any code we ship to the browser, as long as that browser code doesn't reference chalk.

I had to move chalk into here as @vanessayuenn asked me to move this to core-events: #26035 (comment)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also have a funny feeling about adding chalk to that package, but I'll leave that decision to you

@shilman
Copy link
Member

shilman commented Feb 15, 2024

I'm ok with this workaround rather than making it configurable.

However can we link show an example of a dynamic import in the error message? Do we support top level await or do they need to make async preset functions for that?

@ndelangen
Copy link
Member Author

@shilman do you mean like this:
#26035 (comment)

@ndelangen
Copy link
Member Author

ndelangen commented Feb 15, 2024

We do not support top-level await:

(norbert/handle-esm-main-error)⚡ [1] % nr storybook                                                 ~/Projects/Storybook/mono/storybook/sandbox/react-vite-default-ts
@storybook/cli v8.0.0-beta.2

Error: Transform failed with 1 error:
./.storybook/main.ts:6:0: ERROR: Top-level await is currently not supported with the "cjs" output format
    at failureErrorWithLog (./node_modules/esbuild/lib/main.js:1649:15)
    at ./node_modules/esbuild/lib/main.js:847:29
    at responseCallbacks.<computed> (./node_modules/esbuild/lib/main.js:703:9)
    at handleIncomingPacket (./node_modules/esbuild/lib/main.js:762:9)
    at Socket.readFromStdout (./node_modules/esbuild/lib/main.js:679:7)
    at Socket.emit (node:events:513:28)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Readable.push (node:internal/streams/readable:234:10)
    at Pipe.onStreamRead (node:internal/stream_base_commons:190:23)

WARN Broken build, fix the error above.
WARN You may need to refresh the browser.

We never have supported it.

@shilman
Copy link
Member

shilman commented Feb 15, 2024

Yes but what's the actual replacement code for that example?

const { } = await import('...');

Does that top level await work in main.ts? Can we actually spell it out for users?

@ndelangen
Copy link
Member Author

We do not support top-level await

Spelling it out for users would be extremely complex.
I do not see a way to do that, do you?

@ndelangen
Copy link
Member Author

We're going to add an example of converting from static import to dynamic import..
We'll do that in a comment in the original issue.
We'll link to that comment from the error.

@ndelangen
Copy link
Member Author

Here's what it looks like when there's an evaluation/transform error inside of main.ts:
Screenshot 2024-02-16 at 16 25 35

This is how it looks when there's an static import we do not support:
Screenshot 2024-02-16 at 16 27 16

Here's how a runtime error looks like:
Screenshot 2024-02-16 at 16 25 53

And here's when main.ts cannot be found:
Screenshot 2024-02-16 at 16 30 01

Everything through Error classes in server-errors 🎉

@ndelangen
Copy link
Member Author

Here's the link to the instruction to users:
#23972 (comment)

ndelangen and others added 2 commits February 20, 2024 12:12
Co-authored-by: Yann Braga <yannbf@gmail.com>
@@ -81,6 +81,7 @@
"ts-dedent": "^2.0.0"
},
"devDependencies": {
"chalk": "^4.1.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also have a funny feeling about adding chalk to that package, but I'll leave that decision to you

@ndelangen
Copy link
Member Author

ndelangen commented Feb 20, 2024

What's the funny feeling?
Should I just let the full error-body be all-red in the terminal?
Should I use something else?

What are the concerns with this?

@ndelangen ndelangen merged commit b0263eb into next Feb 20, 2024
57 of 58 checks passed
@ndelangen ndelangen deleted the norbert/handle-esm-main-error branch February 20, 2024 15:47
@shilman shilman changed the title Core: Catch fail to lead main.ts over #23972 Core: Make fail to load main.ts error useful Feb 20, 2024
@shilman shilman changed the title Core: Make fail to load main.ts error useful Core: Fix fail to load main.ts error message Feb 20, 2024
@github-actions github-actions bot mentioned this pull request Mar 12, 2024
11 tasks
@github-actions github-actions bot mentioned this pull request Mar 19, 2024
11 tasks
@shilman shilman added the patch:done Patch/release PRs already cherry-picked to main/release branch label Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug ci:normal core patch:done Patch/release PRs already cherry-picked to main/release branch patch:yes Bugfix & documentation PR that need to be picked to main branch
Projects
None yet
5 participants