Skip to content

Commit

Permalink
fix: source maps in inspector for local files (denoland#5245)
Browse files Browse the repository at this point in the history
This commit changes handling of source maps by setting proper
file URLs for "file" and "sources" fields in the source map.
  • Loading branch information
bartlomieju committed May 12, 2020
1 parent 593c3aa commit 053c568
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion cli/tsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use regex::Regex;
use serde::Deserialize;
use serde_json::json;
use serde_json::Value;
use sourcemap::SourceMap;
use std::collections::HashMap;
use std::collections::HashSet;
use std::fs;
Expand Down Expand Up @@ -718,10 +719,27 @@ impl TsCompiler {
return Ok(());
}

let js_key = self
.disk_cache
.get_cache_filename_with_extension(module_specifier.as_url(), "js");
let js_path = self.disk_cache.location.join(js_key);
let js_file_url =
Url::from_file_path(js_path).expect("Bad file URL for file");

let source_map_key = self
.disk_cache
.get_cache_filename_with_extension(module_specifier.as_url(), "js.map");
self.disk_cache.set(&source_map_key, contents.as_bytes())

let mut sm = SourceMap::from_slice(contents.as_bytes())
.expect("Invalid source map content");
sm.set_file(Some(&js_file_url.to_string()));
sm.set_source(0, &module_specifier.to_string());

let mut output: Vec<u8> = vec![];
sm.to_writer(&mut output)
.expect("Failed to write source map");

self.disk_cache.set(&source_map_key, &output)
}
}

Expand Down Expand Up @@ -924,6 +942,29 @@ mod tests {
source_code.split('\n').map(|s| s.to_string()).collect();
let last_line = lines.pop().unwrap();
assert!(last_line.starts_with("//# sourceMappingURL=file:https://"));

// Get source map file and assert it has proper URLs
let source_map = mock_state
.ts_compiler
.get_source_map_file(&specifier)
.expect("Source map not found");
let source_str = String::from_utf8(source_map.source_code).unwrap();
let source_json: Value = serde_json::from_str(&source_str).unwrap();

let js_key = mock_state
.ts_compiler
.disk_cache
.get_cache_filename_with_extension(specifier.as_url(), "js");
let js_path = mock_state.ts_compiler.disk_cache.location.join(js_key);
let js_file_url = Url::from_file_path(js_path).unwrap();

let file_str = source_json.get("file").unwrap().as_str().unwrap();
assert_eq!(file_str, js_file_url.to_string());

let sources = source_json.get("sources").unwrap().as_array().unwrap();
assert_eq!(sources.len(), 1);
let source = sources.get(0).unwrap().as_str().unwrap();
assert_eq!(source, specifier.to_string());
}

#[tokio::test]
Expand Down

0 comments on commit 053c568

Please sign in to comment.