Skip to content

Commit

Permalink
Merge pull request #8789 from weirdan/defer-cache-directory-creation
Browse files Browse the repository at this point in the history
Fixes #4267
  • Loading branch information
weirdan committed Nov 30, 2022
2 parents 4e177bf + 6fd2ff1 commit 870f581
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 27 deletions.
3 changes: 3 additions & 0 deletions UPGRADING.md
Expand Up @@ -27,6 +27,9 @@

- [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
to `$analysis_php_version_id` and changed from `array|null` to `int|null`.
Previously it accepted PHP version as `array{major_version, minor_version}`
Expand Down
72 changes: 45 additions & 27 deletions src/Psalm/Config.php
Expand Up @@ -204,10 +204,14 @@ class Config
/**
* The directory to store PHP Parser (and other) caches
*
* @internal
*
* @var string|null
*/
public $cache_directory;

private bool $cache_directory_initialized = false;

/**
* The directory to store all Psalm project caches
*
Expand Down Expand Up @@ -1094,32 +1098,6 @@ private static function fromXmlAndPaths(

$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'])) {
$attribute_text = (string) $config_xml['serializer'];
$config->use_igbinary = $attribute_text === 'igbinary';
Expand Down Expand Up @@ -2221,6 +2199,44 @@ public function visitStubFiles(Codebase $codebase, ?Progress $progress = null):

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;
}

Expand Down Expand Up @@ -2457,7 +2473,9 @@ public static function removeCacheDirectory(string $dir): 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
Expand Down
4 changes: 4 additions & 0 deletions src/Psalm/Internal/Cli/Psalm.php
Expand Up @@ -241,6 +241,10 @@ public static function run(array $argv): void
$options
);

if (isset($options['no-cache'])) {
$config->cache_directory = null;
}

$config->setIncludeCollector($include_collector);

$in_ci = CliUtils::runningInCI(); // disable progressbar on CI
Expand Down
5 changes: 5 additions & 0 deletions src/Psalm/Internal/Cli/Psalter.php
Expand Up @@ -233,6 +233,11 @@ public static function run(array $argv): void
Report::TYPE_CONSOLE,
$first_autoloader
);

if (isset($options['no-cache'])) {
$config->cache_directory = null;
}

$config->setIncludeCollector($include_collector);

if ($config->resolve_from_config_file) {
Expand Down

0 comments on commit 870f581

Please sign in to comment.