Skip to content

Conversation

@stevegrunwell
Copy link
Member

Since Composer 2.2.6 (the latest version), Composer exposes the COMPOSER_RUNTIME_BIN_DIR environment variable when running as a vendor script.

However, there are a few conditions under which this variable may not be available:

  • When running binaries defined by the root package
  • When running on Composer < 2.2.6
  • When executing the script outside of a Composer script context

This PR replaces the check for COMPOSER_BIN_DIR with COMPOSER_RUNTIME_BIN_DIR and adds a fallback that uses composer config --absolute bin-dir; if we're able to determine a project's Composer bin-dir, we'll strip this off the front of a user's $PATH.

(Re-)fixes #2.

[Since Composer 2.2.6](https://getcomposer.org/doc/articles/vendor-binaries.md#finding-the-composer-bin-dir-from-a-binary) (the latest version), Composer exposes the `COMPOSER_RUNTIME_BIN_DIR` environment variable when running as a vendor script.

However, there are a few conditions under which this variable may not be available:

* When running binaries defined by the root package
* When running on Composer < 2.2.6
* When executing the script outside of a Composer script context

This commit replaces the check for `COMPOSER_BIN_DIR` with `COMPOSER_RUNTIME_BIN_DIR` and adds a fallback that uses `composer config --absolute bin-dir`; if we're able to determine a project's Composer bin-dir, we'll strip this off the front of a user's `$PATH`.

(Re-)fixes #2.
@stevegrunwell stevegrunwell added the bug Something isn't working label Feb 9, 2022
@stevegrunwell
Copy link
Member Author

Pulled and verified this branch into a local project that uses the following Composer script:

"test": [
    "vendor/bin/shellcheck --debug bin/*"
]

Output

Composer 2.2.5

$ composer --version
Composer version 2.2.5 2022-01-21 17:25:52
$ composer test
> vendor/bin/shellcheck --debug bin/*
[DEBUG] COMPOSER_RUNTIME_BIN_DIR not found, checking Composer configuration
[DEBUG] Stripping '/Users/steve/Code/my-project/vendor/bin' from $PATH
[DEBUG] Using /usr/local/bin/shellcheck
[DEBUG] Running `shellcheck bin/my-script`

Composer 2.2.6

Note: The COMPOSER_RUNTIME_BIN_DIR gets written to the vendor/bin/shellcheck file, so I had to wipe out that file after upgrading Composer in order to have it included.

$ composer --version
Composer version 2.2.6 2022-02-04 17:00:38
$ composer test
> vendor/bin/shellcheck --debug bin/*
[DEBUG] Stripping '/Users/steve/Code/my-project/vendor/bin' from $PATH
[DEBUG] Using /usr/local/bin/shellcheck
[DEBUG] Running `shellcheck bin/my-script`

@stevegrunwell stevegrunwell merged commit 22226d2 into develop Feb 9, 2022
@stevegrunwell stevegrunwell deleted the fix/composer-path branch February 9, 2022 15:40
@stevegrunwell stevegrunwell mentioned this pull request Feb 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Recursion when calling ShellCheck via Composer script

2 participants