Skip to content

Commit d4247a4

Browse files
authored
fix: resolve a relative --database against the working directory (#537)
<!-- claude-projects-attribution: {"github_login":"shenxianpeng"} --> As suggested in #535 (comment): a relative `--database` path is now resolved against the current working directory, like cpp-linter v1 (1.15+), instead of against `--repo-root`. Results only differ when `--repo-root` is not `.`. - `ClangParams::from(&Cli)` uses `std::path::absolute()` (no filesystem access, so a missing path still reaches the existing "Failed to read compilation database" handling). - New test `cli::structs::test::relative_db_path` (`--repo-root sub --database build` gives `<cwd>/build`). - `tests/comments.rs` and `tests/reviews.rs` passed `-p=build` together with `--repo-root=<tmp dir>`. With this change that path pointed at `cpp-linter/build`, so the tests still passed but without the meson database (each run logged "Failed to read compilation database file"). They now pass the absolute `<tmp dir>/build`. Checked locally (clang 21): `cargo test --all-features --lib db_path`, `--test comments` (17 passed, single-threaded) and `--test reviews` (16 passed), no "Failed to read compilation database" warning; `cargo clippy --all-features --all-targets` clean, `cargo fmt`. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Relative compilation database paths are now resolved from the current working directory, rather than the repository root. This makes path handling consistent when the configured repository root differs from the directory where the command is run. * If the path cannot be resolved, the supplied path is preserved. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Xianpeng Shen <3353385+shenxianpeng@users.noreply.github.com>
1 parent 6c6c2ee commit d4247a4

3 files changed

Lines changed: 18 additions & 12 deletions

File tree

‎cpp-linter/src/cli/structs.rs‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,19 +272,13 @@ impl From<&Cli> for ClangParams {
272272
let ignore_format: Vec<&str> = ignore_format.iter().map(|s| s.as_str()).collect();
273273
FileFilter::new(&ignore_format, &extensions, Some("clang-format"))
274274
});
275-
let repo_root = args.source_options.repo_root.clone();
275+
// A relative database path is relative to the working directory (like v1),
276+
// not to the repo root.
276277
let database = args
277278
.tidy_options
278279
.database
279280
.as_ref()
280-
.map(PathBuf::from)
281-
.map(|db| {
282-
if db.is_relative() {
283-
repo_root.join(db)
284-
} else {
285-
db
286-
}
287-
});
281+
.map(|db| std::path::absolute(db).unwrap_or_else(|_| db.to_path_buf()));
288282
ClangParams {
289283
tidy_checks: args.tidy_options.tidy_checks.clone(),
290284
lines_changed_only: args.source_options.lines_changed_only.clone(),
@@ -296,7 +290,7 @@ impl From<&Cli> for ClangParams {
296290
clang_format_command: None,
297291
tidy_filter,
298292
format_filter,
299-
repo_root,
293+
repo_root: args.source_options.repo_root.clone(),
300294
gen_patch: args.feedback_options.pr_review
301295
|| args.feedback_options.gen_patch
302296
|| args.feedback_options.gen_patch_path.is_some(),
@@ -510,4 +504,14 @@ mod test {
510504
let clang_params = ClangParams::from(&cli);
511505
assert_eq!(clang_params.database, Some(tmp_dir.path().to_path_buf()));
512506
}
507+
508+
#[test]
509+
fn relative_db_path() {
510+
let cli = Cli::parse_from(["cpp-linter", "--repo-root", "sub", "--database", "build"]);
511+
let clang_params = ClangParams::from(&cli);
512+
assert_eq!(
513+
clang_params.database,
514+
Some(std::env::current_dir().unwrap().join("build"))
515+
);
516+
}
513517
}

‎cpp-linter/tests/comments.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ async fn setup(lib_root: &Path, tmp_dir: &TempDir, test_params: &TestParams) {
274274
"--ignore-format=src/some source.c".to_string(),
275275
format!("--thread-comments={}", test_params.thread_comments),
276276
format!("--no-lgtm={}", test_params.no_lgtm),
277-
"-p=build".to_string(),
277+
// a relative database path is resolved against the working directory
278+
format!("-p={}", tmp_dir.path().join("build").to_str().unwrap()),
278279
"-i=build/**".to_string(),
279280
format!("--repo-root={}", tmp_dir.path().to_str().unwrap()),
280281
format!(

‎cpp-linter/tests/reviews.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ async fn setup(lib_root: &Path, tmp_dir: &TempDir, test_params: &TestParams) ->
272272
format!("--pr-review={}", test_params.pr_review),
273273
format!("--passive-reviews={}", test_params.passive_reviews),
274274
format!("--no-lgtm={}", test_params.no_lgtm),
275-
"-p=build".to_string(),
275+
// a relative database path is resolved against the working directory
276+
format!("-p={}", tmp_dir.path().join("build").to_str().unwrap()),
276277
"-i=build/**".to_string(),
277278
format!("--repo-root={}", tmp_dir.path().to_str().unwrap()),
278279
];

0 commit comments

Comments
 (0)