summaryrefslogtreecommitdiff
path: root/cli/args/lockfile.rs
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2024-05-28 14:58:43 -0400
committerGitHub <noreply@github.com>2024-05-28 14:58:43 -0400
commit448fe67b7a2142f62332b651f9d215534dceb1f5 (patch)
tree3cfc763f39bf275a537e6228767b3e43866f5d0f /cli/args/lockfile.rs
parentcd8f5f53f7616e4c74de0f1ff5eadd6ef024118a (diff)
feat(vendor): support modifying remote files in vendor folder without checksum errors (#23979)
Includes: * https://github.com/denoland/deno_graph/pull/486 * https://github.com/denoland/deno_graph/pull/488 * https://github.com/denoland/deno_lockfile/pull/25 * https://github.com/denoland/deno_lockfile/pull/22 * https://github.com/denoland/deno_graph/pull/483 * https://github.com/denoland/deno_graph/pull/470
Diffstat (limited to 'cli/args/lockfile.rs')
-rw-r--r--cli/args/lockfile.rs34
1 files changed, 32 insertions, 2 deletions
diff --git a/cli/args/lockfile.rs b/cli/args/lockfile.rs
index d5ab14432..79d139487 100644
--- a/cli/args/lockfile.rs
+++ b/cli/args/lockfile.rs
@@ -2,10 +2,13 @@
use std::path::PathBuf;
+use deno_core::anyhow::Context;
use deno_core::error::AnyError;
use deno_runtime::deno_node::PackageJson;
use crate::args::ConfigFile;
+use crate::cache;
+use crate::util::fs::atomic_write_file;
use crate::Flags;
use super::DenoSubcommand;
@@ -13,7 +16,6 @@ use super::InstallFlags;
use super::InstallKind;
pub use deno_lockfile::Lockfile;
-pub use deno_lockfile::LockfileError;
pub fn discover(
flags: &Flags,
@@ -54,6 +56,34 @@ pub fn discover(
},
};
- let lockfile = Lockfile::new(filename, flags.lock_write)?;
+ let lockfile = if flags.lock_write {
+ Lockfile::new_empty(filename, true)
+ } else {
+ read_lockfile_at_path(filename)?
+ };
Ok(Some(lockfile))
}
+
+pub fn read_lockfile_at_path(filename: PathBuf) -> Result<Lockfile, AnyError> {
+ match std::fs::read_to_string(&filename) {
+ Ok(text) => Ok(Lockfile::with_lockfile_content(filename, &text, false)?),
+ Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
+ Ok(Lockfile::new_empty(filename, false))
+ }
+ Err(err) => Err(err).with_context(|| {
+ format!("Failed reading lockfile '{}'", filename.display())
+ }),
+ }
+}
+
+pub fn write_lockfile_if_has_changes(
+ lockfile: &Lockfile,
+) -> Result<(), AnyError> {
+ let Some(bytes) = lockfile.resolve_write_bytes() else {
+ return Ok(()); // nothing to do
+ };
+ // do an atomic write to reduce the chance of multiple deno
+ // processes corrupting the file
+ atomic_write_file(&lockfile.filename, bytes, cache::CACHE_PERM)
+ .context("Failed writing lockfile.")
+}