Skip to content

Commit 631baed

Browse files
feat: Attempt to detect invocation outside project root (#408)
Emit a warning if we detect this, as it may be a user error
1 parent a3f9908 commit 631baed

File tree

5 files changed

+30
-4
lines changed

5 files changed

+30
-4
lines changed

indexer/CompilationDatabase.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,28 @@ compdb::File compdb::File::open(const StdPath &path,
535535
if (fileSizeError) {
536536
return compdbFile;
537537
}
538+
if (validationOptions.tryDetectOutOfProjectRoot) {
539+
auto dirPath = path.lexically_normal();
540+
while (dirPath.has_parent_path() && dirPath.parent_path() != dirPath) {
541+
dirPath = dirPath.parent_path();
542+
auto maybeGitDirPath = dirPath / ".git";
543+
std::error_code error;
544+
auto status = std::filesystem::status(maybeGitDirPath, error);
545+
if (!error && status.type() == std::filesystem::file_type::directory) {
546+
auto cwd = std::filesystem::current_path();
547+
if (cwd != dirPath) {
548+
spdlog::warn(
549+
"found .git directory in {} but current working directory is {};"
550+
" did you invoke scip-clang from the project root?",
551+
dirPath.string(), cwd.string());
552+
spdlog::info(
553+
"invoking scip-clang from a directory other than the project root"
554+
" may lead to incorrect indexing results");
555+
break;
556+
}
557+
}
558+
}
559+
}
538560
compdbFile._sizeInBytes = size;
539561
compdbFile._commandCount = validateAndCountJobs(
540562
compdbFile._sizeInBytes, compdbFile.file, validationOptions);

indexer/CompilationDatabase.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace compdb {
2929

3030
struct ValidationOptions {
3131
bool checkDirectoryPathsAreAbsolute;
32+
bool tryDetectOutOfProjectRoot;
3233
};
3334

3435
class File {

indexer/Driver.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,8 +1227,9 @@ class Driver {
12271227
StdPath compdbStdPath{this->compdbPath().asStringRef()};
12281228
auto compdbFile = compdb::File::openAndExitOnErrors(
12291229
compdbStdPath,
1230-
compdb::ValidationOptions{.checkDirectoryPathsAreAbsolute =
1231-
!this->options.isTesting});
1230+
compdb::ValidationOptions{
1231+
.checkDirectoryPathsAreAbsolute = !this->options.isTesting,
1232+
.tryDetectOutOfProjectRoot = !this->options.isTesting});
12321233
this->compdbCommandCount = compdbFile.commandCount();
12331234
this->options.numWorkers =
12341235
std::min(this->compdbCommandCount, this->numWorkers());

indexer/Worker.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ Worker::Worker(WorkerOptions &&options)
144144
case WorkerMode::Compdb: {
145145
auto compdbFile = compdb::File::openAndExitOnErrors(
146146
this->options.compdbPath,
147-
compdb::ValidationOptions{.checkDirectoryPathsAreAbsolute = true});
147+
compdb::ValidationOptions{.checkDirectoryPathsAreAbsolute = true,
148+
.tryDetectOutOfProjectRoot = true});
148149
compdb::ResumableParser parser{};
149150
parser.initialize(compdbFile,
150151
compdb::ParseOptions::create(

test/test_main.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ TEST_CASE("COMPDB_PARSING") {
175175

176176
auto compdbFile = compdb::File::openAndExitOnErrors(
177177
jsonFilePath,
178-
compdb::ValidationOptions{.checkDirectoryPathsAreAbsolute = false});
178+
compdb::ValidationOptions{.checkDirectoryPathsAreAbsolute = false,
179+
.tryDetectOutOfProjectRoot = false});
179180
if (!compdbFile.file) {
180181
spdlog::error("missing JSON file at path {}", jsonFilePath.c_str());
181182
REQUIRE(compdbFile.file);

0 commit comments

Comments
 (0)