From 5f5bbd597ad3454469b3e51a80cd7cb9be39c04d Mon Sep 17 00:00:00 2001 From: David Sherret Date: Wed, 20 Jul 2022 16:30:57 -0400 Subject: [PATCH] fix(coverage): do not verify emit source hash for coverage (#15260) --- cli/cache/emit.rs | 32 ++++++++++++++++++++------------ cli/emit.rs | 2 +- cli/tools/coverage/mod.rs | 5 +---- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/cli/cache/emit.rs b/cli/cache/emit.rs index 61039a966c..5260a7fb34 100644 --- a/cli/cache/emit.rs +++ b/cli/cache/emit.rs @@ -46,7 +46,7 @@ impl EmitCache { pub fn get_emit_code( &self, specifier: &ModuleSpecifier, - expected_source_hash: u64, + expected_source_hash: Option, ) -> Option { let meta_filename = self.get_meta_filename(specifier)?; let emit_filename = self.get_emit_filename(specifier)?; @@ -54,11 +54,14 @@ impl EmitCache { // load and verify the meta data file is for this source and CLI version let bytes = self.disk_cache.get(&meta_filename).ok()?; let meta: EmitMetadata = serde_json::from_slice(&bytes).ok()?; - if meta.source_hash != expected_source_hash.to_string() - || meta.cli_version != self.cli_version - { + if meta.cli_version != self.cli_version { return None; } + if let Some(expected_source_hash) = expected_source_hash { + if meta.source_hash != expected_source_hash.to_string() { + return None; + } + } // load and verify the emit is for the meta data let emit_bytes = self.disk_cache.get(&emit_filename).ok()?; @@ -171,26 +174,31 @@ mod test { let specifier2 = ModuleSpecifier::from_file_path(temp_dir.path().join("file2.ts")) .unwrap(); - assert_eq!(cache.get_emit_code(&specifier1, 1), None); + assert_eq!(cache.get_emit_code(&specifier1, Some(1)), None); let emit_code1 = "text1".to_string(); let emit_code2 = "text2".to_string(); cache.set_emit_code(&specifier1, 10, &emit_code1); cache.set_emit_code(&specifier2, 2, &emit_code2); // providing the incorrect source hash - assert_eq!(cache.get_emit_code(&specifier1, 5), None); + assert_eq!(cache.get_emit_code(&specifier1, Some(5)), None); // providing the correct source hash assert_eq!( - cache.get_emit_code(&specifier1, 10), + cache.get_emit_code(&specifier1, Some(10)), + Some(emit_code1.clone()), + ); + assert_eq!(cache.get_emit_code(&specifier2, Some(2)), Some(emit_code2)); + // providing no hash + assert_eq!( + cache.get_emit_code(&specifier1, None), Some(emit_code1.clone()), ); - assert_eq!(cache.get_emit_code(&specifier2, 2), Some(emit_code2),); // try changing the cli version (should not load previous ones) let cache = EmitCache { disk_cache: disk_cache.clone(), cli_version: "2.0.0".to_string(), }; - assert_eq!(cache.get_emit_code(&specifier1, 10), None); + assert_eq!(cache.get_emit_code(&specifier1, Some(10)), None); cache.set_emit_code(&specifier1, 5, &emit_code1); // recreating the cache should still load the data because the CLI version is the same @@ -198,12 +206,12 @@ mod test { disk_cache, cli_version: "2.0.0".to_string(), }; - assert_eq!(cache.get_emit_code(&specifier1, 5), Some(emit_code1)); + assert_eq!(cache.get_emit_code(&specifier1, Some(5)), Some(emit_code1)); // adding when already exists should not cause issue let emit_code3 = "asdf".to_string(); cache.set_emit_code(&specifier1, 20, &emit_code3); - assert_eq!(cache.get_emit_code(&specifier1, 5), None); - assert_eq!(cache.get_emit_code(&specifier1, 20), Some(emit_code3)); + assert_eq!(cache.get_emit_code(&specifier1, Some(5)), None); + assert_eq!(cache.get_emit_code(&specifier1, Some(20)), Some(emit_code3)); } } diff --git a/cli/emit.rs b/cli/emit.rs index 1d8ecca433..f96b8484b0 100644 --- a/cli/emit.rs +++ b/cli/emit.rs @@ -249,7 +249,7 @@ pub fn emit_parsed_source( let source_hash = get_source_hash(parsed_source.text_info().text_str(), emit_config_hash); - if let Some(emit_code) = cache.get_emit_code(specifier, source_hash) { + if let Some(emit_code) = cache.get_emit_code(specifier, Some(source_hash)) { Ok(emit_code) } else { let transpiled_source = parsed_source.transpile(emit_options)?; diff --git a/cli/tools/coverage/mod.rs b/cli/tools/coverage/mod.rs index 1ff7f2bc2b..b76792b1e6 100644 --- a/cli/tools/coverage/mod.rs +++ b/cli/tools/coverage/mod.rs @@ -3,7 +3,6 @@ use crate::args::CoverageFlags; use crate::args::Flags; use crate::colors; -use crate::emit::get_source_hash; use crate::fs_util::collect_files; use crate::proc_state::ProcState; use crate::text_encoding::source_map_from_code; @@ -678,9 +677,7 @@ pub async fn cover_files( | MediaType::Mts | MediaType::Cts | MediaType::Tsx => { - let source_hash = - get_source_hash(original_source, ps.emit_options_hash); - match ps.emit_cache.get_emit_code(&file.specifier, source_hash) { + match ps.emit_cache.get_emit_code(&file.specifier, None) { Some(code) => code, None => { return Err(anyhow!(