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

Update documentation to help users what to do after changing 'Hello World' #215

Open
1 task done
Luke1982 opened this issue Mar 17, 2023 · 9 comments
Open
1 task done

Comments

@Luke1982
Copy link

Luke1982 commented Mar 17, 2023

Feature Request

Describe your use case and the problem you are facing

I spent quite a lot of time figuring out why my adapted Hello World scaffolded package command wouldn't run, until I found out I had to update the package to make sure the autoload files in the main composer package were regenerated. Maybe it's because of my lack of composer knowledge, but the docs could have told me that, would have been nice.

@danielbachhuber
Copy link
Member

@Luke1982 Seems reasonable to me! Want to submit a PR with your suggested change?

@Luke1982
Copy link
Author

@danielbachhuber I am willing, but afraid I don't have sufficient knowledge to describe the process correctly. I had a lot of hit 'n misses before I solved it and can't remember correctly how I did it.

@danielbachhuber
Copy link
Member

@Luke1982 Ok, no worries. It would be great if someone attempted this process, and then documented what they need to do to resolve.

@ozamorowski
Copy link

I have the same problem as @Luke1982 a while ago, but I ran wp plugin update and now everything is working. Is this the correct method to do that?

@danielbachhuber
Copy link
Member

Hm, I wouldn't expect wp plugin update to have an impact on this.

@ozamorowski
Copy link

Lol, I did wp package update not wp plugin update. Sorry!

@danielbachhuber
Copy link
Member

Ah. wp package update would reset the autoloader, so yes: that would fix the reported problem.

@billbellon
Copy link

billbellon commented Jul 11, 2024

I'm running into a similar situation. I created a project and it works (i.e., wp hello-world runs successfully). However I want to rename files, classes, namespaces ... so that they don't use "hello world" - I want to use names that make sense for the custom command I want to create. I don't see anything in the documentation on how to do this.

Here's how to reproduce my problem:

  1. use wp scaffold package myusername/mypackage to create a package
  2. run wp hello-world in a WordPress installation - it runs successfully
  3. cd .wp-cli/packages/local/myusername/mypackage/
  4. rename hello-world-command.php to hello-world-command-debug.php as a test to see if I can rename this file
  5. edit .wp-cli.yml to require the new filename (i.e., on line 2 change to: - hello-world-command-debug.php)
  6. edit composer.json to change hello-world-command.php to hello-world-command-debug.php in the autoload section
  7. run wp hello-world in a WordPress installation again and you get the following errors:
PHP Warning:  require(/home/myusername/.wp-cli/packages/vendor/composer/../myusername/mypackage/hello-world-command.php): Failed to open stream: No such file or directory in /home/myusername/.wp-cli/packages/vendor/composer/autoload_real.php on line 78
PHP Fatal error:  Uncaught Error: Failed opening required '/home/myusername/.wp-cli/packages/vendor/composer/../myusername/mypackage/hello-world-command.php' (include_path='.:/usr/share/pear:/usr/share/php') in /home/myusername/.wp-cli/packages/vendor/composer/autoload_real.php:78
Stack trace:
#0 /home/myusername/.wp-cli/packages/vendor/composer/autoload_real.php(61): composerRequire9c3b4f1402cbe43de8bcebbc523f4050()
#1 /home/myusername/.wp-cli/packages/vendor/autoload.php(7): ComposerAutoloaderInit9c3b4f1402cbe43de8bcebbc523f4050::getLoader()
#2 phar:///home/myusername/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/AutoloaderStep.php(52): require('...')
#3 phar:///home/myusername/bin/wp/vendor/wp-cli/wp-cli/php/bootstrap.php(78): WP_CLI\Bootstrap\AutoloaderStep->process()
#4 phar:///home/myusername/bin/wp/vendor/wp-cli/wp-cli/php/wp-cli.php(32): WP_CLI\bootstrap()
#5 phar:///home/myusername/bin/wp/php/boot-phar.php(11): include('...')
#6 /home/myusername/bin/wp(4): include('...')
#7 {main}
  thrown in /home/myusername/.wp-cli/packages/vendor/composer/autoload_real.php on line 78

After I got this error I then tried

wp package update

and re-ran the wp hello-world command and and it still didn't work, with the following errors:

PHP Warning:  require(/home/myusername/.wp-cli/packages/vendor/composer/../myusername/mypackage/hello-world-command.php): Failed to open stream: No such file or directory in /home/myusername/.wp-cli/packages/vendor/composer/autoload_real.php on line 78
PHP Fatal error:  Uncaught Error: Failed opening required '/home/myusername/.wp-cli/packages/vendor/composer/../myusername/mypackage/hello-world-command.php' (include_path='.:/usr/share/pear:/usr/share/php') in /home/myusername/.wp-cli/packages/vendor/composer/autoload_real.php:78
Stack trace:
#0 /home/myusername/.wp-cli/packages/vendor/composer/autoload_real.php(61): composerRequire9c3b4f1402cbe43de8bcebbc523f4050()
#1 /home/myusername/.wp-cli/packages/vendor/autoload.php(7): ComposerAutoloaderInit9c3b4f1402cbe43de8bcebbc523f4050::getLoader()
#2 phar:///home/myusername/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/AutoloaderStep.php(52): require('...')
#3 phar:///home/myusername/bin/wp/vendor/wp-cli/wp-cli/php/bootstrap.php(78): WP_CLI\Bootstrap\AutoloaderStep->process()
#4 phar:///home/myusername/bin/wp/vendor/wp-cli/wp-cli/php/wp-cli.php(32): WP_CLI\bootstrap()
#5 phar:///home/myusername/bin/wp/php/boot-phar.php(11): include('...')
#6 /home/myusername/bin/wp(4): include('...')
#7 {main}
  thrown in /home/myusername/.wp-cli/packages/vendor/composer/autoload_real.php on line 78

@BrianHenryIE
Copy link
Member

BrianHenryIE commented Jul 12, 2024

With Composer's autoloading, you can use a psr-4 key which will use the PHP namespace and classname to determine the path on the filesystem to the class. You also have the options of classmap and files keys which are used when generating the autoloader, i.e. as more files are added, they are not known by the autoloader until it is regenerated.

As you're developing you'll need to run:

composer dump-autoload --working-dir=$(wp package path)

I think wp package update did not work for you because there was nothing actually updated, so it didn't rebuild the autoloader.

https://getcomposer.org/doc/01-basic-usage.md#autoloading

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

No branches or pull requests

5 participants