capacitor
capacitor copied to clipboard
bug: Invalid `Podfile` file: cannot load such file '../../node_modules/@capacitor/ios/scripts/pods_helpers'
Bug Report
Capacitor Version
💊 Capacitor Doctor 💊
Latest Dependencies:
@capacitor/cli: 4.1.0
@capacitor/core: 4.1.0
@capacitor/android: 4.1.0
@capacitor/ios: 4.1.0
Installed Dependencies:
@capacitor/cli: 4.1.0
@capacitor/core: 4.1.0
@capacitor/android: 4.1.0
@capacitor/ios: 4.1.0
[success] iOS looking great! 👌
[success] Android looking great! 👌
Platform(s)
ios
Current Behavior
When running npx cap sync, I get the following error
[!] Invalid `Podfile` file: cannot load such file --
/Users/marco/Documents/frontend/apps/code/node_modules/@capacitor/ios/scripts/pods_helpers.
# from /Users/marco/Documents/frontend/apps/code/ios/App/Podfile:1
# -------------------------------------------
> require_relative '../../node_modules/@capacitor/ios/scripts/pods_helpers'
#
# -------------------------------------------
The 'pods_helper' path doesn't get resolved to the right path, because of the monorepo the module is four directories up instead of two.
All the other paths are getting resolved to
'../../../../node_modules/@capacitor/ios'.
Expected Behavior
The CLI should resolve the '@capacitor/ios' module and replace the path.
For example in my case it would be:
require_relative '../../../../node_modules/@capacitor/ios/scripts/pods_helpers'
After I changed the path, everything worked fine.
Code Reproduction
I created a yarn monorepo with workspaces: https://github.com/Marcoru97/capacitor-cli-demo
Affected file: https://github.com/Marcoru97/capacitor-cli-demo/blob/main/apps/capacitor-test/ios/App/Podfile
Run npx cap sync
to get the error
Other Technical Details
npm --version
output: 8.1.0
node --version
output: v16.13.0
pod --version
output (iOS issues only): 1.11.2
Additional Context
I saw that you are already resolving the package via 'require.resolve' but only replaced it in the pods list: https://github.com/ionic-team/capacitor/blob/17fbabb2a77d1b356d24048efc5883bd4d049104/cli/src/ios/update.ts#L141
This issue may need more information before it can be addressed. In particular, it will need a reliable Code Reproduction that demonstrates the issue.
Please see the Contributing Guide for how to create a Code Reproduction.
Thanks! Ionitron 💙
I created a reproduction repo: https://github.com/Marcoru97/capacitor-cli-demo and updated the issue
The path is hardcoded in the template, didn't have mono repos into account. As workaround you can manually change the path because it's not being written by Capacitor CLI, so if you change it, it won't be overwritten. We will need to use a regular expression to replace the hardcoded value with the actual value.
I ran into the same issue, and managed to solve it by adding nohoist
to my monorepo's root package.json:
- "workspaces": [
+ "workspaces": {
+ "packages": [
"packages/*"
+ ],
+ "nohoist": [
+ "**/@capacitor/ios"
+ ]
- ],
+ },
I wrote the script which changes deps automatically, pls check it and add it into update.js, this helps many people who can not understand why their project doesn't work
`const { rootDir } = require('../../../rootDir.js');
const lengthRootDir = rootDir.split('/').length; const lengthCurrentDir = __dirname.split('/').length; const relativeDiffLength = lengthCurrentDir - lengthRootDir;
const relativePrefix = getRelativePrefix(relativeDiffLength);
let podFileContent = utils_fs_1.readFileSync(podFilePath, { encoding: 'utf-8' }); podFileContent = podFileContent.replace(/require_relative '(../)+/g, "require_relative '"); podFileContent = podFileContent.replace('node_modules/@capacitor/ios/scripts/pods_helpers', ${relativePrefix}node_modules/@capacitor/ios/scripts/pods_helpers); utils_fs_1.writeFileSync(podFilePath, podFileContent, { encoding: 'utf-8' });
function getRelativePrefix(relativeDiffLength) { let relativePrefix = ''; let count = 0 while(count <= relativeDiffLength) { count++ relativePrefix = relativePrefix + '../' }
return relativePrefix; }`
I also ran into this issue today as I'm trying to add capacitor to a package inside a monorepo. To resolve it I did the following:
- added the nohoist syntax recommended by @ryaninvents here
- ran
npx cap add ios
- still got the failure - open
./ios/App/Podfile
and changerequire_relative '../../node_modules/@capacitor/ios/scripts/pods_helpers'
on line 1 torequire_relative '../../../../node_modules/@capacitor/ios/scripts/pods_helpers'
- ran
npx cap sync
Removing the ios dir to re-run npx cap add ios
means you lose your changes to the Podfile. It took me a while to figure out i could run npx cap sync
to install the missing dependencies.
Any idea on the ETA for fixing this bug?
This might be resolvable by making use of import.meta.url
, though may not be an option for Cap v4.. v5 I'm sure could use that API, though. It may be good to just add a command line argument that would allow for someone to specify the workspace root. Just tossing some ideas around
This bug was logged for Capacitor v4.1.0, and I can confirm that the bug still exists in a monorepo using v5.0.3 (i.e. upgrading to v5 doesn't fix this issue). The workaround posted by @ErisDS works for v5 as well - thanks!
Still persisting, @ErisDS's fix worked.
The relative path to ../../node_modules/@capacitor/ios/scripts/pods_helpers
won't work if you're using Yarn Plug 'n' Play since it won't write a node_modules/
folder to disk at all.
You can force Yarn to write the @capacitor/ios
package to disk by unplugging it: yarn unplug @capacitor/ios
. You can then update the relative paths to point to the correct location under .yarn/unplugged
.
E.g. my Podfile now looks like this:
require_relative '../../.yarn/unplugged/@capacitor-ios-virtual-b6b1ba2888/node_modules/@capacitor/ios/scripts/pods_helpers'
# NOTE: you need to update the hash in the import above to match the version you have installed under .yarn/unplugged
platform :ios, '13.0'
use_frameworks!
def capacitor_pods
pod 'Capacitor', :path => '../../.yarn/unplugged/@capacitor-ios-virtual-b6b1ba2888/node_modules/@capacitor/ios'
# ...
end
This was fixed in Capacitor 5.3.0 in https://github.com/ionic-team/capacitor/pull/6811
It will be slightly changed in 5.3.1 with https://github.com/ionic-team/capacitor/pull/6878
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.