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

Phar version and no composer autoloader doesn't seem to work #1923

Closed
theseer opened this issue Jul 9, 2019 · 11 comments
Closed

Phar version and no composer autoloader doesn't seem to work #1923

theseer opened this issue Jul 9, 2019 · 11 comments

Comments

@theseer
Copy link

theseer commented Jul 9, 2019

This might be pretty much identical to #935, but I'm not sure it qualifies as a duplicate just yet.

Maybe I'm just doing things wrong.

I'm trying to introduce psalm to phar-io/version. As it's a simple library, it doesn't have any external dependencies but of course can be used as a dependency by others - thus provides a composer.json file.

Yet, we do not use composer for autoloading but provide our own autoloader - in src/autoload.php.

I found the autoloader attribute for psalm.xml but that seems to not be working the way I expected it to - because psalm keeps complaining it cannot find a composer autoloader.

Bug or feature? ;-)

@muglug
Copy link
Collaborator

muglug commented Jul 9, 2019

When I run Psalm in that repo after running composer install it seems to work just fine - am I misunderstanding something?

@theseer
Copy link
Author

theseer commented Jul 9, 2019

Yes, you used composer to install it. :)
When you use composer to install psalm you of course have a composer autoloader.

Try using the phar distribution of psalm.

Side-Rant: Imho, using composer to install tools is fundamentally broken as it mangles tool dependencies with runtime dependencies of the software and creates a bloated autoloader. Tools should always be installed using a self contained phar and, ideally, scoped so no conflicts with tool dependencies and software dependencies can arise.

@muglug
Copy link
Collaborator

muglug commented Jul 9, 2019

Yes, you used composer to install it. :)
When you use composer to install psalm you of course have a composer autoloader.

I didn't, though - I used the Phar.

I ran

git clone https://github.com/phar-io/version
cd version
composer install
~/Downloads/psalm.phar --init
~/Downloads/psalm.phar

and Psalm happily reported results. If it detects a composer.json file, it expects composer install to have been run.

@theseer
Copy link
Author

theseer commented Jul 9, 2019

theseer@nyda /tmp/1923 $ git clone git@github.com:phar-io/version.git

Cloning into 'version'...
remote: Enumerating objects: 260, done.
remote: Total 260 (delta 0), reused 0 (delta 0), pack-reused 260
Receiving objects: 100% (260/260), 39.49 KiB | 421.00 KiB/s, done.
Resolving deltas: 100% (168/168), done.

theseer@nyda /tmp/1923 $ cd version/

theseer@nyda /tmp/1923/version master $ phive install psalm

Phive 0.12.2-dirty - Copyright (C) 2015-2019 by Arne Blankerts, Sebastian Heuer and Contributors
Linking /home/theseer/.phive/phars/psalm.phar to /tmp/1923/version/tools/psalm

theseer@nyda /tmp/1923/version master $ ./tools/psalm
Could not find any composer autoloaders in /tmp/1923/version/
Add a --root=[your/project/directory] flag to specify a particular project to run Psalm on.

@theseer
Copy link
Author

theseer commented Jul 9, 2019

Why would I need to run composer install? There is nothing to install?

@muglug
Copy link
Collaborator

muglug commented Jul 9, 2019

Why would I need to run composer install? There is nothing to install?

You don't, actually - composer dumpautoload is sufficient. Psalm uses the generated files to avoid having to use reflection (which executes some amount of PHP) in the scanning step.

@theseer
Copy link
Author

theseer commented Jul 9, 2019

Can you point me to the code for that? I'm confused :)

@theseer
Copy link
Author

theseer commented Jul 9, 2019

The only code I can find (using ack autoload src) doesn't seem to do anything in particular with the composer generated autoloader except activating it.

What am I missing? :)

@muglug
Copy link
Collaborator

muglug commented Jul 9, 2019

The composer autoload initialisation logic is here: https://github.com/vimeo/psalm/blob/master/src/command_functions.php#L16

@theseer
Copy link
Author

theseer commented Jul 9, 2019

Found that already. I'm still missing something probably trivial ;)

In line 85 the autoload files are merely required.

In case no composer autoloader is in use the init code will return null. Which doesn't seem to be a problem given it's documented as nullable ;)

Given I can (can I?) specify my own autoloader in psalm.xml (<psalm autoloader="src/autoload.php" .... />), the result should be the same: A (short) stack of autoloaders being registered.

There must be something more to it that I just fail to see or find. I found more code dealing with the composer autoloader class in psalm's Config class but then again, if the aforementioned init code is allowed to return null, that can't be a hard requirement. Or can it?

Feel free to close this btw. if you think it's working as intended and we'll discuss the topic in fall at IPC in Munich ;)

@muglug
Copy link
Collaborator

muglug commented Jul 9, 2019

Here's where the specific error you're seeing is generated:

if (!$has_autoloader && file_exists($autoload_root . '/composer.json')) {
$error_message = 'Could not find any composer autoloaders in ' . $autoload_root;
if (!$has_explicit_root) {
$error_message .= PHP_EOL . 'Add a --root=[your/project/directory] flag '
. 'to specify a particular project to run Psalm on.';
}
fwrite(STDERR, $error_message . PHP_EOL);
exit(1);
}

But yes, going to close! See you in Munich!

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

No branches or pull requests

2 participants