Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defer cache directory creation until it's needed #8789

Merged
merged 2 commits into from Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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