summaryrefslogtreecommitdiff
path: root/cli/cache
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2024-06-05 11:04:16 -0400
committerGitHub <noreply@github.com>2024-06-05 17:04:16 +0200
commit7ed90a20d04982ae15a52ae2378cbffd4b6839df (patch)
tree3297d6f7227fbf1cf80e17a2a376ef4dfa52e6ad /cli/cache
parent0544d60012006b1c7799d8b6eafacec9567901ad (diff)
fix: better handling of npm resolution occurring on workers (#24094)
Closes https://github.com/denoland/deno/issues/24063
Diffstat (limited to 'cli/cache')
-rw-r--r--cli/cache/emit.rs40
-rw-r--r--cli/cache/parsed_source.rs2
2 files changed, 26 insertions, 16 deletions
diff --git a/cli/cache/emit.rs b/cli/cache/emit.rs
index ac7c09a9a..1d903fbba 100644
--- a/cli/cache/emit.rs
+++ b/cli/cache/emit.rs
@@ -45,7 +45,7 @@ impl EmitCache {
&self,
specifier: &ModuleSpecifier,
expected_source_hash: u64,
- ) -> Option<String> {
+ ) -> Option<Vec<u8>> {
let meta_filename = self.get_meta_filename(specifier)?;
let emit_filename = self.get_emit_filename(specifier)?;
@@ -63,8 +63,7 @@ impl EmitCache {
}
// everything looks good, return it
- let emit_text = String::from_utf8(emit_bytes).ok()?;
- Some(emit_text)
+ Some(emit_bytes)
}
/// Gets the filepath which stores the emit.
@@ -85,7 +84,7 @@ impl EmitCache {
&self,
specifier: &ModuleSpecifier,
source_hash: u64,
- code: &str,
+ code: &[u8],
) {
if let Err(err) = self.set_emit_code_result(specifier, source_hash, code) {
// should never error here, but if it ever does don't fail
@@ -101,7 +100,7 @@ impl EmitCache {
&self,
specifier: &ModuleSpecifier,
source_hash: u64,
- code: &str,
+ code: &[u8],
) -> Result<(), AnyError> {
let meta_filename = self
.get_meta_filename(specifier)
@@ -113,14 +112,14 @@ impl EmitCache {
// save the metadata
let metadata = EmitMetadata {
source_hash,
- emit_hash: compute_emit_hash(code.as_bytes(), self.cli_version),
+ emit_hash: compute_emit_hash(code, self.cli_version),
};
self
.disk_cache
.set(&meta_filename, &serde_json::to_vec(&metadata)?)?;
// save the emit source
- self.disk_cache.set(&emit_filename, code.as_bytes())?;
+ self.disk_cache.set(&emit_filename, code)?;
Ok(())
}
@@ -163,6 +162,8 @@ mod test {
disk_cache: disk_cache.clone(),
cli_version: "1.0.0",
};
+ let to_string =
+ |bytes: Vec<u8>| -> String { String::from_utf8(bytes).unwrap() };
let specifier1 =
ModuleSpecifier::from_file_path(temp_dir.path().join("file1.ts"))
@@ -173,16 +174,19 @@ mod test {
assert_eq!(cache.get_emit_code(&specifier1, 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);
+ cache.set_emit_code(&specifier1, 10, emit_code1.as_bytes());
+ cache.set_emit_code(&specifier2, 2, emit_code2.as_bytes());
// providing the incorrect source hash
assert_eq!(cache.get_emit_code(&specifier1, 5), None);
// providing the correct source hash
assert_eq!(
- cache.get_emit_code(&specifier1, 10),
+ cache.get_emit_code(&specifier1, 10).map(to_string),
Some(emit_code1.clone()),
);
- assert_eq!(cache.get_emit_code(&specifier2, 2), Some(emit_code2));
+ assert_eq!(
+ cache.get_emit_code(&specifier2, 2).map(to_string),
+ Some(emit_code2)
+ );
// try changing the cli version (should not load previous ones)
let cache = EmitCache {
@@ -190,19 +194,25 @@ mod test {
cli_version: "2.0.0",
};
assert_eq!(cache.get_emit_code(&specifier1, 10), None);
- cache.set_emit_code(&specifier1, 5, &emit_code1);
+ cache.set_emit_code(&specifier1, 5, emit_code1.as_bytes());
// recreating the cache should still load the data because the CLI version is the same
let cache = EmitCache {
disk_cache,
cli_version: "2.0.0",
};
- assert_eq!(cache.get_emit_code(&specifier1, 5), Some(emit_code1));
+ assert_eq!(
+ cache.get_emit_code(&specifier1, 5).map(to_string),
+ 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);
+ cache.set_emit_code(&specifier1, 20, emit_code3.as_bytes());
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, 20).map(to_string),
+ Some(emit_code3)
+ );
}
}
diff --git a/cli/cache/parsed_source.rs b/cli/cache/parsed_source.rs
index 688f2b9fb..e956361f4 100644
--- a/cli/cache/parsed_source.rs
+++ b/cli/cache/parsed_source.rs
@@ -75,7 +75,7 @@ impl ParsedSourceCache {
) -> Result<ParsedSource, deno_ast::ParseDiagnostic> {
if let Some(parsed_source) = self.remove_parsed_source(specifier) {
if parsed_source.media_type() == media_type
- && parsed_source.text_info().text_str() == source.as_ref()
+ && parsed_source.text().as_ref() == source.as_ref()
{
// note: message used tests
log::debug!("Removed parsed source: {}", specifier);