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

Windows support #180

Closed
lehoa85 opened this issue Jul 1, 2020 · 9 comments · Fixed by #192
Closed

Windows support #180

lehoa85 opened this issue Jul 1, 2020 · 9 comments · Fixed by #192

Comments

@lehoa85
Copy link

lehoa85 commented Jul 1, 2020

How is drupal-check installed?

drupal-check is installed globally via Composer

Environment:

  • OS: Windows
  • PHP Version: 7.4
  • Drupal core: 9.x.x

Describe the bug
I move to G:\xampp\htdocs\myDrupal and run command: drupal-check -a modules

Console output
Using Drupal root: G:\xampp\htdocs\myDrupal
Using vendor root: G:\xampp\htdocs\myDrupal/vendor
PHPStan configuration:
parameters:
tipsOfTheDay: false
reportUnmatchedIgnoredErrors: false
excludes_analyse:
- /tests/Drupal/Tests/Listeners/Legacy/
- /tests/fixtures/.php
- /settings.php

    drupal:
            drupal_root: G:\xampp\htdocs\myDrupal

    level: 4
    bootstrap: C:\Users\Chris\AppData\Roaming\Composer\vendor\mglaman\drupal-check\src\Command/../../error-bootstrap.php

includes:
- C:\Users\Chris\AppData\Roaming\Composer\vendor\mglaman\drupal-check\src\Command/../../../../phpstan/phpstan-deprecation-rules/rules.neon
- C:\Users\Chris\AppData\Roaming\Composer\vendor\mglaman\drupal-check\src\Command/../../../../mglaman/phpstan-drupal/extension.neon

'"C:\Users\Chris\AppData\Roaming\Composer\vendor\mglaman\drupal-check\src\Command/../../../../phpstan/phpstan/phpstan"' is not recognized as an internal or external command,
operable program or batch file.

@flyke
Copy link

flyke commented Jul 2, 2020

I have a very similar error. Windows 10. drupal-check is globally installed using composer. In my drupal project directory (or anywhere else on my whole computer for that matters) if I try any drupal check command:
drupal-check -a web/modules/custom
results in:

'"C:\Users\myusername\AppData\Roaming\Composer\vendor\mglaman\drupal-check\src\Command/../../../../phpstan/phpstan/phpstan"' is not recognized as an internal or external command,
operable program or batch file.

Error is the same if I try drupal-check web/modules/custom or drupal-check web
If I do not enter required second (path) parameter, then I know drupal-check does something:
Just typing drupal-check gives expected error:
Not enough arguments (missing: "path").

So, is it an autoload problem or something ?
However, I checked where exactly phpstan is loaded and that is in C:\Users\myusername\AppData\Roaming\Composer\vendor\mglaman\drupal-check\src\Command\CheckCommand.php
In this part:

        if ($pharPath !== '') {
            // Running in packaged Phar archive.
            $phpstanBin = 'vendor/phpstan/phpstan/phpstan';
            $configuration_data['parameters']['bootstrap'] = $pharPath . '/error-bootstrap.php';
            $configuration_data['includes'] = [
                $pharPath . '/vendor/phpstan/phpstan-deprecation-rules/rules.neon',
                $pharPath . '/vendor/mglaman/phpstan-drupal/extension.neon',
            ];
        } elseif (file_exists(__DIR__ . '/../../vendor/autoload.php')) {
            // Running as a project dependency.
            $phpstanBin = __DIR__ . '/../../vendor/phpstan/phpstan/phpstan';
            $configuration_data['parameters']['bootstrap'] = __DIR__ . '/../../error-bootstrap.php';
            $configuration_data['includes'] = [
                __DIR__ . '/../../vendor/phpstan/phpstan-deprecation-rules/rules.neon',
                __DIR__ . '/../../vendor/mglaman/phpstan-drupal/extension.neon',
            ];
        } elseif (file_exists(__DIR__ . '/../../../../autoload.php')) {
            // Running as a global dependency.
            $phpstanBin = __DIR__ . '/../../../../phpstan/phpstan/phpstan';
            $configuration_data['parameters']['bootstrap'] = __DIR__ . '/../../error-bootstrap.php';
            // The phpstan/extension-installer doesn't seem to register.
            $configuration_data['includes'] = [
                __DIR__ . '/../../../../phpstan/phpstan-deprecation-rules/rules.neon',
                __DIR__ . '/../../../../mglaman/phpstan-drupal/extension.neon',
            ];
        } else {
            throw new ShouldNotHappenException('Could not determine if local or global installation');
        }

And I can see that it gets correct path at the // Running as a global dependency. section because if I change anything there in the $phpstanBin variable, like removing a '../' or something, then my drupal-check command results in:
The system cannot execute the specified program.

@flyke
Copy link

flyke commented Jul 2, 2020

Here is what did seem to work in my case. In C:\Users\myusername\AppData\Roaming\Composer\vendor\mglaman\drupal-check\src\Command\CheckCommand.php
change
$phpstanBin = __DIR__ . '/../../../../phpstan/phpstan/phpstan';
to
$phpstanBin = 'C:\Users\myusername\AppData\Roaming\Composer\vendor\bin\phpstan.bat';

@mglaman mglaman changed the title Error when run drupal-check with global composer Windows support Jul 29, 2020
@mglaman
Copy link
Owner

mglaman commented Jul 29, 2020

So this looks like we need to support Windows.

This probably needs to be wrapped in realpath: $phpstanBin = __DIR__ . '/../../../../phpstan/phpstan/phpstan';

We also need to detect if Windows and use the .bat.

@flyke
Copy link

flyke commented Aug 5, 2020

Great, let's do that :-)

@mglaman
Copy link
Owner

mglaman commented Oct 9, 2020

It's close.

I just don't know why this errors

php drupal-check %APPVEYOR_BUILD_FOLDER%\..\drupal\web\core\install.php
The filename, directory name, or volume label syntax is incorrect.

@mglaman
Copy link
Owner

mglaman commented Oct 12, 2020

I released https://github.com/mglaman/drupal-check/releases/tag/1.1.4, which calls PHPStan properly. But there's still an error that I can't quite figure out. A Windows user debugging would be helpful

@DanChadwick
Copy link

Re
The filename, directory name, or volume label syntax is incorrect.

I installed drupal-check on windows 10 locally into my drupal directory using composer. I experienced the above issue.

The problem is caused in DrupalCheck\Command\CheckCommand::execute():

        if (substr(PHP_OS, 0, 3) == 'WIN') {
            $phpstanBin = "php $phpstanBin";
        }

        [SNIP]

        $command = [
            $phpstanBin,
            'analyse',
            '-c',
            $configuration,
            '--error-format=' . $input->getOption('format')
        ];

Later in Symfony\Component\Process\Process::escapeArgument, the command is escaped because it contains a backslash. But the first argument contains php, which leads to literally (including quotes), something like
"php path\to\PHPStan.phar" path\to\modules -option1 -option2

So my fix is to change the above code to:

        // if (substr(PHP_OS, 0, 3) == 'WIN') {
        //     $phpstanBin = "php $phpstanBin";
        // }

        [SNIP]

        $command = [
            $phpstanBin,
            'analyse',
            '-c',
            $configuration,
            '--error-format=' . $input->getOption('format')
        ];

        if (substr(PHP_OS, 0, 3) == 'WIN') {
            array_unshift($command, 'php');
        }

After making this change, drupal-check works as expected on Windows, at least for me.

@mglaman
Copy link
Owner

mglaman commented Oct 19, 2020

🤦 so my fix of appending php to $phpstanBin was wrong, it needed to be prepended to the array. OOOF.

Thank you @DanChadwick so much, I'm going to make a PR for this! 🙏

@mglaman
Copy link
Owner

mglaman commented Oct 19, 2020

😱 Windows is green! Tagging a release

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

Successfully merging a pull request may close this issue.

4 participants