Fix npm build perms during upgrade

This commit is contained in:
root
2026-02-01 01:48:39 +02:00
parent 3d709093c1
commit cb845e88c2
3 changed files with 73 additions and 15 deletions

View File

@@ -1 +1 @@
VERSION=0.9-rc35
VERSION=0.9-rc36

View File

@@ -185,6 +185,11 @@ class UpgradeCommand extends Command
try {
$this->ensureCommandAvailable('npm');
$this->ensureNpmCacheDirectory();
$this->ensureNodeModulesPermissions();
if (! $this->isNodeModulesWritable()) {
$this->warn('Skipping frontend build because node_modules is not writable by the current user.');
$this->warn('Run: sudo chown -R www-data:www-data '.$this->getNodeModulesPath());
} else {
$npmInstall = File::exists($this->basePath.'/package-lock.json') ? 'npm ci' : 'npm install';
$installResult = $this->executeCommand($npmInstall, 1200);
if ($installResult['exitCode'] !== 0) {
@@ -201,6 +206,9 @@ class UpgradeCommand extends Command
if ($buildResult['output'] !== '') {
$this->line($buildResult['output']);
}
$this->ensureNodeModulesPermissions();
}
} catch (Exception $e) {
$this->error('Asset build failed: '.$e->getMessage());
@@ -428,6 +436,7 @@ class UpgradeCommand extends Command
$paths = [
$this->basePath.'/database',
$this->basePath.'/storage',
$this->getNodeModulesPath(),
$this->getNpmCacheDir(),
$this->getPuppeteerCacheDir(),
$this->getXdgCacheDir(),
@@ -465,11 +474,56 @@ class UpgradeCommand extends Command
}
protected function ensureNodeModulesPermissions(): void
{
$nodeModules = $this->getNodeModulesPath();
if (! File::isDirectory($nodeModules)) {
return;
}
if ($this->isRunningAsRoot() && $this->userExists('www-data')) {
$escaped = escapeshellarg($nodeModules);
$this->executeCommand("chgrp -R www-data {$escaped}");
$this->executeCommand("chmod -R g+rwX {$escaped}");
$this->executeCommand("find {$escaped} -type d -exec chmod g+s {} +");
return;
}
$this->executeCommand('chmod -R u+rwX '.escapeshellarg($nodeModules));
}
protected function isNodeModulesWritable(): bool
{
$nodeModules = $this->getNodeModulesPath();
if (! File::isDirectory($nodeModules)) {
return true;
}
$binPath = $nodeModules.'/.bin';
if (! is_writable($nodeModules)) {
return false;
}
if (File::isDirectory($binPath) && ! is_writable($binPath)) {
return false;
}
return true;
}
protected function getNpmCacheDir(): string
{
return $this->basePath.'/storage/npm-cache';
}
protected function getNodeModulesPath(): string
{
return $this->basePath.'/node_modules';
}
protected function getPuppeteerCacheDir(): string
{
return $this->basePath.'/storage/puppeteer-cache';

View File

@@ -132,6 +132,10 @@ class UpgradeCommandTest extends TestCase
$paths = [
base_path().'/database',
base_path().'/storage',
base_path().'/node_modules',
base_path().'/storage/npm-cache',
base_path().'/storage/puppeteer-cache',
base_path().'/storage/.cache',
base_path().'/bootstrap/cache',
];