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

fix: Fix compilation of projects using dev composer plugins #879

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .github/workflows/e2e-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ jobs:
- e2e_dockerfile
- e2e_dockerfile_no_extension
- e2e_custom_composer_bin
- e2e_installed_dev
php: [ '8.1', '8.2', '8.3' ]
tools:
- 'composer:2.2'
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ _infection_ci: $(INFECTION_BIN) $(COVERAGE_XML_DIR) $(COVERAGE_JUNIT) vendor
#---------------------------------------------------------------------------

.PHONY: test_e2e
test_e2e: e2e_scoper_alias e2e_scoper_expose_symbols e2e_php_settings_checker_no_restart e2e_php_settings_checker_xdebug_enabled e2e_php_settings_checker_readonly_enabled e2e_php_settings_checker_memory_limit_lower e2e_php_settings_checker_memory_limit_higher e2e_symfony e2e_composer_installed_versions e2e_phpstorm_stubs e2e_dockerfile e2e_dockerfile_no_extension e2e_custom_composer_bin e2e_symfony_runtime
test_e2e: e2e_scoper_alias e2e_scoper_expose_symbols e2e_php_settings_checker_no_restart e2e_php_settings_checker_xdebug_enabled e2e_php_settings_checker_readonly_enabled e2e_php_settings_checker_memory_limit_lower e2e_php_settings_checker_memory_limit_higher e2e_symfony e2e_composer_installed_versions e2e_phpstorm_stubs e2e_dockerfile e2e_dockerfile_no_extension e2e_custom_composer_bin e2e_symfony_runtime e2e_installed_dev


.PHONY: blackfire
Expand Down
24 changes: 24 additions & 0 deletions Makefile.e2e
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ E2E_CUSTOM_COMPOSER_BIN_ACTUAL_STDERR := $(E2E_CUSTOM_COMPOSER_BIN_OUTPUT_DIR)/a
E2E_CUSTOM_COMPOSER_BIN_ACTUAL_COMPILATION := $(E2E_CUSTOM_COMPOSER_BIN_OUTPUT_DIR)/actual-compilation
E2E_CUSTOM_COMPOSER_BIN_PHAR := $(E2E_CUSTOM_COMPOSER_BIN_OUTPUT_DIR)/index.phar

E2E_COMPOSER_INSTALLED_DEV_DIR = fixtures/build/dir017
E2E_COMPOSER_INSTALLED_DEV_OUTPUT_DIR = dist/dir017
E2E_COMPOSER_INSTALLED_DEV_EXPECTED_OUTPUT := $(E2E_COMPOSER_INSTALLED_DEV_DIR)/expected-output
E2E_COMPOSER_INSTALLED_DEV_ACTUAL_OUTPUT := $(E2E_COMPOSER_INSTALLED_DEV_OUTPUT_DIR)/actual-output

ifeq ($(OS),Darwin)
SED = sed -i ''
else
Expand Down Expand Up @@ -414,6 +419,18 @@ e2e_custom_composer_bin: $(SCOPED_BOX_BIN) $(E2E_CUSTOM_COMPOSER_BIN_DIR)/vendor
$(E2E_CUSTOM_COMPOSER_BIN_ACTUAL_COMPILATION)


.PHONY: e2e_installed_dev
e2e_installed_dev: $(SCOPED_BOX_BIN) $(E2E_COMPOSER_INSTALLED_DEV_DIR)/vendor
@# Packages an app with dev composer plugins
$(SCOPED_BOX) compile --working-dir=$(E2E_COMPOSER_INSTALLED_DEV_DIR) --no-parallel --ansi

@mkdir -p $(E2E_COMPOSER_INSTALLED_DEV_OUTPUT_DIR)
php $(E2E_COMPOSER_INSTALLED_DEV_DIR)/index.phar > $(E2E_COMPOSER_INSTALLED_DEV_ACTUAL_OUTPUT)
mv -vf $(E2E_COMPOSER_INSTALLED_DEV_DIR)/index.phar $(E2E_COMPOSER_INSTALLED_DEV_OUTPUT_DIR)/index.phar

$(DIFF) $(E2E_COMPOSER_INSTALLED_DEV_EXPECTED_OUTPUT) $(E2E_COMPOSER_INSTALLED_DEV_ACTUAL_OUTPUT)


#
# Rules from files
#---------------------------------------------------------------------------
Expand Down Expand Up @@ -472,6 +489,13 @@ $(E2E_DOCKERFILE_NO_EXT_PHAR): $(SCOPED_BOX_BIN) $(E2E_DOCKERFILE_NO_EXT_DIR)/in
$(SCOPED_BOX) compile --ansi --working-dir=$(E2E_DOCKERFILE_NO_EXT_DIR)
touch -c $@

$(E2E_COMPOSER_INSTALLED_DEV_DIR)/vendor: $(E2E_COMPOSER_INSTALLED_DEV_DIR)/composer.lock
composer install --ansi --working-dir=$(E2E_COMPOSER_INSTALLED_DEV_DIR)
touch -c $@
$(E2E_COMPOSER_INSTALLED_DEV_DIR)/composer.lock: $(E2E_COMPOSER_INSTALLED_DEV_DIR)/composer.json
@echo "$(ERROR_COLOR)$(@) is not up to date. You may want to run the following command:$(NO_COLOR)"
@echo "$$ composer update --lock --working-dir=$(E2E_COMPOSER_INSTALLED_DEV_DIR) && touch -c $(@)"

.PHONY: docker_images
docker_images:
./.docker/build
Expand Down
2 changes: 2 additions & 0 deletions fixtures/build/dir017/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/*.phar
/vendor/
1 change: 1 addition & 0 deletions fixtures/build/dir017/box.json.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
16 changes: 16 additions & 0 deletions fixtures/build/dir017/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"bin": "index.php",
"require": {
"php": "^8.1",
"nikic/iter": "^2.2"
},
"require-dev": {
"phpstan/phpstan": "^1.9",
"phpstan/extension-installer": "^1.2"
},
"config": {
"allow-plugins": {
"phpstan/extension-installer": true
}
}
}
175 changes: 175 additions & 0 deletions fixtures/build/dir017/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions fixtures/build/dir017/expected-output
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The package "nikic/iter" is installed.
The package "phpstan/extension-installer" is NOT installed.
31 changes: 31 additions & 0 deletions fixtures/build/dir017/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

/*
* This file is part of the box project.
*
* (c) Kevin Herrera <kevin@herrera.io>
* Théo Fidry <theo.fidry@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

use Composer\InstalledVersions;
use function iter\toIter;

require __DIR__.'/vendor/autoload.php';

$packages = toIter([
'nikic/iter',
'phpstan/extension-installer',
]);

foreach ($packages as $package) {
echo sprintf(
'The package "%s" is %sinstalled.'.PHP_EOL,
$package,
InstalledVersions::isInstalled($package) ? '' : 'NOT ',
);
}
62 changes: 61 additions & 1 deletion src/Composer/ComposerOrchestrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Process;
use function file_exists;
use function file_get_contents;
use function file_put_contents;
use function KevinGH\Box\FileSystem\dump_file;
use function KevinGH\Box\FileSystem\file_contents;
use function preg_replace;
Expand Down Expand Up @@ -114,7 +117,8 @@ public static function dumpAutoload(

$composerExecutable = $composerBin ?? self::retrieveComposerExecutable();

self::dumpAutoloader($composerExecutable, true === $excludeDevFiles, $logger);
self::removeDevInstallPackages($composerExecutable, $excludeDevFiles, $logger);
self::dumpAutoloader($composerExecutable, $excludeDevFiles, $logger);

if ('' !== $prefix) {
$autoloadFile = self::retrieveAutoloadFile($composerExecutable, $logger);
Expand Down Expand Up @@ -177,6 +181,62 @@ private static function retrieveComposerExecutable(): string
return $composer;
}

private static function removeDevInstallPackages(string $composerExecutable, bool $noDev, CompilerLogger $logger): void
{
if (false === $noDev) {
return;
}

$composerCommand = [$composerExecutable, 'install', '--no-dev', '--no-scripts', '--no-plugins'];

if (null !== $verbosity = self::retrieveSubProcessVerbosity($logger->getIO())) {
$composerCommand[] = $verbosity;
}

if ($logger->getIO()->isDecorated()) {
$composerCommand[] = '--ansi';
}

$removeDevInstallPackages = new Process($composerCommand);

$logger->log(
CompilerLogger::CHEVRON_PREFIX,
$removeDevInstallPackages->getCommandLine(),
OutputInterface::VERBOSITY_VERBOSE,
);

$composerInstalledVersionsPath = 'vendor/composer/InstalledVersions.php';

if (file_exists($composerInstalledVersionsPath)) {
$composerInstalledVersionsContents = file_get_contents($composerInstalledVersionsPath);
}

$removeDevInstallPackages->run(null, self::getDefaultEnvVars());

if (false === $removeDevInstallPackages->isSuccessful()) {
throw new RuntimeException(
'Could not remove the dev packages.',
0,
new ProcessFailedException($removeDevInstallPackages),
);
}

if ('' !== $output = $removeDevInstallPackages->getOutput()) {
$logger->getIO()->writeln($output, OutputInterface::VERBOSITY_VERBOSE);
}

if ('' !== $output = $removeDevInstallPackages->getErrorOutput()) {
$logger->getIO()->writeln($output, OutputInterface::VERBOSITY_VERBOSE);
}

if (isset($composerInstalledVersionsContents)) {
file_put_contents(
$composerInstalledVersionsPath,
$composerInstalledVersionsContents,
);
}
}

private static function dumpAutoloader(string $composerExecutable, bool $noDev, CompilerLogger $logger): void
{
$composerCommand = [$composerExecutable, 'dump-autoload', '--classmap-authoritative'];
Expand Down
10 changes: 0 additions & 10 deletions tests/Composer/ComposerOrchestratorComposer22Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,16 +343,6 @@ public function test_it_can_dump_the_autoloader_with_a_composer_json_lock_and_in
'composer.json',
'composer.lock',
'vendor/autoload.php',
'vendor/beberlei/assert/composer.json',
'vendor/beberlei/assert/lib/Assert/Assert.php',
'vendor/beberlei/assert/lib/Assert/Assertion.php',
'vendor/beberlei/assert/lib/Assert/AssertionChain.php',
'vendor/beberlei/assert/lib/Assert/AssertionFailedException.php',
'vendor/beberlei/assert/lib/Assert/functions.php',
'vendor/beberlei/assert/lib/Assert/InvalidArgumentException.php',
'vendor/beberlei/assert/lib/Assert/LazyAssertion.php',
'vendor/beberlei/assert/lib/Assert/LazyAssertionException.php',
'vendor/beberlei/assert/LICENSE',
'vendor/composer/autoload_classmap.php',
'vendor/composer/autoload_namespaces.php',
'vendor/composer/autoload_psr4.php',
Expand Down
Loading