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

fix: Fix ECMAScript analyzer #8656

Merged
merged 15 commits into from
Jul 4, 2024
Merged

fix: Fix ECMAScript analyzer #8656

merged 15 commits into from
Jul 4, 2024

Conversation

kdy1
Copy link
Member

@kdy1 kdy1 commented Jul 3, 2024

Description

I faced this bug while working on #8523. Tree shaking inevitably creates direct imports for let bindings, and when the LHS of += is imported/exported, it triggers a bug.

The problematic input file looks like

// Combined load times for loading client components
let clientComponentLoadStart = 0
let clientComponentLoadTimes = 0
let clientComponentLoadCount = 0

export function wrapClientComponentLoader(ComponentMod: any) {
  if (!('performance' in globalThis)) {
    return ComponentMod.__next_app__
  }

  return {
    require: (...args: any[]) => {
      const startTime = performance.now()

      if (clientComponentLoadStart === 0) {
        clientComponentLoadStart = startTime
      }

      try {
        clientComponentLoadCount += 1
        return ComponentMod.__next_app__.require(...args)
      } finally {
        clientComponentLoadTimes += performance.now() - startTime
      }
    },
    loadChunk: (...args: any[]) => {
      const startTime = performance.now()
      try {
        clientComponentLoadCount += 1
        return ComponentMod.__next_app__.loadChunk(...args)
      } finally {
        clientComponentLoadTimes += performance.now() - startTime
      }
    },
  }
}

export function getClientComponentLoaderMetrics(
  options: { reset?: boolean } = {}
) {
  const metrics =
    clientComponentLoadStart === 0
      ? undefined
      : {
          clientComponentLoadStart,
          clientComponentLoadTimes,
          clientComponentLoadCount,
        }

  if (options.reset) {
    clientComponentLoadStart = 0
    clientComponentLoadTimes = 0
    clientComponentLoadCount = 0
  }

  return metrics
}

and it works if I apply this PR or I replace clientComponentLoadTimes += performance.now() - startTime with clientComponentLoadTimes = clientComponentLoadTimes + performance.now() - startTime.

Testing Instructions

Test references changed quite a lot.

Copy link

vercel bot commented Jul 3, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
rust-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 3, 2024 2:23pm
9 Skipped Deployments
Name Status Preview Comments Updated (UTC)
examples-basic-web ⬜️ Ignored (Inspect) Visit Preview Jul 3, 2024 2:23pm
examples-designsystem-docs ⬜️ Ignored (Inspect) Visit Preview Jul 3, 2024 2:23pm
examples-gatsby-web ⬜️ Ignored (Inspect) Visit Preview Jul 3, 2024 2:23pm
examples-kitchensink-blog ⬜️ Ignored (Inspect) Visit Preview Jul 3, 2024 2:23pm
examples-native-web ⬜️ Ignored (Inspect) Visit Preview Jul 3, 2024 2:23pm
examples-svelte-web ⬜️ Ignored (Inspect) Visit Preview Jul 3, 2024 2:23pm
examples-tailwind-web ⬜️ Ignored (Inspect) Visit Preview Jul 3, 2024 2:23pm
examples-vite-web ⬜️ Ignored (Inspect) Visit Preview Jul 3, 2024 2:23pm
examples-nonmonorepo ⬜️ Skipped (Inspect) Jul 3, 2024 2:23pm

Copy link
Contributor

github-actions bot commented Jul 3, 2024

✅ This change can build next-swc

@kdy1 kdy1 marked this pull request as ready for review July 3, 2024 03:59
@kdy1 kdy1 requested a review from a team as a code owner July 3, 2024 03:59
Copy link
Contributor

github-actions bot commented Jul 3, 2024

🟢 Turbopack Benchmark CI successful 🟢

Thanks

Copy link
Contributor

github-actions bot commented Jul 3, 2024

⚠️ CI failed ⚠️

The following steps have failed in CI:

  • Turbopack Rust tests (mac/win, non-blocking)

See workflow summary for details

@kdy1 kdy1 marked this pull request as draft July 3, 2024 05:40
Comment on lines 1 to 3
a = ("" | `${a}x` | ???*0*)
- *0* a
⚠️ pattern without value
Copy link
Member

Choose a reason for hiding this comment

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

That doesn't look correct

@vercel vercel bot temporarily deployed to Preview – examples-nonmonorepo July 3, 2024 08:21 Inactive
kdy1 added a commit that referenced this pull request Jul 3, 2024
Copy link
Member

@sokra sokra left a comment

Choose a reason for hiding this comment

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

a test case would be great

@vercel vercel bot temporarily deployed to Preview – examples-nonmonorepo July 3, 2024 14:20 Inactive
@kdy1 kdy1 self-assigned this Jul 3, 2024
@kdy1 kdy1 requested a review from sokra July 3, 2024 14:21
@kdy1 kdy1 marked this pull request as ready for review July 3, 2024 14:21
@kdy1 kdy1 changed the title fix: Fix the let bug fix: Fix ECMAScript analyzer Jul 3, 2024
@kdy1 kdy1 merged commit ca9b635 into main Jul 4, 2024
56 of 58 checks passed
@kdy1 kdy1 deleted the kdy1/let-bugfix branch July 4, 2024 01:51
ForsakenHarmony pushed a commit to vercel/next.js that referenced this pull request Jul 25, 2024
### Description

I faced this bug while working on
vercel/turborepo#8523. Tree shaking inevitably
creates direct imports for let bindings, and when the LHS of `+=` is
imported/exported, it triggers a bug.

The problematic input file looks like

```js
// Combined load times for loading client components
let clientComponentLoadStart = 0
let clientComponentLoadTimes = 0
let clientComponentLoadCount = 0

export function wrapClientComponentLoader(ComponentMod: any) {
  if (!('performance' in globalThis)) {
    return ComponentMod.__next_app__
  }

  return {
    require: (...args: any[]) => {
      const startTime = performance.now()

      if (clientComponentLoadStart === 0) {
        clientComponentLoadStart = startTime
      }

      try {
        clientComponentLoadCount += 1
        return ComponentMod.__next_app__.require(...args)
      } finally {
        clientComponentLoadTimes += performance.now() - startTime
      }
    },
    loadChunk: (...args: any[]) => {
      const startTime = performance.now()
      try {
        clientComponentLoadCount += 1
        return ComponentMod.__next_app__.loadChunk(...args)
      } finally {
        clientComponentLoadTimes += performance.now() - startTime
      }
    },
  }
}

export function getClientComponentLoaderMetrics(
  options: { reset?: boolean } = {}
) {
  const metrics =
    clientComponentLoadStart === 0
      ? undefined
      : {
          clientComponentLoadStart,
          clientComponentLoadTimes,
          clientComponentLoadCount,
        }

  if (options.reset) {
    clientComponentLoadStart = 0
    clientComponentLoadTimes = 0
    clientComponentLoadCount = 0
  }

  return metrics
}

```

and it works if I apply this PR or I replace `clientComponentLoadTimes
+= performance.now() - startTime` with `clientComponentLoadTimes =
clientComponentLoadTimes + performance.now() - startTime`.


### Testing Instructions

Test references changed quite a lot.
ForsakenHarmony pushed a commit to vercel/next.js that referenced this pull request Jul 29, 2024
### Description

I faced this bug while working on
vercel/turborepo#8523. Tree shaking inevitably
creates direct imports for let bindings, and when the LHS of `+=` is
imported/exported, it triggers a bug.

The problematic input file looks like

```js
// Combined load times for loading client components
let clientComponentLoadStart = 0
let clientComponentLoadTimes = 0
let clientComponentLoadCount = 0

export function wrapClientComponentLoader(ComponentMod: any) {
  if (!('performance' in globalThis)) {
    return ComponentMod.__next_app__
  }

  return {
    require: (...args: any[]) => {
      const startTime = performance.now()

      if (clientComponentLoadStart === 0) {
        clientComponentLoadStart = startTime
      }

      try {
        clientComponentLoadCount += 1
        return ComponentMod.__next_app__.require(...args)
      } finally {
        clientComponentLoadTimes += performance.now() - startTime
      }
    },
    loadChunk: (...args: any[]) => {
      const startTime = performance.now()
      try {
        clientComponentLoadCount += 1
        return ComponentMod.__next_app__.loadChunk(...args)
      } finally {
        clientComponentLoadTimes += performance.now() - startTime
      }
    },
  }
}

export function getClientComponentLoaderMetrics(
  options: { reset?: boolean } = {}
) {
  const metrics =
    clientComponentLoadStart === 0
      ? undefined
      : {
          clientComponentLoadStart,
          clientComponentLoadTimes,
          clientComponentLoadCount,
        }

  if (options.reset) {
    clientComponentLoadStart = 0
    clientComponentLoadTimes = 0
    clientComponentLoadCount = 0
  }

  return metrics
}

```

and it works if I apply this PR or I replace `clientComponentLoadTimes
+= performance.now() - startTime` with `clientComponentLoadTimes =
clientComponentLoadTimes + performance.now() - startTime`.


### Testing Instructions

Test references changed quite a lot.
ForsakenHarmony pushed a commit to vercel/next.js that referenced this pull request Jul 29, 2024
### Description

I faced this bug while working on
vercel/turborepo#8523. Tree shaking inevitably
creates direct imports for let bindings, and when the LHS of `+=` is
imported/exported, it triggers a bug.

The problematic input file looks like

```js
// Combined load times for loading client components
let clientComponentLoadStart = 0
let clientComponentLoadTimes = 0
let clientComponentLoadCount = 0

export function wrapClientComponentLoader(ComponentMod: any) {
  if (!('performance' in globalThis)) {
    return ComponentMod.__next_app__
  }

  return {
    require: (...args: any[]) => {
      const startTime = performance.now()

      if (clientComponentLoadStart === 0) {
        clientComponentLoadStart = startTime
      }

      try {
        clientComponentLoadCount += 1
        return ComponentMod.__next_app__.require(...args)
      } finally {
        clientComponentLoadTimes += performance.now() - startTime
      }
    },
    loadChunk: (...args: any[]) => {
      const startTime = performance.now()
      try {
        clientComponentLoadCount += 1
        return ComponentMod.__next_app__.loadChunk(...args)
      } finally {
        clientComponentLoadTimes += performance.now() - startTime
      }
    },
  }
}

export function getClientComponentLoaderMetrics(
  options: { reset?: boolean } = {}
) {
  const metrics =
    clientComponentLoadStart === 0
      ? undefined
      : {
          clientComponentLoadStart,
          clientComponentLoadTimes,
          clientComponentLoadCount,
        }

  if (options.reset) {
    clientComponentLoadStart = 0
    clientComponentLoadTimes = 0
    clientComponentLoadCount = 0
  }

  return metrics
}

```

and it works if I apply this PR or I replace `clientComponentLoadTimes
+= performance.now() - startTime` with `clientComponentLoadTimes =
clientComponentLoadTimes + performance.now() - startTime`.


### Testing Instructions

Test references changed quite a lot.
ForsakenHarmony pushed a commit to vercel/next.js that referenced this pull request Aug 1, 2024
### Description

I faced this bug while working on
vercel/turborepo#8523. Tree shaking inevitably
creates direct imports for let bindings, and when the LHS of `+=` is
imported/exported, it triggers a bug.

The problematic input file looks like

```js
// Combined load times for loading client components
let clientComponentLoadStart = 0
let clientComponentLoadTimes = 0
let clientComponentLoadCount = 0

export function wrapClientComponentLoader(ComponentMod: any) {
  if (!('performance' in globalThis)) {
    return ComponentMod.__next_app__
  }

  return {
    require: (...args: any[]) => {
      const startTime = performance.now()

      if (clientComponentLoadStart === 0) {
        clientComponentLoadStart = startTime
      }

      try {
        clientComponentLoadCount += 1
        return ComponentMod.__next_app__.require(...args)
      } finally {
        clientComponentLoadTimes += performance.now() - startTime
      }
    },
    loadChunk: (...args: any[]) => {
      const startTime = performance.now()
      try {
        clientComponentLoadCount += 1
        return ComponentMod.__next_app__.loadChunk(...args)
      } finally {
        clientComponentLoadTimes += performance.now() - startTime
      }
    },
  }
}

export function getClientComponentLoaderMetrics(
  options: { reset?: boolean } = {}
) {
  const metrics =
    clientComponentLoadStart === 0
      ? undefined
      : {
          clientComponentLoadStart,
          clientComponentLoadTimes,
          clientComponentLoadCount,
        }

  if (options.reset) {
    clientComponentLoadStart = 0
    clientComponentLoadTimes = 0
    clientComponentLoadCount = 0
  }

  return metrics
}

```

and it works if I apply this PR or I replace `clientComponentLoadTimes
+= performance.now() - startTime` with `clientComponentLoadTimes =
clientComponentLoadTimes + performance.now() - startTime`.


### Testing Instructions

Test references changed quite a lot.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants