Skip to content

Commit 613804f

Browse files
committed
fix(go-runner): copy whole git repository to avoid missing files or packages
1 parent 87aded0 commit 613804f

2 files changed

Lines changed: 43 additions & 13 deletions

File tree

go-runner/src/builder/templater.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,17 @@ pub fn run<P: AsRef<Path>>(
2525
package: &BenchmarkPackage,
2626
profile_dir: P,
2727
) -> anyhow::Result<(TempDir, PathBuf)> {
28-
// 1. Copy the whole module to a build directory
28+
// 1. Copy the whole git repository to a build directory
2929
let target_dir = TempDir::new()?;
3030
std::fs::create_dir_all(&target_dir).context("Failed to create target directory")?;
31-
utils::copy_dir_recursively(&package.module.dir, &target_dir)?;
31+
32+
let git_root = if let Ok(git_dir) = utils::get_parent_git_repo_path(&package.module.dir) {
33+
git_dir
34+
} else {
35+
warn!("Could not find git repository root. Falling back to module directory as root");
36+
PathBuf::from(&package.module.dir)
37+
};
38+
utils::copy_dir_recursively(&git_root, &target_dir)?;
3239

3340
// Create a new go-runner.metadata file in the root of the project
3441
//
@@ -58,12 +65,11 @@ pub fn run<P: AsRef<Path>>(
5865
.test_files()
5966
.with_context(|| anyhow::anyhow!("No test files found for package: {}", package.name))?;
6067

61-
// Calculate the relative path from module root to package directory
68+
// Calculate the relative path from git root to package directory
6269
let package_dir = Path::new(&package.dir);
63-
let module_dir = Path::new(&package.module.dir);
64-
let relative_package_path = package_dir.strip_prefix(module_dir).context(format!(
65-
"Package dir {:?} is not within module dir {:?}",
66-
package.dir, package.module.dir
70+
let relative_package_path = package_dir.strip_prefix(&git_root).context(format!(
71+
"Package dir {:?} is not within git root {:?}",
72+
package.dir, git_root
6773
))?;
6874
debug!("Relative package path: {relative_package_path:?}");
6975

@@ -87,8 +93,16 @@ pub fn run<P: AsRef<Path>>(
8793
}
8894
patcher::patch_imports(&target_dir)?;
8995

90-
// 3. Install codspeed-go dependency at the module level (once for the whole module)
91-
patcher::install_codspeed_dependency(&target_dir)?;
96+
// 3. Install codspeed-go dependency at the package module level
97+
// Find the module directory by getting the relative path from git root
98+
let module_dir = Path::new(&package.module.dir)
99+
.strip_prefix(&git_root)
100+
.map(|relative_module_path| target_dir.path().join(relative_module_path))
101+
.unwrap_or_else(|_| {
102+
// Fall back to target_dir if we can't calculate relative path
103+
target_dir.path().to_path_buf()
104+
});
105+
patcher::install_codspeed_dependency(&module_dir)?;
92106

93107
// 3. Handle test files differently based on whether they're external or internal tests
94108
let codspeed_dir = target_dir

go-runner/src/utils.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,36 @@ pub fn copy_dir_recursively(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io:
88
for entry in fs::read_dir(src)? {
99
let entry = entry?;
1010
let ty = entry.file_type()?;
11+
let path = entry.path();
12+
1113
if ty.is_dir() {
12-
if entry.file_name() == ".git" {
14+
if entry.file_name() == ".git" || entry.file_name() == "target" {
1315
continue;
1416
}
1517

16-
copy_dir_recursively(entry.path(), dst.as_ref().join(entry.file_name()))?;
18+
copy_dir_recursively(&path, dst.as_ref().join(entry.file_name()))?;
19+
} else if ty.is_symlink() {
20+
// Follow symlinks to directories, copy other symlinks as-is
21+
if let Ok(metadata) = fs::metadata(&path) {
22+
if metadata.is_dir() {
23+
// Symlink points to a directory, follow it recursively
24+
copy_dir_recursively(&path, dst.as_ref().join(entry.file_name()))?;
25+
} else {
26+
// Symlink points to a file, copy it
27+
fs::copy(&path, dst.as_ref().join(entry.file_name()))?;
28+
}
29+
} else {
30+
// If metadata fails (broken symlink), copy the symlink as-is
31+
fs::copy(&path, dst.as_ref().join(entry.file_name()))?;
32+
}
1733
} else {
18-
fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
34+
fs::copy(&path, dst.as_ref().join(entry.file_name()))?;
1935
}
2036
}
2137
Ok(())
2238
}
2339

24-
fn get_parent_git_repo_path(abs_path: &Path) -> io::Result<PathBuf> {
40+
pub fn get_parent_git_repo_path(abs_path: &Path) -> io::Result<PathBuf> {
2541
if abs_path.join(".git").exists() {
2642
Ok(abs_path.to_path_buf())
2743
} else {

0 commit comments

Comments
 (0)