Skip to content

Commit

Permalink
All temporary directories created by compile should be removed by rem…
Browse files Browse the repository at this point in the history
…oveCompiledFiles as well as files

The order of unlink and rmdir is crucial. Since files exist within
directories, rmdir won't work until the files are deleted.
  • Loading branch information
ernix committed Mar 4, 2024
1 parent 1324974 commit ef947cd
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/Workspaces/Concerns/CompilesWorkspace.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ trait CompilesWorkspace
*/
protected array $compiledFiles = [];

/**
* A list of all directories created by the workspace compiler.
*
* @var string[]
*/
protected array $createdDirs = [];

/**
* The path to the last temporary directory used.
*
Expand Down Expand Up @@ -81,6 +88,10 @@ public function removeCompiledFiles(): Workspace
@unlink($compiledPath);
}

foreach ($this->createdDirs as $dir) {
@rmdir($dir);
}

return $this;
}

Expand Down Expand Up @@ -137,7 +148,6 @@ public function compile(string $outputDirectory): void
{
$outputDirectory = Paths::normalizePathWithTrailingSlash($outputDirectory);
$this->lastTempDirectory = $outputDirectory;
$createdDirs = [];

try {
/** @var Document $doc */
Expand All @@ -157,17 +167,14 @@ public function compile(string $outputDirectory): void

if (! file_exists($dir)) {
@mkdir($dir, 0755, true);
$createdDirs[] = $dir;
$this->createdDirs[] = $dir;
}

file_put_contents($compilePath, $result);
$this->compiledFiles[$compilePath] = $doc->getFilePath();
$this->compiledDocuments[$compilePath] = $doc;
}
} catch (Exception $e) {
foreach ($createdDirs as $dir) {
@rmdir($dir);
}
$this->removeCompiledFiles();

throw $e;
Expand Down

0 comments on commit ef947cd

Please sign in to comment.