apache / apache/cordova-lib

platform-specific after_prepare hooks don't run after restoring

Open
#940 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
231
Forks
242
Avg merge
2h 51m
Merged PRs (30d)
7

Description

# Bug Report

## Problem
Include `cordova-plugin-androidx-adapter` in package.json, along with `cordova-ios` and `cordova-android`.

With no platforms or plugins directories, run `cordova prepare`.

### What is expected to happen?
The `after_prepare` hook of `cordova-plugin-androidx-adapter` should be invoked at the end of the prepare, because the Android platform and the plugin have just been restored.

### What does actually happen?
The `after_prepare` hook never fires, cordova-lib verbose output indicates that no hook scripts were found.

## Information

What happens is that the ScriptsFinder tries to look up scripts in plugins for the provided list of platforms:
https://github.com/apache/cordova-lib/blob/db5c8c014e917004108a440563b1aad7fc6b3140/src/hooks/scriptsFinder.js#L87

That list of platforms comes from the options of the HooksRunner:
https://github.com/apache/cordova-lib/blob/db5c8c014e917004108a440563b1aad7fc6b3140/src/hooks/scriptsFinder.js#L114

The HooksRunner sets its list of platforms to a provided option, or falls back to querying for the installed platforms:
https://github.com/apache/cordova-lib/blob/db5c8c014e917004108a440563b1aad7fc6b3140/src/hooks/HooksRunner.js#L68-L69

In the case of `prepare`, if a platform is not manually specified, we set that list of platforms to an empty array:
https://github.com/apache/cordova-lib/blob/db5c8c014e917004108a440563b1aad7fc6b3140/src/cordova/prepare.js#L32

The end result is that after restoring all the platforms and plugins, the HooksRunner still tries to find hook scripts with an empty platforms list, and the Android-specific hook never runs.

### Suggested fix

HooksRunner should maybe check if the platforms array is empty and then still fallback to querying the installed platforms, rather than taking the empty input and running with it.

### Version information
Was seeing this in an older project using Cordova CLI 10, but having looked at the code it seems like it's probably still an issue today.

## Checklist

- [x] I searched for existing GitHub issues
- [x] I updated all Cordova tooling to most recent version
- [x] I included all the necessary information above

Contributor guide

Open the contributing guide

Research direction

Start with src/cordova/prepare.js, src/hooks/HooksRunner.js, and src/hooks/scriptsFinder.js, then reproduce the issue by running cordova prepare with no platforms or plugins directories. Trace how the platforms list reaches hook discovery; done means the platform-specific after_prepare hook runs after the Android platform and plugin are restored.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.