Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add array_unique call to prevent loading the same file multiple times. #11109

Merged
merged 7 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/Composer/Autoload/AutoloadGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,10 +658,26 @@ protected function getIncludePathsFile(array $packageMap, Filesystem $filesystem
*/
protected function getIncludeFilesFile(array $files, Filesystem $filesystem, string $basePath, string $vendorPath, string $vendorPathCode, string $appBaseDirCode)
{
// Get the path to each file, and make sure these paths are unique.
$files = array_map(
function (string $functionFile) use ($filesystem, $basePath, $vendorPath): string {
return $this->getPathCode($filesystem, $basePath, $vendorPath, $functionFile);
},
$files
);
$uniqueFiles = array_unique($files);
if (count($uniqueFiles) < count($files)) {
$this->io->writeError('<warning>The following "files" autoload rules are included multiple times, this may cause issues and should be resolved:</warning>');
foreach (array_unique(array_diff_assoc($files, $uniqueFiles)) as $duplicateFile) {
$this->io->writeError('<warning> - '.$duplicateFile.'</warning>');
}
}
unset($uniqueFiles);

$filesCode = '';

foreach ($files as $fileIdentifier => $functionFile) {
$filesCode .= ' ' . var_export($fileIdentifier, true) . ' => '
. $this->getPathCode($filesystem, $basePath, $vendorPath, $functionFile) . ",\n";
$filesCode .= ' ' . var_export($fileIdentifier, true) . ' => ' . $functionFile . ",\n";
}

if (!$filesCode) {
Expand Down
36 changes: 35 additions & 1 deletion tests/Composer/Test/Autoload/AutoloadGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

use Composer\Autoload\AutoloadGenerator;
use Composer\Filter\PlatformRequirementFilter\PlatformRequirementFilterFactory;
use Composer\IO\BufferIO;
use Composer\IO\IOInterface;
use Composer\Package\CompletePackage;
use Composer\Package\Link;
use Composer\Package\Version\VersionParser;
Expand Down Expand Up @@ -74,6 +76,11 @@ class AutoloadGeneratorTest extends TestCase
*/
private $fs;

/**
* @var BufferIO
*/
private $io;

/**
* @var EventDispatcher&MockObject
*/
Expand Down Expand Up @@ -109,6 +116,8 @@ protected function setUp(): void
},
];

$this->io = new BufferIO();

$this->config->expects($this->atLeastOnce())
->method('get')
->will($this->returnCallback(function ($arg) {
Expand Down Expand Up @@ -145,7 +154,7 @@ protected function setUp(): void
->disableOriginalConstructor()
->getMock();

$this->generator = new AutoloadGenerator($this->eventDispatcher);
$this->generator = new AutoloadGenerator($this->eventDispatcher, $this->io);
}

protected function tearDown(): void
Expand Down Expand Up @@ -369,6 +378,31 @@ public function testRootPackageAutoloadingWithTargetDir(): void
$this->assertAutoloadFiles('classmap6', $this->vendorDir.'/composer', 'classmap');
}

public function testDuplicateFilesWarning(): void
{
$package = new RootPackage('root/a', '1.0', '1.0');
$package->setAutoload([
'files' => ['foo.php', 'bar.php', './foo.php', '././foo.php'],
]);

$this->repository->expects($this->once())
->method('getCanonicalPackages')
->will($this->returnValue([]));

$this->fs->ensureDirectoryExists($this->vendorDir.'/a');
$this->fs->ensureDirectoryExists($this->workingDir.'/src');
$this->fs->ensureDirectoryExists($this->workingDir.'/lib');

file_put_contents($this->workingDir.'/foo.php', '<?php class FilesFoo {}');
file_put_contents($this->workingDir.'/bar.php', '<?php class FilesBar {}');

$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesWarning');
self::assertFileContentEquals(__DIR__.'/Fixtures/autoload_files_duplicates.php', $this->vendorDir.'/composer/autoload_files.php');
$expected = '<warning>The following "files" autoload rules are included multiple times, this may cause issues and should be resolved:</warning>'.PHP_EOL.
'<warning> - $baseDir . \'/foo.php\'</warning>'.PHP_EOL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we really output the PHP code in the warnings ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was also wondering about this.. but I think at least it is clear what dir this is, otherwise you'd just see "./foo" and we don't have the reference to the package or anything.. So I find this is maybe clearer.

self::assertEquals($expected, $this->io->getOutput());;
}

public function testVendorsAutoloading(): void
{
$package = new RootPackage('root/a', '1.0', '1.0');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

// autoload_files.php @generated by Composer

$vendorDir = dirname(__DIR__);
$baseDir = dirname($vendorDir);

return array(
'b4b4f2eb2151c57cadbd5714a7aba8b7' => $baseDir . '/foo.php',
'99b24fc198db06c1d2d8118a8a5475eb' => $baseDir . '/bar.php',
'6bad5af0771cca3d076e69b25d0791bb' => $baseDir . '/foo.php',
'51841489e2c601aedd3623cb72708483' => $baseDir . '/foo.php',
);
Loading