Skip to content

Commit 6b3dd56

Browse files
committed
Defer cache directory creation until it's needed
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
1 parent 9d597cf commit 6b3dd56

File tree

3 files changed

+54
-27
lines changed

3 files changed

+54
-27
lines changed

src/Psalm/Config.php

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,14 @@ class Config
204204
/**
205205
* The directory to store PHP Parser (and other) caches
206206
*
207+
* @internal
208+
*
207209
* @var string|null
208210
*/
209211
public $cache_directory;
210212

213+
private bool $cache_directory_initialized = false;
214+
211215
/**
212216
* The directory to store all Psalm project caches
213217
*
@@ -1094,32 +1098,6 @@ private static function fromXmlAndPaths(
10941098

10951099
$config->cache_directory .= DIRECTORY_SEPARATOR . sha1($base_dir);
10961100

1097-
$cwd = null;
1098-
1099-
if ($config->resolve_from_config_file) {
1100-
$cwd = getcwd();
1101-
chdir($config->base_dir);
1102-
}
1103-
1104-
if (!is_dir($config->cache_directory)) {
1105-
try {
1106-
if (mkdir($config->cache_directory, 0777, true) === false) {
1107-
// any other error than directory already exists/permissions issue
1108-
throw new RuntimeException('Failed to create Psalm cache directory for unknown reasons');
1109-
}
1110-
} catch (RuntimeException $e) {
1111-
if (!is_dir($config->cache_directory)) {
1112-
// rethrow the error with default message
1113-
// it contains the reason why creation failed
1114-
throw $e;
1115-
}
1116-
}
1117-
}
1118-
1119-
if ($cwd) {
1120-
chdir($cwd);
1121-
}
1122-
11231101
if (isset($config_xml['serializer'])) {
11241102
$attribute_text = (string) $config_xml['serializer'];
11251103
$config->use_igbinary = $attribute_text === 'igbinary';
@@ -2221,6 +2199,44 @@ public function visitStubFiles(Codebase $codebase, ?Progress $progress = null):
22212199

22222200
public function getCacheDirectory(): ?string
22232201
{
2202+
if ($this->cache_directory === null) {
2203+
return null;
2204+
}
2205+
2206+
if ($this->cache_directory_initialized) {
2207+
return $this->cache_directory;
2208+
}
2209+
2210+
$cwd = null;
2211+
2212+
if ($this->resolve_from_config_file) {
2213+
$cwd = getcwd();
2214+
chdir($this->base_dir);
2215+
}
2216+
2217+
try {
2218+
if (!is_dir($this->cache_directory)) {
2219+
try {
2220+
if (mkdir($this->cache_directory, 0777, true) === false) {
2221+
// any other error than directory already exists/permissions issue
2222+
throw new RuntimeException('Failed to create Psalm cache directory for unknown reasons');
2223+
}
2224+
} catch (RuntimeException $e) {
2225+
if (!is_dir($this->cache_directory)) {
2226+
// rethrow the error with default message
2227+
// it contains the reason why creation failed
2228+
throw $e;
2229+
}
2230+
}
2231+
}
2232+
} finally {
2233+
if ($cwd) {
2234+
chdir($cwd);
2235+
}
2236+
}
2237+
2238+
$this->cache_directory_initialized = true;
2239+
22242240
return $this->cache_directory;
22252241
}
22262242

@@ -2457,7 +2473,9 @@ public static function removeCacheDirectory(string $dir): void
24572473

24582474
public function setServerMode(): void
24592475
{
2460-
$this->cache_directory .= '-s';
2476+
if ($this->cache_directory !== null) {
2477+
$this->cache_directory .= '-s';
2478+
}
24612479
}
24622480

24632481
public function addStubFile(string $stub_file): void

src/Psalm/Internal/Cli/Psalm.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ public static function run(array $argv): void
241241
$options
242242
);
243243

244+
if (isset($options['no-cache'])) {
245+
$config->cache_directory = null;
246+
}
247+
244248
$config->setIncludeCollector($include_collector);
245249

246250
$in_ci = CliUtils::runningInCI(); // disable progressbar on CI

src/Psalm/Internal/Cli/Psalter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ public static function run(array $argv): void
233233
Report::TYPE_CONSOLE,
234234
$first_autoloader
235235
);
236+
237+
if (isset($options['no-cache'])) {
238+
$config->cache_directory = null;
239+
}
240+
236241
$config->setIncludeCollector($include_collector);
237242

238243
if ($config->resolve_from_config_file) {

0 commit comments

Comments
 (0)