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
very dynamic requests will only lead to a warning #7640
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
crates/turbopack-ecmascript/src/references/dynamic_expression.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use anyhow::Result; | ||
use serde::{Deserialize, Serialize}; | ||
use swc_core::quote; | ||
use turbo_tasks::{debug::ValueDebugFormat, trace::TraceRawVcs, Vc}; | ||
|
||
use super::AstPath; | ||
use crate::{ | ||
chunk::EcmascriptChunkingContext, | ||
code_gen::{CodeGenerateable, CodeGeneration}, | ||
create_visitor, | ||
}; | ||
|
||
#[derive(PartialEq, Eq, TraceRawVcs, Serialize, Deserialize, ValueDebugFormat)] | ||
enum DynamicExpressionType { | ||
Promise, | ||
Normal, | ||
} | ||
|
||
#[turbo_tasks::value] | ||
pub struct DynamicExpression { | ||
path: Vc<AstPath>, | ||
ty: DynamicExpressionType, | ||
} | ||
|
||
#[turbo_tasks::value_impl] | ||
impl DynamicExpression { | ||
#[turbo_tasks::function] | ||
pub fn new(path: Vc<AstPath>) -> Vc<Self> { | ||
Self::cell(DynamicExpression { | ||
path, | ||
ty: DynamicExpressionType::Normal, | ||
}) | ||
} | ||
|
||
#[turbo_tasks::function] | ||
pub fn new_promise(path: Vc<AstPath>) -> Vc<Self> { | ||
Self::cell(DynamicExpression { | ||
path, | ||
ty: DynamicExpressionType::Promise, | ||
}) | ||
} | ||
} | ||
|
||
#[turbo_tasks::value_impl] | ||
impl CodeGenerateable for DynamicExpression { | ||
#[turbo_tasks::function] | ||
async fn code_generation( | ||
&self, | ||
_context: Vc<Box<dyn EcmascriptChunkingContext>>, | ||
) -> Result<Vc<CodeGeneration>> { | ||
let path = &self.path.await?; | ||
|
||
let visitor = match self.ty { | ||
DynamicExpressionType::Normal => { | ||
create_visitor!(path, visit_mut_expr(expr: &mut Expr) { | ||
*expr = quote!("(() => { const e = new Error(\"Cannot find module as expression is too dynamic\"); e.code = 'MODULE_NOT_FOUND'; throw e; })()" as Expr); | ||
}) | ||
} | ||
DynamicExpressionType::Promise => { | ||
create_visitor!(path, visit_mut_expr(expr: &mut Expr) { | ||
*expr = quote!("Promise.resolve().then(() => { const e = new Error(\"Cannot find module as expression is too dynamic\"); e.code = 'MODULE_NOT_FOUND'; throw e; })" as Expr); | ||
}) | ||
} | ||
}; | ||
|
||
Ok(CodeGeneration { | ||
visitors: vec![visitor], | ||
} | ||
.cell()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
crates/turbopack-tests/tests/snapshot/dynamic-request/very-dynamic/input/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import child_process from "node:child_process"; | ||
import fs, { readFileSync } from "node:fs"; | ||
|
||
const unknown = Math.random(); | ||
|
||
child_process.spawnSync(unknown); | ||
child_process.spawnSync("node", unknown); | ||
child_process.spawnSync("node", [unknown, unknown]); | ||
|
||
require(unknown); | ||
|
||
import(unknown); | ||
|
||
fs.readFileSync(unknown); | ||
readFileSync(unknown); | ||
|
||
new URL(unknown, import.meta.url); |
13 changes: 13 additions & 0 deletions
13
...request/very-dynamic/issues/__l___lint TP1001__ import(FreeVar(Math)[__quo__ra-fcce98.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
warning - [analysis] [project]/crates/turbopack-tests/tests/snapshot/dynamic-request/very-dynamic/input/index.js /crates/turbopack-tests/tests/snapshot/dynamic-request/very-dynamic/input/index.js:12:0 lint TP1001 import(FreeVar(Math)["random"]()) is very dynamic | ||
|
||
8 | child_process.spawnSync("node", [unknown, unknown]); | ||
9 | | ||
10 | require(unknown); | ||
11 | | ||
+ v-------------v | ||
12 + import(unknown); | ||
+ ^-------------^ | ||
13 | | ||
14 | fs.readFileSync(unknown); | ||
15 | readFileSync(unknown); | ||
16 | |
13 changes: 13 additions & 0 deletions
13
...request/very-dynamic/issues/__l___lint TP1002__ require(FreeVar(Math)[__quo__r-fa7bb4.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
warning - [analysis] [project]/crates/turbopack-tests/tests/snapshot/dynamic-request/very-dynamic/input/index.js /crates/turbopack-tests/tests/snapshot/dynamic-request/very-dynamic/input/index.js:10:0 lint TP1002 require(FreeVar(Math)["random"]()) is very dynamic | ||
|
||
6 | child_process.spawnSync(unknown); | ||
7 | child_process.spawnSync("node", unknown); | ||
8 | child_process.spawnSync("node", [unknown, unknown]); | ||
9 | | ||
+ v--------------v | ||
10 + require(unknown); | ||
+ ^--------------^ | ||
11 | | ||
12 | import(unknown); | ||
13 | | ||
14 | fs.readFileSync(unknown); |
12 changes: 12 additions & 0 deletions
12
...request/very-dynamic/issues/__l___lint TP1004__ fs.readFileSync(FreeVar(Math)[-45dc7a.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
warning - [analysis] [project]/crates/turbopack-tests/tests/snapshot/dynamic-request/very-dynamic/input/index.js /crates/turbopack-tests/tests/snapshot/dynamic-request/very-dynamic/input/index.js:15:0 lint TP1004 fs.readFileSync(FreeVar(Math)["random"]()) is very dynamic | ||
|
||
11 | | ||
12 | import(unknown); | ||
13 | | ||
14 | fs.readFileSync(unknown); | ||
+ v-------------------v | ||
15 + readFileSync(unknown); | ||
+ ^-------------------^ | ||
16 | | ||
17 | new URL(unknown, import.meta.url); | ||
18 | |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will completely throw away information about what would have been called
can we add something back so it shows up in the stack trace?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Giving the inline function a name would probably work
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to source mapping the error will point to the actual user code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But the stack trace still won't contain a
require
/import