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: pass all valid options to nested commands in ddev composer create, fixes #6300, fixes #6246, for #5058 #6303

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

stasadev
Copy link
Member

@stasadev stasadev commented Jun 12, 2024

The Issue

Composer options were not respected by composer run-script.

How This PR Solves The Issue

Makes ddev composer create fully compatible with all possible composer options. I removed all the hard-coded options.
Adds composer options to composer run-script.
Adds --no-interaction to CakePHP quickstart.


--preserve-flags (which doesn't add --no-plugins and --no-scripts) didn't work as expected, I decided to remove it.

This is not a breaking change, because --preserve-flags didn't work before (it was always false).


I moved composer run-script post-create-project-cmd outside the condition for composer install, to make it work the same way as in composer create-project.


If you passed --no-scripts, DDEV tried to run composer run-script, which was wrong behavior, fixed it.


If --help or --version flags were provided, composer showed it's help page or it's version, and DDEV tried to continue creating the project, which was wrong behavior, fixed it.


I refactored the test logic to make it more clear and added more tests. I removed the testing for the psr/log package and replaced it with custom-made fake packages.

Manual Testing Instructions

  1. The order of commands executed for ddev composer create must be the same as for composer create-project.

To check this behavior, you can use a test repository (it is used for testdata here) which contains all the necessary situations to check:

cd ~/workspace && mkdir composer-create && cd composer-create
ddev config --auto
mkdir -p .ddev/test-ddev-composer-create/{test-ddev-require,test-ddev-require-dev}

curl -o .ddev/test-ddev-composer-create/composer.json https://raw.githubusercontent.com/stasadev/ddev/20240612_stasadev_composer_create/cmd/ddev/cmd/testdata/TestComposerCreateCmd/.ddev/test-ddev-composer-create/composer.json
curl -o .ddev/test-ddev-composer-create/test-ddev-require/composer.json https://raw.githubusercontent.com/stasadev/ddev/20240612_stasadev_composer_create/cmd/ddev/cmd/testdata/TestComposerCreateCmd/.ddev/test-ddev-composer-create/test-ddev-require/composer.json
curl -o .ddev/test-ddev-composer-create/test-ddev-require-dev/composer.json https://raw.githubusercontent.com/stasadev/ddev/20240612_stasadev_composer_create/cmd/ddev/cmd/testdata/TestComposerCreateCmd/.ddev/test-ddev-composer-create/test-ddev-require-dev/composer.json

export repo='{"type": "path", "url": ".ddev/test-ddev-composer-create", "options": {"symlink": false}}'

# see the result for:
composer create-project --repository $repo test/ddev-composer-create # add different flags here

# clear the directory
ls | grep -xv ".ddev" | xargs rm -rf

# see the result for:
ddev composer create --repository $repo test/ddev-composer-create # add different flags here

# clear the directory
ls | grep -xv ".ddev" | xargs rm -rf

test/ddev-composer-create package (.ddev/test-ddev-composer-create/composer.json) contains this:

  • it requires test/ddev-require from require section:
    • vendor/test/ddev-require will be here, but only if there is no --no-install flag
  • it requires test/ddev-require-dev from require-dev section:
    • vendor/test/ddev-require-dev will be here, but only if there is no --no-dev/--no-install flag
  • it runs post-root-package-install script:
    • this script should run before composer install
    • it creates created-by-post-root-package-install file, but only if there is no --no-scripts flag
  • it runs post-create-project-cmd script:
    • this script should run after composer install
    • it creates created-by-post-create-project-cmd file, but only if there is no --no-scripts flag

And use different flag combinations to see how it works for composer create-project and ddev composer create. It is important to check if the commands run in the same order.

Available flags for composer create-project https://getcomposer.org/doc/03-cli.md#create-project

Ignore the test-ddev-require and test-ddev-require-dev directories created in the project root (they are expected to be created every time as they are part of the composer package).

Every ddev composer create will have this output:

Executing composer command: [composer ...]

You can see what flags are used for each of these composer commands, and flags like --no-interaction will be also used for composer run-script and composer install.

For example:

This should not create vendor folder, and there should be no created-by-* files

# see the result for:
composer create-project --repository $repo test/ddev-composer-create --no-install --no-scripts

# clear the directory
ls | grep -xv ".ddev" | xargs rm -rf

# see the result for:
ddev composer create --repository $repo test/ddev-composer-create --no-install --no-scripts

# clear the directory
ls | grep -xv ".ddev" | xargs rm -rf

This should create vendor/test/ddev-require folder, (but not vendor/test/ddev-require-dev) and there should be no created-by-* files

# see the result for:
composer create-project --repository $repo test/ddev-composer-create --no-scripts --no-dev

# clear the directory
ls | grep -xv ".ddev" | xargs rm -rf

# see the result for:
ddev composer create --repository $repo test/ddev-composer-create --no-scripts --no-dev

# clear the directory
ls | grep -xv ".ddev" | xargs rm -rf

And so on.


  1. Try CakePHP quickstart (without interaction), it should not ask for input:
mkdir my-cakephp-site && cd my-cakephp-site
ddev config --project-type=cakephp --docroot=webroot
ddev composer create --prefer-dist --no-interaction cakephp/app:~5.0

  1. Try Steps To Reproduce example (it should not ask for input) from:

Automated Testing Overview

Release/Deployment Notes

@stasadev stasadev requested review from a team as code owners June 12, 2024 13:38
@stasadev stasadev requested review from rfay and tyler36 June 12, 2024 13:38
Copy link

github-actions bot commented Jun 12, 2024

Copy link
Member

@rfay rfay left a comment

Choose a reason for hiding this comment

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

Could you please add test coverage for this? Thanks! We have been fiddling with ddev composer create for years and years now, and we keep incrementally improving it, but we never seem to completely get it finished.

@stasadev
Copy link
Member Author

Yes, I will add some new test or modify an existing one.

@stasadev
Copy link
Member Author

I added more conditions and checks to the test, now it covers most possible situations.

@stasadev stasadev requested a review from rfay June 12, 2024 23:24
@stasadev stasadev changed the title fix: pass all valid options to nested commands in composer-create, fixes #6300 fix: pass all valid options to nested commands in composer-create, fixes #6300, for #5058 Jun 12, 2024
@tyler36
Copy link
Collaborator

tyler36 commented Jun 13, 2024

Confirmed. This PR fixes #6300 . Thank you for the quick turn around.

Test

  1. Confirm DDEV version
$ ddev -v
ddev version v1.23.1-62-g4f748c85a
  1. Follow (updated) cakePHP quickstart.
mkdir my-cakephp-site && cd my-cakephp-site
ddev config --project-type=cakephp --docroot=webroot
ddev composer create --prefer-dist --no-interaction cakephp/app:~5.0
ddev cake
ddev launch

Note

This PR updates the quickstart to include --no-interaction . This makes no difference in DDEV 1.23.1 (see #6300 )

@stasadev
Copy link
Member Author

There is still some difference how composer scripts work in:

  • ddev composer create --no-install runs only post-root-package-install and nothing else
  • composer create-project --no-install runs post-root-package-install and post-create-project-cmd

I will investigate this further today and adjust the behavior.

@stasadev stasadev force-pushed the 20240612_stasadev_composer_create branch from 4f748c8 to a5635db Compare June 13, 2024 21:59
@stasadev
Copy link
Member Author

--preserve-flags (which doesn't add --no-plugins and --no-scripts) didn't work as expected:

The order was:

  • composer create-project
  • composer run-script post-root-package-install
  • composer run-script post-create-project-cmd
  • composer install

The right order:

  • composer create-project
  • composer run-script post-root-package-install
  • composer install
  • composer run-script post-create-project-cmd

This is why I made --preserve-flags not add only --no-plugins

This is not a breaking change, because --preserve-flags didn't work before (it was always false).


I moved composer run-script post-create-project-cmd outside the condition for composer install, to make it work the same way as in composer create-project.


If you passed --no-scripts, DDEV tried to run composer run-script, which was wrong behavior, fixed it.


If --help or --version flags were provided, composer showed it's help page or it's version, and DDEV tried to continue creating the project, which was wrong behavior, fixed it.


I refactored the test logic to make it more clear and added more tests. I removed the testing for the psr/log package and replaced it with custom-made fake packages.

@stasadev stasadev changed the title fix: pass all valid options to nested commands in composer-create, fixes #6300, for #5058 fix: pass all valid options to nested commands in composer-create, fixes #6300, fixes #6246, for #5058 Jun 13, 2024
@stasadev stasadev requested a review from mandrasch June 13, 2024 22:09
@stasadev stasadev changed the title fix: pass all valid options to nested commands in composer-create, fixes #6300, fixes #6246, for #5058 fix: pass all valid options to nested commands in ddev composer create, fixes #6300, fixes #6246, for #5058 Jun 13, 2024
@stasadev
Copy link
Member Author

When I split combined short flags, it broke -vvv functionality.
It turned out that -v -v -v is not the same as -vvv, so I will see how to fix it.

@stasadev stasadev force-pushed the 20240612_stasadev_composer_create branch from a5635db to b4ae1d4 Compare June 14, 2024 17:06
@stasadev
Copy link
Member Author

stasadev commented Jun 14, 2024

Rebased.

I fixed -vvv and changed a bit the logic for flags.

I updated our documentation with a more detailed explanation of what steps we do with ddev composer create.

I researched the Composer documentation, learned more about what --no-plugins, --no-scripts and --no-install flags do. In result I decided to remove --preserve-flags completely, because if we do not add --no-plugins or --no-scripts or --no-install to composer create-project, it breaks the original logic, so I don't think we ever needed it.

--preserve-flags didn't work before this PR, so this removal is not a breaking change. And with with the addition of "bad" flags removal, nobody will ever notice it was removed.


I'm pretty sure the most of the original logic here works correctly.

There are some conflicts with the composer create-project flags, such as --ask and --working-dir, using which results in error for ddev composer create, but I decided not to hard-code them, as the composer may add/remove some flags in the future, and I don't want to add unnecessary checks.

@stasadev stasadev force-pushed the 20240612_stasadev_composer_create branch from b4ae1d4 to 50f7a91 Compare June 14, 2024 17:35
Copy link
Member

@rfay rfay left a comment

Choose a reason for hiding this comment

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

I just looked through the docs, not code, had minor comments.

@@ -40,7 +40,14 @@ For example:

When using `ddev composer create` your project should be essentially empty or the command will refuse to run, to avoid loss of your files.

By default `--no-plugins` and `--no-scripts` options are used while cloning the project and `composer install` is executed afterwards as long as `--no-install` is not provided. When using `--preserve-flags` plugins and scripts are allowed during the cloning step.
Standard order of operations for `ddev composer create`:
Copy link
Member

Choose a reason for hiding this comment

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

I sure like this section!

Suggested change
Standard order of operations for `ddev composer create`:
`ddev composer create` does these things in this order:

Standard order of operations for `ddev composer create`:

1. `composer create-project --no-plugins --no-scripts --no-install` clones a bare Composer package without any additional actions.
2. `composer run-script post-root-package-install` runs if `--no-scripts` is not provided manually.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
2. `composer run-script post-root-package-install` runs if `--no-scripts` is not provided manually.
2. `composer run-script post-root-package-install` runs if `--no-scripts` is not given as a flag to `ddev composer create`.


1. `composer create-project --no-plugins --no-scripts --no-install` clones a bare Composer package without any additional actions.
2. `composer run-script post-root-package-install` runs if `--no-scripts` is not provided manually.
3. `composer install` runs if `--no-install` is not provided manually.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
3. `composer install` runs if `--no-install` is not provided manually.
3. `composer install` runs if `--no-install` is not given as a flag to `ddev composer create`.

1. `composer create-project --no-plugins --no-scripts --no-install` clones a bare Composer package without any additional actions.
2. `composer run-script post-root-package-install` runs if `--no-scripts` is not provided manually.
3. `composer install` runs if `--no-install` is not provided manually.
4. `composer run-script post-create-project-cmd` runs if `--no-scripts` is not provided manually.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
4. `composer run-script post-create-project-cmd` runs if `--no-scripts` is not provided manually.
4. `composer run-script post-create-project-cmd` runs if `--no-scripts` is not given as a flag to `ddev composer create`.

@rfay rfay requested a review from rpkoller June 14, 2024 19:30
@rfay
Copy link
Member

rfay commented Jun 14, 2024

@rpkoller would you be willing to validate the quickstarts with this PR? I know it's loads of work, but your attention is so valuable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants