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 vs POSIX #406

Closed
Boegie opened this issue May 12, 2022 · 4 comments · Fixed by #417
Closed

Windows vs POSIX #406

Boegie opened this issue May 12, 2022 · 4 comments · Fixed by #417

Comments

@Boegie
Copy link
Contributor

Boegie commented May 12, 2022

When creating a new baseline on a Windows OS machine using vendor/bin/phpstan analyze --configuration=core/phpstan.neon.dist --generate-baseline ./core/phpstan-baseline.neon on drupal/core with (at least) 1.1.16 I get this suppression:

		-
			message: "#^File core/modules/system/tests/modules/entity_test\\\\entity_test\\.inc could not be loaded from Drupal\\\\Core\\\\Extension\\\\ModuleHandlerInterface\\:\\:loadInclude$#"
			count: 2
			path: modules/system/tests/modules/entity_test/entity_test.install

on a POSIX machine I get:

		-
			message: "#^File core/modules/system/tests/modules/entity_test/entity_test\\.inc could not be loaded from Drupal\\\\Core\\\\Extension\\\\ModuleHandlerInterface\\:\\:loadInclude$#"
			count: 2
			path: modules/system/tests/modules/entity_test/entity_test.install

This leads to sad TestBots and sad users.

@mglaman
Copy link
Owner

mglaman commented May 12, 2022

Hm. I'm not sure what to do here: https://github.com/mglaman/phpstan-drupal/blob/main/src/Rules/Drupal/ModuleLoadInclude.php#L40

        try {
            // Try to invoke it similarly as the module handler itself.
            [$moduleName, $filename] = $this->parseLoadIncludeArgs($args[1], $args[0], $args[2] ?? null, $scope);
            $module = $this->extensionMap->getModule($moduleName);
            if ($module === null) {

Which is from

    protected function parseLoadIncludeArgs(Node\Arg $module, Node\Arg $type, ?Node\Arg $name, Scope $scope): array
    {
        $moduleName = $this->getStringArgValue($module->value, $scope);
        if ($moduleName === null) {
            return [];
        }
        $fileType = $this->getStringArgValue($type->value, $scope);
        if ($fileType === null) {
            return [];
        }
        $baseName = null;
        if ($name !== null) {
            $baseName = $this->getStringArgValue($name->value, $scope);
        }
        if ($baseName === null) {
            $baseName = $moduleName;
        }

        return [$moduleName, "$baseName.$fileType"];
    }

@Boegie
Copy link
Contributor Author

Boegie commented May 13, 2022

@mglaman I think for this case we're looking at https://github.com/mglaman/phpstan-drupal/blob/main/src/Rules/Drupal/LoadIncludes.php#L73, but the principle seems to be the same.

For reasons, I as a mere mortal don't understand, on Windows OS, the dirname() in in $module->getPath() (See

return dirname($this->pathname);
) returns a path with / as directory separator (hence the core/modules/system/tests/modules/entity_test-bit in the message), but the actual DIRECTORY_SEPARATOR in line https://github.com/mglaman/phpstan-drupal/blob/main/src/Rules/Drupal/LoadIncludes.php#L73 always returns a \ (which seems to gets double-escaped and ends up as a \\\\ in the message).
That leaves us with the worst of both worlds.

I would replace the DIRECTORY_SEPARATOR with an actual /, and I think that's a pattern that's used more throughout the code (and should be replaced more if my theory is correct).

In fact I see that you're already doing so in lines like this

return $this->getPath() . '/' . $this->filename;
and
include_once $this->root . '/' . $this->getPath() . '/' . $this->filename;
for example.

Does that make any sense?

@mglaman
Copy link
Owner

mglaman commented May 26, 2022

@Boegie makes sense! How does #417 look? PHP does some fancy fixes of mixed / or \ and DIRECTORY_SEPARATOR. For the fix, I just made sure we do not use DIRECTORY_SEPARATOR in the error message.

@Boegie
Copy link
Contributor Author

Boegie commented May 28, 2022

@mglaman Works for me :)

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.

2 participants