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(core): fix caching outputs which have symlinks #22548

Merged
merged 1 commit into from
Mar 28, 2024
Merged
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
90 changes: 80 additions & 10 deletions packages/nx/src/native/cache/file_ops.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fs;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::{fs, io};

use fs_extra::error::ErrorKind;

Expand All @@ -13,25 +13,55 @@ pub fn remove(src: String) -> anyhow::Result<()> {

#[napi]
pub fn copy(src: String, dest: String) -> anyhow::Result<()> {
let copy_options = fs_extra::dir::CopyOptions::new()
.overwrite(true)
.skip_exist(false);

let dest: PathBuf = dest.into();
let dest_parent = dest.parent().unwrap_or(&dest);

let src: PathBuf = src.into();

if !dest_parent.exists() {
fs::create_dir_all(dest_parent)?;
}

fs_extra::copy_items(&[src], dest_parent, &copy_options).map_err(|err| match err.kind {
ErrorKind::Io(err_kind) => anyhow::Error::new(err_kind),
_ => anyhow::Error::new(err),
})?;
if src.is_dir() {
copy_dir_all(&src, dest).map_err(anyhow::Error::new)?;
} else if src.is_symlink() {
symlink(fs::read_link(src)?, dest)?;
} else {
fs::copy(src, dest)?;
}

Ok(())
}

#[cfg(windows)]
fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
std::os::windows::fs::symlink_file(original, link)
}

#[cfg(unix)]
fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
std::os::unix::fs::symlink(original, link)
}

fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
fs::create_dir_all(&dst)?;
for entry in fs::read_dir(src)? {
let entry = entry?;
let ty = entry.file_type()?;
if ty.is_dir() {
copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;
} else if ty.is_symlink() {
symlink(
fs::read_link(entry.path())?,
dst.as_ref().join(entry.file_name()),
)?;
} else {
fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
}
}
Ok(())
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -71,4 +101,44 @@ mod test {

assert!(temp.child("new-parent/file.txt").exists());
}

#[test]
fn should_copy_symlinks() {
let temp = TempDir::new().unwrap();
let target = temp.child("parent").child("target.txt");
target.touch().unwrap();
let link = temp.child("parent").child("file.txt");

link.symlink_to_file(&target).unwrap();

let src = temp.join("parent/file.txt");
let dest = temp.join("new-parent/file.txt");
copy(src.to_string_lossy().into(), dest.to_string_lossy().into()).unwrap();

assert!(temp.child("new-parent/file.txt").exists());
assert_eq!(
temp.child("new-parent/file.txt").read_link().unwrap(),
target.path()
);
}

#[test]
fn should_copy_directories_with_symlinks() {
let temp = TempDir::new().unwrap();
let target = temp.child("parent").child("target.txt");
target.touch().unwrap();
let link = temp.child("parent").child("file.txt");

link.symlink_to_file(&target).unwrap();

let src = temp.join("parent");
let dest = temp.join("new-parent");
copy(src.to_string_lossy().into(), dest.to_string_lossy().into()).unwrap();

assert!(temp.child("new-parent/file.txt").exists());
assert_eq!(
temp.child("new-parent/file.txt").read_link().unwrap(),
target.path()
);
}
}