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

Autoloader: fix the fatal that occurs during plugin update #16279

Merged
merged 1 commit into from
Jun 25, 2020

Conversation

kbrown9
Copy link
Member

@kbrown9 kbrown9 commented Jun 24, 2020

Fix the fatal that occurs during plugin update. The fatal occurs because, during a plugin update, a function in the just-installed version of autoload_functions.php is called. However, that file had already been loaded, so the just-installed version of the file isn't available.

Fixes #16273

Changes proposed in this Pull Request:

  • Move the upgrader_post_install callback function, reset_maps_after_update, to the autoload_functions.php file.
  • In reset_maps_after_update, call set_up_autoloader to reset the autoloader after a plugin update.

Jetpack product discussion

  • n/a

Does this pull request change what data or activity we track or use?

  • n/a

Testing instructions:

Cause a fatal error during a plugin update
  1. Install the Jetpack beta plugin.
  2. Install and activate the master branch.
  3. Delete the existing jetpack folder and rename the jetpack-dev folder to jetpack.
  4. Decrease the plugin version in the plugin header of the jetpack.php file to < 8.6.1.
  5. Connect Jetpack.
  6. Add this snippet to your site. It does two things:
    • Uses a class from a Jetpack package when the upgrader_process_complete hook fires. Note that the snippet uses the Pre_Connection_JITM class, which is not loaded when Jetpack is connected.
    • Sets the source file for the Jetpack plugin update to wp-content/plugins/jetpack.zip.
add_action( 'upgrader_process_complete', 'load_package_class', 1 );

function load_package_class() {
	$class = new \Automattic\Jetpack\JITMS\Pre_Connection_JITM();
}

add_action( 'upgrader_package_options', 'set_plugin_path' );

function set_plugin_path( $options ) {
	if( 'jetpack/jetpack.php' === $options['hook_extra']['plugin'] ) {
		$options['package'] = WP_PLUGIN_DIR . '/jetpack.zip';
	}
	return $options;
}
  1. Create the source zip file for the Jetpack update using a command like zip -r jetpack.zip jetpack. The file name and location must match the source zip file in the upgrader_package_options hook callback. If you use the snippet in step 5, the source file path must be wp-content/plugins/jetpack.zip.

  2. In jetpack/vendor/composer/jetpack_autoload_classmap.php, change the file name for the Pre_Connection_JITM class.

    • Note that this file is in the original location in the jetpack.zip file. This change allows us to simulate the file being moved.
  3. Change the namespace of the autoloader files. Get the namespace from the vendor/autoload_functions.php file. It will be something like:

    namespace Automattic\Jetpack\Autoloader\jp659a3ca3ee216d6e4c3d3ab24194e47z;

    • You can use something like the command below to change the namespace in all of the generated autoloader files. This should be run from the vendor directory. Change the strings to match your current and changed namespaces.

find . -type f -exec sed -i 's/jp659a3ca3ee216d6e4c3d3ab24194e47z/jp659a3ca3ee216d6e4c3d3ab24194e47a/g' {} +

  1. Navigate to wp-admin -> Plugins. An update should be available for Jetpack. Click update now.

  2. The update should fail and a fatal error should be generated in the debug log because the set_up_autoloader() function in the newly installed namespace can't be found. The error should look like:

[24-Jun-2020 15:00:11 UTC] PHP Fatal error: Uncaught Error: Call to undefined function Automattic\Jetpack\Autoloader\jpc74400305ae47f17074763a083b7f93f\set_up_autoloader() in wp-content/plugins/jetpack-dev/vendor/autoload_packages.php:14

Test the fix
  1. Install this branch.
  2. Use the same steps as above, and the update should complete successfully.

Proposed changelog entry for your changes:

  • n/a

Fix the fatal that occurs during plugin update. The fatal occurs because a function in
the updated version of autoload_functions.php was called. However, that file had already been
loaded, so the updated version of  the file isn't available.

Instead, put the upgrader_post_install callback function in the autoload_functions.php file
and call set_up_autoloader in the existing file to reset the autoloader after a plugin update.
@kbrown9 kbrown9 added this to the 8.7 milestone Jun 24, 2020
@kbrown9 kbrown9 self-assigned this Jun 24, 2020
@github-actions github-actions bot added the [Status] Needs Package Release This PR made changes to a package. Let's update that package now. label Jun 24, 2020
@jetpackbot
Copy link

Thank you for the great PR description!

When this PR is ready for review, please apply the [Status] Needs Review label. If you are an a11n, please have someone from your team review the code if possible. The Jetpack team will also review this PR and merge it to be included in the next Jetpack release.

E2E results is available here (for debugging purposes): https://jetpack-e2e-dashboard.herokuapp.com/pr-16279

Scheduled Jetpack release: July 7, 2020.
Scheduled code freeze: June 30, 2020

Generated by 🚫 dangerJS against 9ea190b

@kbrown9 kbrown9 added [Status] In Progress [Package] Autoloader [Status] Needs Review To request a review from Crew. Label will be renamed soon. and removed [Status] In Progress labels Jun 24, 2020
Copy link
Member

@dereksmart dereksmart left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Followed the testing instructions, and it worked as described

Though I'm still not 100% that this is exactly the problem the beta update faced before, I'm confident enough to merge it.

@dereksmart dereksmart merged commit 650a54a into master Jun 25, 2020
@dereksmart dereksmart deleted the fix/autoloader_plugin_update_fatal branch June 25, 2020 01:44
@matticbot matticbot added [Status] Needs Changelog and removed [Status] Needs Review To request a review from Crew. Label will be renamed soon. labels Jun 25, 2020
@jeherve jeherve removed the [Status] Needs Package Release This PR made changes to a package. Let's update that package now. label Jul 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Autoloader: Fatal errors on each plugin update
5 participants