Skip to content

Lint-staged fails if composer not installed in build-cs #961

@westonruter

Description

@westonruter

Bug Description

I just tried merging trunk into feature/image-loading-optimization. After I resolved conflicts, I attempted to commit but the pre-commit check fails:

✔ Preparing lint-staged...
❯ Running tasks for staged files...
  ❯ package.json — 32 files
    ❯ *.php — 16 files
      ✖ composer run-script lint [FAILED]
      ◼ composer run-script phpstan
    ❯ *.js — 1 file
      ✖ npm run lint-js
↓ Skipped because of errors from tasks. [SKIPPED]
✔ Reverting to original state because of errors...
✔ Cleaning up temporary files...

✖ composer run-script lint:
> composer --working-dir=build-cs update --no-interaction 
...
Cannot update only a partial set of packages without a lock file present. Run `composer update` to generate a lock file.
Script composer --working-dir=build-cs update --no-interaction handling the lint event returned with error code 3
✔ Preparing lint-staged...
❯ Running tasks for staged files...
  ❯ package.json — 32 files
    ❯ *.php — 16 files
      ✖ composer run-script lint [FAILED]
      ◼ composer run-script phpstan
    ❯ *.js — 1 file
      ✖ npm run lint-js [KILLED]
↓ Skipped because of errors from tasks. [SKIPPED]
✔ Reverting to original state because of errors...
✔ Cleaning up temporary files...
husky - pre-commit hook exited with code 1 (error)

PHPCS apparently this is because the build-cs directory has never had composer install run on it before.

If I modify composer.json as follows to composer install instead of composer update:

--- a/composer.json
+++ b/composer.json
@@ -21,15 +21,15 @@
   },
   "scripts": {
     "phpstan": [
-      "composer --working-dir=build-cs update --no-interaction",
+      "composer --working-dir=build-cs install --no-interaction",
       "build-cs/vendor/bin/phpstan analyse --memory-limit=2048M -c phpstan.neon.dist"
     ],
     "format": [
-      "composer --working-dir=build-cs update --no-interaction",
+      "composer --working-dir=build-cs install --no-interaction",
       "build-cs/vendor/bin/phpcbf --standard=phpcs.xml.dist --report-summary --report-source"
     ],
     "lint": [
-      "composer --working-dir=build-cs update --no-interaction",
+      "composer --working-dir=build-cs install --no-interaction",
       "build-cs/vendor/bin/phpcs --standard=phpcs.xml.dist"
     ],
     "test": "phpunit -c phpunit.xml.dist --verbose",

I then get a different error:

✔ Preparing lint-staged...
❯ Running tasks for staged files...
  ❯ package.json — 1 file
    ❯ *.php — 1 file
      ✖ composer run-script lint [FAILED]
      ◼ composer run-script phpstan
    ↓ *.js — no files [SKIPPED]
↓ Skipped because of errors from tasks. [SKIPPED]
✔ Reverting to original state because of errors...
✔ Cleaning up temporary files...

✖ composer run-script lint:
> composer --working-dir=build-cs install --no-interaction '/home/westonruter/repos/performance/load.php'
Invalid argument /home/westonruter/repos/performance/load.php. Use "composer require /home/westonruter/repos/performance/load.php" instead to add packages to your composer.json.
Script composer --working-dir=build-cs install --no-interaction handling the lint event returned with error code 1
husky - pre-commit hook exited with code 1 (error)

This is apparently because lint-staged is passing the modified file (here /home/westonruter/repos/performance/load.php) to both subcommands of the lint command:

    "lint": [
      "composer --working-dir=build-cs install --no-interaction",
      "build-cs/vendor/bin/phpcs --standard=phpcs.xml.dist"
    ],

I suggest that we not run composer update in the subcommands for lint, format, and phpstan because (1) the lock file may not be present so it will fail and (2) it slows down running those commands. Instead, I suggest that we do composer install and composer update for the build-cs directory via top-level post-install-cmd and post-update-cmd scripts, respectively.

Steps to reproduce

  1. Modify a PHP file and stage the change.
  2. Delete the build-cs/composer.lock file and build-cs/vendor directory (if they even exist yet)
  3. Try to commit the change to the PHP file.

Metadata

Metadata

Assignees

Labels

[Type] BugAn existing feature is broken

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions