Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ops::package leaving old path in created FileLockGuard #792

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion scarb/src/flock.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::fs::{File, OpenOptions};
use std::ops::{Deref, DerefMut};
use std::path::Path;
use std::sync::{Arc, Weak};
use std::{fmt, io};

use anyhow::{Context, Result};
use anyhow::{ensure, Context, Result};
use camino::{Utf8Path, Utf8PathBuf};
use fs4::{lock_contended_error, FileExt};
use tokio::sync::Mutex;
Expand All @@ -12,6 +13,7 @@ use scarb_ui::components::Status;

use crate::core::Config;
use crate::internal::fsx;
use crate::internal::fsx::PathUtf8Ext;
use crate::internal::lazy_directory_creator::LazyDirectoryCreator;

const OK_FILE: &str = ".scarb-ok";
Expand All @@ -37,6 +39,18 @@ impl FileLockGuard {
pub fn lock_kind(&self) -> FileLockKind {
self.lock_kind
}

pub fn rename(&mut self, to: impl AsRef<Path>) -> Result<&mut Self> {
ensure!(
self.lock_kind == FileLockKind::Exclusive,
"cannot rename shared file: {}",
self.path,
);
let to = to.as_ref().try_to_utf8()?;
fsx::rename(&self.path, &to)?;
self.path = to;
Ok(self)
}
}

impl Deref for FileLockGuard {
Expand Down
4 changes: 2 additions & 2 deletions scarb/src/ops/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::core::publishing::manifest_normalization::prepare_manifest_for_publis
use crate::core::publishing::source::list_source_files;
use crate::core::{Package, PackageId, PackageName, Workspace};
use crate::flock::FileLockGuard;
use crate::internal::{fsx, restricted_names};
use crate::internal::restricted_names;
use crate::{ops, MANIFEST_FILE_NAME};

const VERSION: u8 = 1;
Expand Down Expand Up @@ -117,7 +117,7 @@ fn package_one_impl(

dst.seek(SeekFrom::Start(0))?;

fsx::rename(dst.path(), dst.path().with_file_name(filename))?;
dst.rename(dst.path().with_file_name(filename))?;

let dst_metadata = dst
.metadata()
Expand Down