Skip to content

Commit

Permalink
feat: Attempt to detect invocation outside project root (#408)
Browse files Browse the repository at this point in the history
Emit a warning if we detect this, as it may be a user error
  • Loading branch information
varungandhi-src authored Jul 27, 2023
1 parent a3f9908 commit 631baed
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
22 changes: 22 additions & 0 deletions indexer/CompilationDatabase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,28 @@ compdb::File compdb::File::open(const StdPath &path,
if (fileSizeError) {
return compdbFile;
}
if (validationOptions.tryDetectOutOfProjectRoot) {
auto dirPath = path.lexically_normal();
while (dirPath.has_parent_path() && dirPath.parent_path() != dirPath) {
dirPath = dirPath.parent_path();
auto maybeGitDirPath = dirPath / ".git";
std::error_code error;
auto status = std::filesystem::status(maybeGitDirPath, error);
if (!error && status.type() == std::filesystem::file_type::directory) {
auto cwd = std::filesystem::current_path();
if (cwd != dirPath) {
spdlog::warn(
"found .git directory in {} but current working directory is {};"
" did you invoke scip-clang from the project root?",
dirPath.string(), cwd.string());
spdlog::info(
"invoking scip-clang from a directory other than the project root"
" may lead to incorrect indexing results");
break;
}
}
}
}
compdbFile._sizeInBytes = size;
compdbFile._commandCount = validateAndCountJobs(
compdbFile._sizeInBytes, compdbFile.file, validationOptions);
Expand Down
1 change: 1 addition & 0 deletions indexer/CompilationDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace compdb {

struct ValidationOptions {
bool checkDirectoryPathsAreAbsolute;
bool tryDetectOutOfProjectRoot;
};

class File {
Expand Down
5 changes: 3 additions & 2 deletions indexer/Driver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1227,8 +1227,9 @@ class Driver {
StdPath compdbStdPath{this->compdbPath().asStringRef()};
auto compdbFile = compdb::File::openAndExitOnErrors(
compdbStdPath,
compdb::ValidationOptions{.checkDirectoryPathsAreAbsolute =
!this->options.isTesting});
compdb::ValidationOptions{
.checkDirectoryPathsAreAbsolute = !this->options.isTesting,
.tryDetectOutOfProjectRoot = !this->options.isTesting});
this->compdbCommandCount = compdbFile.commandCount();
this->options.numWorkers =
std::min(this->compdbCommandCount, this->numWorkers());
Expand Down
3 changes: 2 additions & 1 deletion indexer/Worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ Worker::Worker(WorkerOptions &&options)
case WorkerMode::Compdb: {
auto compdbFile = compdb::File::openAndExitOnErrors(
this->options.compdbPath,
compdb::ValidationOptions{.checkDirectoryPathsAreAbsolute = true});
compdb::ValidationOptions{.checkDirectoryPathsAreAbsolute = true,
.tryDetectOutOfProjectRoot = true});
compdb::ResumableParser parser{};
parser.initialize(compdbFile,
compdb::ParseOptions::create(
Expand Down
3 changes: 2 additions & 1 deletion test/test_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ TEST_CASE("COMPDB_PARSING") {

auto compdbFile = compdb::File::openAndExitOnErrors(
jsonFilePath,
compdb::ValidationOptions{.checkDirectoryPathsAreAbsolute = false});
compdb::ValidationOptions{.checkDirectoryPathsAreAbsolute = false,
.tryDetectOutOfProjectRoot = false});
if (!compdbFile.file) {
spdlog::error("missing JSON file at path {}", jsonFilePath.c_str());
REQUIRE(compdbFile.file);
Expand Down

0 comments on commit 631baed

Please sign in to comment.