ionic-team / capacitor

Build cross-platform Native Progressive Web Apps for iOS, Android, and the Web ⚡️
https://capacitorjs.com
MIT License
12.21k stars 1k forks source link

bug: capacitor commands (sync/add) not working for non default platforms #6958

Open richardliebmann opened 1 year ago

richardliebmann commented 1 year ago

Bug Report

Capacitor Version

 Capacitor Doctor   

Latest Dependencies:

  @capacitor/cli: 5.4.2
  @capacitor/core: 5.4.2
  @capacitor/android: 5.4.2
  @capacitor/ios: 5.4.2

Installed Dependencies:

  @capacitor/android: not installed
  @capacitor/ios: not installed
  @capacitor/core: 5.4.2
  @capacitor/cli: 5.4.2

Platform(s)

Current Behavior

capacitor commands for non default platforms are not executed

Expected Behavior

commands should be executed

Code Reproduction

Example Code Base: https://github.com/richardliebmann/capacitor-electron-platform-command-problem

  1. create-nx-workspace (angular, integrated-monorepo)
  2. nx build cap-sync-error
  3. cd apps/cap-sync-error
  4. npx cap init
  5. change webDir to '../../dist/apps/cap-sync-error'
  6. npm install @capacitor-community/electron --save
  7. cap add @capacitor-community/electron

The command does nothing

Code cause:

For non default platforms (isValidPlatform) capacitor searches for the platformDir and find it unter node_modules (e.g. "D:\Repos\andi-ui\node_modules\@capacitor-community\electron"). Code: https://github.com/ionic-team/capacitor/blob/e50e56c5231f230497d1bd420e02e2e065c38f86/cli/src/tasks/add.ts#L45-L70

The runPlatformHook checks if we are in a nx monorepo and overrides the package.json path to the project root folder (D:\Repos\andi-u) instead of the platform root folder (D:\Repos\andi-ui\node_modules\@capacitor-community\electron).

https://github.com/ionic-team/capacitor/blob/e50e56c5231f230497d1bd420e02e2e065c38f86/cli/src/common.ts#L165-L200

The required hook (capacitor:add) defined in the script section of the package.json of the platform therefor is not found in the project root package.json.

The code change was done with feat.Better support monorepos #6811

Other Technical Details

npm --version output: 8.17.0

node --version output: v18.16.1

pod --version output (iOS issues only):

Additional Context

richardliebmann commented 1 year ago

Workaround: Add following scripts to the package.json/script section in the root folder:

        "capacitor:add": "node dist/cli-scripts/cap-scripts.js add",
        "capacitor:copy": "node dist/cli-scripts/cap-scripts.js copy",
        "capacitor:update": "node dist/cli-scripts/cap-scripts.js update",
        "capacitor:sync": "node dist/cli-scripts/cap-scripts.js sync",
        "capacitor:open": "node dist/cli-scripts/cap-scripts.js open"
ionitron-bot[bot] commented 1 year ago

This issue needs more information before it can be addressed. In particular, the reporter needs to provide a minimal sample app that demonstrates the issue. If no sample app is provided within 15 days, the issue will be closed. Please see the Contributing Guide for how to create a Sample App. Thanks! Ionitron 💙

richardliebmann commented 1 year ago

Sample app with reproduction added (https://github.com/richardliebmann/capacitor-electron-platform-command-problem)

ionitron-bot[bot] commented 1 year ago

This issue has been labeled as type: bug. This label is added to issues that that have been reproduced and are being tracked in our internal issue tracker.

WolfWalter commented 10 months ago

I ran into the exact same issue.