Skip to content

Commit 40e6def

Browse files
ENGCOM-5612: Handle the error properly when LESS source file is empty or having compilation error, and make the error message clear and clean. #22182
- Merge Pull Request #22182 from Hailong/magento2:empty-less-source-error-handling - Merged commits: 1. 517788a 2. e54c7f7 3. b4a7ca4
2 parents b31bb4f + b4a7ca4 commit 40e6def

File tree

5 files changed

+50
-11
lines changed

5 files changed

+50
-11
lines changed

app/code/Magento/Deploy/Package/Package.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ public function __construct(
150150
}
151151

152152
/**
153+
* Get area.
154+
*
153155
* @return string
154156
*/
155157
public function getArea()
@@ -158,6 +160,8 @@ public function getArea()
158160
}
159161

160162
/**
163+
* Get parent.
164+
*
161165
* @return Package
162166
*/
163167
public function getParent()
@@ -166,6 +170,8 @@ public function getParent()
166170
}
167171

168172
/**
173+
* Get theme.
174+
*
169175
* @return string
170176
*/
171177
public function getTheme()
@@ -174,6 +180,8 @@ public function getTheme()
174180
}
175181

176182
/**
183+
* Get locale.
184+
*
177185
* @return string
178186
*/
179187
public function getLocale()
@@ -204,6 +212,8 @@ public function isVirtual()
204212
}
205213

206214
/**
215+
* Get param.
216+
*
207217
* @param string $name
208218
* @return mixed|null
209219
*/
@@ -213,6 +223,8 @@ public function getParam($name)
213223
}
214224

215225
/**
226+
* Set param.
227+
*
216228
* @param string $name
217229
* @param mixed $value
218230
* @return bool
@@ -320,7 +332,10 @@ public function getFilesByType($type)
320332
*/
321333
public function deleteFile($fileId)
322334
{
335+
$file = $this->files[$fileId];
336+
$deployedFileId = $file->getDeployedFileId();
323337
unset($this->files[$fileId]);
338+
unset($this->map[$deployedFileId]);
324339
}
325340

326341
/**
@@ -348,6 +363,8 @@ public function aggregate(Package $parentPackage = null)
348363
}
349364

350365
/**
366+
* Set parent.
367+
*
351368
* @param Package $parent
352369
* @return bool
353370
*/
@@ -368,6 +385,8 @@ public function getMap()
368385
}
369386

370387
/**
388+
* Get state.
389+
*
371390
* @return int
372391
*/
373392
public function getState()
@@ -376,6 +395,8 @@ public function getState()
376395
}
377396

378397
/**
398+
* Set state.
399+
*
379400
* @param int $state
380401
* @return bool
381402
*/
@@ -386,6 +407,8 @@ public function setState($state)
386407
}
387408

388409
/**
410+
* Get inheritance level.
411+
*
389412
* @return int
390413
*/
391414
public function getInheritanceLevel()
@@ -422,6 +445,7 @@ public function getParentMap()
422445
{
423446
$map = [];
424447
foreach ($this->getParentPackages() as $parentPackage) {
448+
// phpcs:ignore Magento2.Performance.ForeachArrayMerge.ForeachArrayMerge
425449
$map = array_merge($map, $parentPackage->getMap());
426450
}
427451
return $map;
@@ -438,8 +462,10 @@ public function getParentFiles($type = null)
438462
$files = [];
439463
foreach ($this->getParentPackages() as $parentPackage) {
440464
if ($type === null) {
465+
// phpcs:ignore Magento2.Performance.ForeachArrayMerge.ForeachArrayMerge
441466
$files = array_merge($files, $parentPackage->getFiles());
442467
} else {
468+
// phpcs:ignore Magento2.Performance.ForeachArrayMerge.ForeachArrayMerge
443469
$files = array_merge($files, $parentPackage->getFilesByType($type));
444470
}
445471
}
@@ -477,6 +503,8 @@ public function getParentPackages()
477503
}
478504

479505
/**
506+
* Get pre processors.
507+
*
480508
* @return Processor\ProcessorInterface[]
481509
*/
482510
public function getPreProcessors()
@@ -485,6 +513,8 @@ public function getPreProcessors()
485513
}
486514

487515
/**
516+
* Get post processors.
517+
*
488518
* @return Processor\ProcessorInterface[]
489519
*/
490520
public function getPostProcessors()

app/code/Magento/Deploy/Service/DeployPackage.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,10 @@ public function deployEmulated(Package $package, array $options, $skipLogging =
134134
} catch (ContentProcessorException $exception) {
135135
$errorMessage = __('Compilation from source: ')
136136
. $file->getSourcePath()
137-
. PHP_EOL . $exception->getMessage();
137+
. PHP_EOL . $exception->getMessage() . PHP_EOL;
138138
$this->errorsCount++;
139139
$this->logger->critical($errorMessage);
140+
$package->deleteFile($file->getFileId());
140141
} catch (\Exception $exception) {
141142
$this->logger->critical(
142143
'Compilation from source ' . $file->getSourcePath() . ' failed' . PHP_EOL . (string)$exception

dev/tests/integration/testsuite/Magento/Email/Model/Template/FilterTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Framework\App\State;
1010
use Magento\Framework\App\TemplateTypesInterface;
1111
use Magento\Framework\Phrase;
12+
use Magento\Framework\View\Asset\ContentProcessorInterface;
1213
use Magento\Setup\Module\I18n\Locale;
1314
use Magento\Theme\Block\Html\Footer;
1415

@@ -267,7 +268,7 @@ public function cssDirectiveDataProvider()
267268
'Empty or missing file' => [
268269
TemplateTypesInterface::TYPE_HTML,
269270
'file="css/non-existent-file.css"',
270-
'/* Contents of the specified CSS file could not be loaded or is empty */'
271+
'/*' . PHP_EOL . ContentProcessorInterface::ERROR_MESSAGE_PREFIX . 'LESS file is empty: ',
271272
],
272273
'File with compilation error results in error message' => [
273274
TemplateTypesInterface::TYPE_HTML,
@@ -407,10 +408,12 @@ public function inlinecssDirectiveThrowsExceptionWhenMissingParameterDataProvide
407408
protected function setUpDesignParams()
408409
{
409410
$themeCode = 'Vendor_EmailTest/custom_theme';
410-
$this->model->setDesignParams([
411-
'area' => Area::AREA_FRONTEND,
412-
'theme' => $themeCode,
413-
'locale' => Locale::DEFAULT_SYSTEM_LOCALE,
414-
]);
411+
$this->model->setDesignParams(
412+
[
413+
'area' => Area::AREA_FRONTEND,
414+
'theme' => $themeCode,
415+
'locale' => Locale::DEFAULT_SYSTEM_LOCALE,
416+
]
417+
);
415418
}
416419
}

lib/internal/Magento/Framework/Css/PreProcessor/Adapter/Less/Processor.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ public function __construct(
6161

6262
/**
6363
* @inheritdoc
64-
* @throws ContentProcessorException
6564
*/
6665
public function processContent(File $asset)
6766
{
@@ -77,7 +76,9 @@ public function processContent(File $asset)
7776
$content = $this->assetSource->getContent($asset);
7877

7978
if (trim($content) === '') {
80-
return '';
79+
throw new ContentProcessorException(
80+
new Phrase('Compilation from source: LESS file is empty: ' . $path)
81+
);
8182
}
8283

8384
$tmpFilePath = $this->temporaryFile->createFile($path, $content);
@@ -88,8 +89,9 @@ public function processContent(File $asset)
8889
gc_enable();
8990

9091
if (trim($content) === '') {
91-
$this->logger->warning('Parsed less file is empty: ' . $path);
92-
return '';
92+
throw new ContentProcessorException(
93+
new Phrase('Compilation from source: LESS file is empty: ' . $path)
94+
);
9395
} else {
9496
return $content;
9597
}

lib/internal/Magento/Framework/Css/Test/Unit/PreProcessor/Adapter/Less/ProcessorTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ public function testProcessContentException()
111111

112112
/**
113113
* Test for processContent method (empty content)
114+
*
115+
* @expectedException \Magento\Framework\View\Asset\ContentProcessorException
116+
* @expectedExceptionMessageRegExp (Compilation from source: LESS file is empty: test-path)
114117
*/
115118
public function testProcessContentEmpty()
116119
{

0 commit comments

Comments
 (0)