Skip to content

Commit af7a82e

Browse files
committed
feat: add native source maps support
1 parent 578c090 commit af7a82e

File tree

27 files changed

+221
-45
lines changed

27 files changed

+221
-45
lines changed

Cargo.lock

Lines changed: 18 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ repository = "https://github.com/denoland/deno"
6161

6262
[workspace.dependencies]
6363
deno_ast = { version = "=0.50.3", features = ["transpiling"] }
64-
deno_core = { version = "0.368.0" }
64+
deno_core = { version = "0.369.0" }
6565

6666
deno_cache_dir = "=0.25.0"
6767
deno_doc = "=0.186.0"

cli/module_loader.rs

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,8 @@ impl<TGraphContainer: ModuleGraphContainer> ModuleLoader
12181218
"wasm" | "file" | "http" | "https" | "data" | "blob" => (),
12191219
_ => return None,
12201220
}
1221+
1222+
// Load the prepared module and extract inline source map
12211223
let graph = self.0.graph_container.graph();
12221224
let source = self
12231225
.0
@@ -1228,29 +1230,76 @@ impl<TGraphContainer: ModuleGraphContainer> ModuleLoader
12281230
source_map_from_code(source.source.as_bytes()).map(Cow::Owned)
12291231
}
12301232

1233+
fn load_external_source_map(
1234+
&self,
1235+
source_map_url: &str,
1236+
) -> Option<Cow<'_, [u8]>> {
1237+
let specifier = resolve_url(source_map_url).ok()?;
1238+
1239+
if let Ok(Some(file)) = self
1240+
.0
1241+
.shared
1242+
.file_fetcher
1243+
.get_cached_source_or_local(&specifier)
1244+
{
1245+
return Some(Cow::Owned(file.source.to_vec()));
1246+
}
1247+
1248+
None
1249+
}
1250+
12311251
fn get_source_mapped_source_line(
12321252
&self,
12331253
file_name: &str,
12341254
line_number: usize,
12351255
) -> Option<String> {
1256+
let specifier = resolve_url(file_name).ok()?;
12361257
let graph = self.0.graph_container.graph();
1237-
let code = match graph.get(&resolve_url(file_name).ok()?) {
1258+
1259+
let code = match graph.get(&specifier) {
12381260
Some(deno_graph::Module::Js(module)) => &module.source.text,
12391261
Some(deno_graph::Module::Json(module)) => &module.source.text,
1240-
_ => return None,
1262+
Some(
1263+
deno_graph::Module::Wasm(_)
1264+
| deno_graph::Module::Npm(_)
1265+
| deno_graph::Module::Node(_)
1266+
| deno_graph::Module::External(_),
1267+
) => {
1268+
return None;
1269+
}
1270+
None => {
1271+
// Not in graph, try to read from file system (for source-mapped original files)
1272+
if let Ok(Some(file)) = self
1273+
.0
1274+
.shared
1275+
.file_fetcher
1276+
.get_cached_source_or_local(&specifier)
1277+
{
1278+
return extract_source_line(
1279+
&String::from_utf8_lossy(&file.source),
1280+
line_number,
1281+
);
1282+
} else {
1283+
return None;
1284+
}
1285+
}
12411286
};
1242-
// Do NOT use .lines(): it skips the terminating empty line.
1243-
// (due to internally using_terminator() instead of .split())
1244-
let lines: Vec<&str> = code.split('\n').collect();
1245-
if line_number >= lines.len() {
1246-
Some(format!(
1247-
"{} Couldn't format source line: Line {} is out of bounds (source may have changed at runtime)",
1248-
crate::colors::yellow("Warning"),
1249-
line_number + 1,
1250-
))
1251-
} else {
1252-
Some(lines[line_number].to_string())
1253-
}
1287+
1288+
extract_source_line(code, line_number)
1289+
}
1290+
}
1291+
1292+
/// Extracts a specific line from source code text.
1293+
fn extract_source_line(text: &str, line_number: usize) -> Option<String> {
1294+
// Do NOT use .lines(): it skips the terminating empty line.
1295+
// (due to internally using_terminator() instead of .split())
1296+
match text.split('\n').nth(line_number) {
1297+
Some(line) => Some(line.to_string()),
1298+
None => Some(format!(
1299+
"{} Couldn't format source line: Line {} is out of bounds (source may have changed at runtime)",
1300+
crate::colors::yellow("Warning"),
1301+
line_number + 1,
1302+
)),
12541303
}
12551304
}
12561305

cli/rt/run.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,15 @@ impl ModuleLoader for EmbeddedModuleLoader {
583583
data.source_map
584584
}
585585

586+
fn load_external_source_map(
587+
&self,
588+
source_map_url: &str,
589+
) -> Option<Cow<'_, [u8]>> {
590+
let url = Url::parse(source_map_url).ok()?;
591+
let data = self.shared.modules.read(&url).ok()??;
592+
Some(Cow::Owned(data.data.to_vec()))
593+
}
594+
586595
fn get_source_mapped_source_line(
587596
&self,
588597
file_name: &str,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
1
22
1
33
error: Uncaught (in promise) TypeError: this.otherMethod is not a function
4+
return this.otherMethod();
5+
^
46
at getValue (file://[WILDCARD]/@denotest/cjs-this-in-exports/1.0.0/index.js:3:17)
57
at file://[WILDCARD]/main.js:11:1
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"args": "run external.js",
3+
"output": "external.js.out",
4+
"exitCode": 1
5+
}

tests/specs/run/sourcemap_external/external.js

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/specs/run/sourcemap_external/external.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
error: Uncaught (in promise) Error: Test error with external sourcemap
2+
throw new Error("Test error with external sourcemap");
3+
^
4+
at throwError ([WILDCARD]original.ts:3:3)
5+
at [WILDCARD]original.ts:6:14
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Original TypeScript file
2+
function throwError() {
3+
throw new Error("Test error with external sourcemap");
4+
}
5+
6+
throwError();

0 commit comments

Comments
 (0)