Skip to content

Commit

Permalink
Defer cache directory creation until it's needed
Browse files Browse the repository at this point in the history
Honestly, this is a bit of a hack, as we let `Config` to generate the
cache directory name and then reset it to null from the cli entrypoint.
Yet it's easier than passing a no-cache flag through so many layers of
static calls.

`$this->cache_directory_initialized` flag is used to make sure we
attempt to create the directory only once.

Fixes #4267
  • Loading branch information
weirdan committed Nov 29, 2022
1 parent 9d597cf commit 6b3dd56
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 27 deletions.
72 changes: 45 additions & 27 deletions src/Psalm/Config.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 6b3dd56

Please sign in to comment.