Repository: vercel/next.js
Pull request: Turbopack: add lint rule to not cell in async map

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the PR.
- Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to understand the PR)
- When linking to a Slack thread, you might want to share details of the conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Changed files:
filename: .config/ast-grep/rule-tests/__snapshots__/no-map-async-cell-snapshot.yml
patch: @@ -0,0 +1,282 @@
+id: no-map-async-cell
+snapshots:
+  ? |
+    items.into_iter()
+        .map(async |item| Ok(item.process().await?.cell()))
+        .try_join()
+        .await
+  : labels:
+      - source: item.process().await?.cell()
+        style: primary
+        start: 43
+        end: 71
+      - source: |-
+          items.into_iter()
+              .map(async |item| Ok(item.process().await?.cell()))
+        style: secondary
+        start: 0
+        end: 73
+      - source: async |item| Ok(item.process().await?.cell())
+        style: secondary
+        start: 27
+        end: 72
+  ? |
+    items.into_iter()
+        .map(async |item| item.process().await.cell())
+        .try_join()
+        .await
+  : labels:
+      - source: |-
+          items.into_iter()
+              .map(async |item| item.process().await.cell())
+        style: primary
+        start: 0
+        end: 68
+      - source: item.process().await.cell()
+        style: secondary
+        start: 40
+        end: 67
+  ? |
+    items.into_iter()
+        .map(async |item| {
+            Ok(item.process().await?.cell())
+        })
+        .try_join()
+        .await
+  : labels:
+      - source: item.process().await?.cell()
+        style: primary
+        start: 53
+        end: 81
+      - source: |-
+          items.into_iter()
+              .map(async |item| {
+                  Ok(item.process().await?.cell())
+              })
+        style: secondary
+        start: 0
+        end: 89
+      - source: |-
+          async |item| {
+                  Ok(item.process().await?.cell())
+              }
+        style: secondary
+        start: 27
+        end: 88
+  ? |
+    items.into_iter()
+        .map(async |item| {
+            item.process().await.cell()
+        })
+        .try_join()
+        .await
+  : labels:
+      - source: |-
+          items.into_iter()
+              .map(async |item| {
+                  item.process().await.cell()
+              })
+        style: primary
+        start: 0
+        end: 84
+      - source: item.process().await.cell()
+        style: secondary
+        start: 50
+        end: 77
+  ? |
+    items.into_iter()
+        .map(|item| async move {
+            Ok(item.process().await?.cell())
+        })
+        .try_join()
+        .await
+  : labels:
+      - source: item.process().await?.cell()
+        style: primary
+        start: 58
+        end: 86
+      - source: |-
+          items.into_iter()
+              .map(|item| async move {
+                  Ok(item.process().await?.cell())
+              })
+        style: secondary
+        start: 0
+        end: 94
+      - source: |-
+          |item| async move {
+                  Ok(item.process().await?.cell())
+              }
+        style: secondary
+        start: 27
+        end: 93
+  ? |
+    items.into_iter()
+        .map(|item| async {
+            Ok(item.process().await?.cell())
+        })
+        .try_join()
+        .await
+  : labels:
+      - source: item.process().await?.cell()
+        style: primary
+        start: 53
+        end: 81
+      - source: |-
+          items.into_iter()
+              .map(|item| async {
+                  Ok(item.process().await?.cell())
+              })
+        style: secondary
+        start: 0
+        end: 89
+      - source: |-
+          |item| async {
+                  Ok(item.process().await?.cell())
+              }
+        style: secondary
+        start: 27
+        end: 88
+  ? |
+    map.into_iter()
+        .map(|(ty, items)| {
+            (
+                ty,
+                ChunkItemBatchGroup {
+                    items,
+                    chunk_groups: this.chunk_groups.clone(),
+                },
+            )
+        })
+        .map(async |(ty, batch_group)| Ok((ty, batch_group.resolved_cell())))
+        .try_join()
+  : labels:
+      - source: batch_group.resolved_cell()
+        style: primary
+        start: 256
+        end: 283
+      - source: |-
+          map.into_iter()
+              .map(|(ty, items)| {
+                  (
+                      ty,
+                      ChunkItemBatchGroup {
+                          items,
+                          chunk_groups: this.chunk_groups.clone(),
+                      },
+                  )
+              })
+              .map(async |(ty, batch_group)| Ok((ty, batch_group.resolved_cell())))
+        style: secondary
+        start: 0
+        end: 286
+      - source: async |(ty, batch_group)| Ok((ty, batch_group.resolved_cell()))
+        style: secondary
+        start: 222
+        end: 285
+  ? |
+    modules
+        .map(async |module| {
+            let result = module.compute().await?;
+            result.into_value().cell()
+        })
+  : labels:
+      - source: result.into_value().cell()
+        style: primary
+        start: 88
+        end: 114
+      - source: |-
+          modules
+              .map(async |module| {
+                  let result = module.compute().await?;
+                  result.into_value().cell()
+              })
+        style: secondary
+        start: 0
+        end: 121
+      - source: |-
+          async |module| {
+                  let result = module.compute().await?;
+                  result.into_value().cell()
+              }
+        style: secondary
+        start: 17
+        end: 120
+  ? |
+    modules
+        .map(async |module| {
+            let result = module.compute().await?;
+            result.into_value().cell()
+        })
+        .try_join()
+  : labels:
+      - source: |-
+          modules
+              .map(async |module| {
+                  let result = module.compute().await?;
+                  result.into_value().cell()
+              })
+        style: primary
+        start: 0
+        end: 121
+      - source: result.into_value().cell()
+        style: secondary
+        start: 88
+        end: 114
+  ? |
+    paths.map(async |(path, asset)| {
+        AssetBinding {
+            name: wasm_edge_var_name(Vc::upcast(*asset)).owned().await?,
+        }.cell()
+    })
+  : labels:
+      - source: |-
+          paths.map(async |(path, asset)| {
+              AssetBinding {
+                  name: wasm_edge_var_name(Vc::upcast(*asset)).owned().await?,
+              }.cell()
+          })
+        style: primary
+        start: 0
+        end: 137
+      - source: |-
+          AssetBinding {
+                  name: wasm_edge_var_name(Vc::upcast(*asset)).owned().await?,
+              }.cell()
+        style: secondary
+        start: 38
+        end: 134
+  ? |
+    paths.map(async |(path, asset)| {
+        Ok(AssetBinding {
+            name: wasm_edge_var_name(Vc::upcast(*asset)).owned().await?,
+        }.cell())
+    })
+    .try_join()
+    .await
+  : labels:
+      - source: |-
+          AssetBinding {
+                  name: wasm_edge_var_name(Vc::upcast(*asset)).owned().await?,
+              }.cell()
+        style: primary
+        start: 41
+        end: 137
+      - source: |-
+          paths.map(async |(path, asset)| {
+              Ok(AssetBinding {
+                  name: wasm_edge_var_name(Vc::upcast(*asset)).owned().await?,
+              }.cell())
+          })
+        style: secondary
+        start: 0
+        end: 141
+      - source: |-
+          async |(path, asset)| {
+              Ok(AssetBinding {
+                  name: wasm_edge_var_name(Vc::upcast(*asset)).owned().await?,
+              }.cell())
+          }
+        style: secondary
+        start: 10
+        end: 140

filename: .config/ast-grep/rule-tests/no-map-async-cell-test.yml
patch: @@ -0,0 +1,70 @@
+id: no-map-async-cell
+valid:
+  - |
+    items.into_iter()
+        .map(|item| item.process())
+        .collect()
+  - |
+    items.into_iter()
+        .map(|item| {
+            SomeStruct { field: item }.cell()
+        })
+        .collect()
+  - |
+    call_once(|| async {
+        X{}.cell()
+    })
+invalid:
+  - |
+    items.into_iter()
+        .map(|item| async move {
+            Ok(item.process().await?.cell())
+        })
+        .try_join()
+        .await
+  - |
+    items.into_iter()
+        .map(|item| async {
+            Ok(item.process().await?.cell())
+        })
+        .try_join()
+        .await
+  - |
+    items.into_iter()
+        .map(async |item| {
+            Ok(item.process().await?.cell())
+        })
+        .try_join()
+        .await
+  - |
+    items.into_iter()
+        .map(async |item| Ok(item.process().await?.cell()))
+        .try_join()
+        .await
+  - |
+    paths.map(async |(path, asset)| {
+        Ok(AssetBinding {
+            name: wasm_edge_var_name(Vc::upcast(*asset)).owned().await?,
+        }.cell())
+    })
+    .try_join()
+    .await
+  - |
+    modules
+        .map(async |module| {
+            let result = module.compute().await?;
+            result.into_value().cell()
+        })
+  - |
+    map.into_iter()
+        .map(|(ty, items)| {
+            (
+                ty,
+                ChunkItemBatchGroup {
+                    items,
+                    chunk_groups: this.chunk_groups.clone(),
+                },
+            )
+        })
+        .map(async |(ty, batch_group)| Ok((ty, batch_group.resolved_cell())))
+        .try_join()

filename: .config/ast-grep/rules/no-map-async-cell.yml
patch: @@ -0,0 +1,23 @@
+# yaml-language-server: $schema=https://raw.githubusercontent.com/ast-grep/ast-grep/main/schemas/rule.json
+
+id: no-map-async-cell
+message: Using `.map(async |...| { ...cell() })` causes non-deterministic cell order
+note: You probably want to first `try_join()` the async computations before wrapping them in a cell.
+severity: error
+language: Rust
+rule:
+  all:
+    - any:
+        - pattern: $X.cell()
+        - pattern: $X.resolved_cell()
+    - inside:
+        stopBy: end
+        all:
+          - any:
+              - pattern: 'async |$$$ARGS| $BODY'
+              - pattern: '|$$$ARGS| async { $$$BODY }'
+              - pattern: '|$$$ARGS| async move { $$$BODY }'
+          - inside:
+              stopBy: end
+              field: arguments
+              pattern: $EXPR.map($$$CLOSURE)