1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 12:55:26 +01:00

Merge pull request #8789 from weirdan/defer-cache-directory-creation

Fixes https://github.com/vimeo/psalm/issues/4267
This commit is contained in:
Bruce Weirdan 2022-11-30 10:52:17 -04:00 committed by GitHub
commit 870f5817d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 27 deletions

View File

@ -27,6 +27,9 @@
- [BC] TPositiveInt has been removed and replaced by TIntRange - [BC] TPositiveInt has been removed and replaced by TIntRange
- [BC] Property `Psalm\Config::$cache_directory` is now internal. Use
`Psalm\Config::getCacheDirectory()` instead.
- [BC] The parameter `$php_version` of `Psalm\Type\Atomic::create()` renamed - [BC] The parameter `$php_version` of `Psalm\Type\Atomic::create()` renamed
to `$analysis_php_version_id` and changed from `array|null` to `int|null`. to `$analysis_php_version_id` and changed from `array|null` to `int|null`.
Previously it accepted PHP version as `array{major_version, minor_version}` Previously it accepted PHP version as `array{major_version, minor_version}`

View File

@ -204,10 +204,14 @@ class Config
/** /**
* The directory to store PHP Parser (and other) caches * The directory to store PHP Parser (and other) caches
* *
* @internal
*
* @var string|null * @var string|null
*/ */
public $cache_directory; public $cache_directory;
private bool $cache_directory_initialized = false;
/** /**
* The directory to store all Psalm project caches * The directory to store all Psalm project caches
* *
@ -1094,32 +1098,6 @@ class Config
$config->cache_directory .= DIRECTORY_SEPARATOR . sha1($base_dir); $config->cache_directory .= DIRECTORY_SEPARATOR . sha1($base_dir);
$cwd = null;
if ($config->resolve_from_config_file) {
$cwd = getcwd();
chdir($config->base_dir);
}
if (!is_dir($config->cache_directory)) {
try {
if (mkdir($config->cache_directory, 0777, true) === false) {
// any other error than directory already exists/permissions issue
throw new RuntimeException('Failed to create Psalm cache directory for unknown reasons');
}
} catch (RuntimeException $e) {
if (!is_dir($config->cache_directory)) {
// rethrow the error with default message
// it contains the reason why creation failed
throw $e;
}
}
}
if ($cwd) {
chdir($cwd);
}
if (isset($config_xml['serializer'])) { if (isset($config_xml['serializer'])) {
$attribute_text = (string) $config_xml['serializer']; $attribute_text = (string) $config_xml['serializer'];
$config->use_igbinary = $attribute_text === 'igbinary'; $config->use_igbinary = $attribute_text === 'igbinary';
@ -2221,6 +2199,44 @@ class Config
public function getCacheDirectory(): ?string public function getCacheDirectory(): ?string
{ {
if ($this->cache_directory === null) {
return null;
}
if ($this->cache_directory_initialized) {
return $this->cache_directory;
}
$cwd = null;
if ($this->resolve_from_config_file) {
$cwd = getcwd();
chdir($this->base_dir);
}
try {
if (!is_dir($this->cache_directory)) {
try {
if (mkdir($this->cache_directory, 0777, true) === false) {
// any other error than directory already exists/permissions issue
throw new RuntimeException('Failed to create Psalm cache directory for unknown reasons');
}
} catch (RuntimeException $e) {
if (!is_dir($this->cache_directory)) {
// rethrow the error with default message
// it contains the reason why creation failed
throw $e;
}
}
}
} finally {
if ($cwd) {
chdir($cwd);
}
}
$this->cache_directory_initialized = true;
return $this->cache_directory; return $this->cache_directory;
} }
@ -2457,7 +2473,9 @@ class Config
public function setServerMode(): void public function setServerMode(): void
{ {
$this->cache_directory .= '-s'; if ($this->cache_directory !== null) {
$this->cache_directory .= '-s';
}
} }
public function addStubFile(string $stub_file): void public function addStubFile(string $stub_file): void

View File

@ -241,6 +241,10 @@ final class Psalm
$options $options
); );
if (isset($options['no-cache'])) {
$config->cache_directory = null;
}
$config->setIncludeCollector($include_collector); $config->setIncludeCollector($include_collector);
$in_ci = CliUtils::runningInCI(); // disable progressbar on CI $in_ci = CliUtils::runningInCI(); // disable progressbar on CI

View File

@ -233,6 +233,11 @@ final class Psalter
Report::TYPE_CONSOLE, Report::TYPE_CONSOLE,
$first_autoloader $first_autoloader
); );
if (isset($options['no-cache'])) {
$config->cache_directory = null;
}
$config->setIncludeCollector($include_collector); $config->setIncludeCollector($include_collector);
if ($config->resolve_from_config_file) { if ($config->resolve_from_config_file) {