Skip to content

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

Open
@richardliebmann

Description

@richardliebmann

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)

  • @capacitor-community/electron": "^5.0.1"
  • maybe other non default platforms

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:

await runPlatformHook(
config,
selectedPlatformName,
platformDir,
'capacitor:add',
);
} else {
let msg = `Platform ${c.input(selectedPlatformName)} not found.`;
if (await isValidCommunityPlatform(selectedPlatformName)) {
msg += `\nTry installing ${c.strong(
`@capacitor-community/${selectedPlatformName}`,
)} and adding the platform again.`;
}
if (await isValidEnterprisePlatform(selectedPlatformName)) {
msg +=
`\nThis is an enterprise platform and @ionic-enterprise/capacitor-${selectedPlatformName} is not installed.\n` +
`To learn how to use this platform, visit https://ionic.io/docs/${selectedPlatformName}`;
}
logger.error(msg);
}
} else {
const knownPlatforms = await getKnownPlatforms();
const platformName = await promptForPlatform(

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).

capacitor/cli/src/common.ts

Lines 165 to 200 in e50e56c

export async function runPlatformHook(
config: Config,
platformName: string,
platformDir: string,
hook: string,
): Promise<void> {
const { spawn } = await import('child_process');
let pkg;
if (isNXMonorepo(platformDir)) {
pkg = await readJSON(join(findNXMonorepoRoot(platformDir), 'package.json'));
} else {
pkg = await readJSON(join(platformDir, 'package.json'));
}
const cmd = pkg.scripts?.[hook];
if (!cmd) {
return;
}
return new Promise((resolve, reject) => {
const p = spawn(cmd, {
stdio: 'inherit',
shell: true,
cwd: platformDir,
env: {
INIT_CWD: platformDir,
CAPACITOR_ROOT_DIR: config.app.rootDir,
CAPACITOR_WEB_DIR: config.app.webDirAbs,
CAPACITOR_CONFIG: JSON.stringify(config.app.extConfig),
CAPACITOR_PLATFORM_NAME: platformName,
...process.env,
},
});
p.on('close', () => {
resolve();
});

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions