find_package: Save package information only after successfully loading it

If a package configuration file sets `<PackageName>_FOUND` to false,
the package is considered not found.  Do not save its package info.

Note that this exposes an existing pointer invalidation on nested
`find_package` calls, which will be fixed in following commits.

Issue: #27730
This commit is contained in:
Brad King
2026-04-01 11:12:37 -04:00
parent 3a7d797d5b
commit 9387988626

View File

@@ -1668,12 +1668,6 @@ bool cmFindPackageCommand::HandlePackageMode(
"fileFound is true but FileFound is empty!");
fileFound = false;
}
if (fileFound) {
this->CurrentPackageInfo->Directory =
cmSystemTools::GetFilenamePath(this->FileFound);
this->CurrentPackageInfo->Version = this->VersionFound;
}
}
std::string const foundVar = cmStrCat(this->Name, "_FOUND");
@@ -1735,6 +1729,12 @@ bool cmFindPackageCommand::HandlePackageMode(
// The configuration file is invalid.
result = false;
}
if (this->UseConfigFiles && found) {
this->CurrentPackageInfo->Directory =
cmSystemTools::GetFilenamePath(this->FileFound);
this->CurrentPackageInfo->Version = this->VersionFound;
}
}
if (this->UseFindModules && !found &&