apache / cordova-ios

Apache Cordova iOS
https://cordova.apache.org/
Apache License 2.0
2.15k stars 986 forks source link

Copy deployment-target from root config.xml to platform-specific config.xml before usage in Podfile #1422

Open expcapitaldev opened 2 weeks ago

expcapitaldev commented 2 weeks ago

Issue Currently, the deployment-target variable is not being copied from the root config.xml file to the platforms/ios/[App Name]/config.xml before it is accessed in the code. As a result, the script uses the default deployment target from the template (https://github.com/apache/cordova-ios/blob/rel/7.0.1/templates/project/__PROJECT_NAME__/config.xml) instead of the user-specified value in the root config.xml. It is mean in Podfile we will have deployment-target from root config.xml only first successful prepare hook. Reproduce is very easy: try to install any pod library with ios target version more than 11. -> you will have error

Expected Behavior The deployment-target set in the root config.xml should be reflected in the platforms/ios/[App Name]/config.xml before any script or process accesses it.

Proposed Solution Modify the lib/prepare.js file to ensure that deployment-target and possibly other preferences are copied from the root config.xml to the platforms/ios/[App Name]/config.xml early in the preparation process. This could potentially be implemented at line 250, before the preferences are required by subsequent processes in the script.

Additional Information This change will ensure that the deployment target and other settings are correctly set according to the project's configuration before they are needed, preventing any default settings from causing conflicts or errors during build or runtime.

Workaround:

breautek commented 2 weeks ago

This was an issue in cordova-ios 6.3 but I believe was patched in 7.0 (definitely in 7.1).

I have a google maps plugin that requires iOS 15 and I used to manually workaround this issue but it was corrected after I updated my projects to 7.1.

I used:

<platform name="ios">
  <preference name="deployment-target" value="15.0" />
</platform>

adding the plugin and then the platform works as expected, with the generated podfile properly containing 15.0 instead of the default 11.0. This also works if you add the platform first, or use the preference outside of the <platform name="ios"> block. Updating the preference and running cordova prepare ios also shows the changes reflected in both the platform's Podfile and config.xml.

The deployment-target set in the root config.xml should be reflected in the platforms/ios/[App Name]/config.xml before any script or process accesses it.

The expectation of "any script or process" is not quite right. The platform files (including config.xml and Podfile) gets updated during the prepare step, so if you're using an hook, script or process that runs before prepare (such as before_prepare hook) for example, that will run before the config.xml file is updated inside the platform project, and that would be desired behaviour because the hook is for manipulating things before cordova does things. after_prepare is a hook for after cordova manipulates things, etc.

I can not reproduce the issue using the current version of cordova-ios. Simply adding a plugin in which requires a higher deployment target of 11 is not enough to reproduce the issue. If you can provide a sample reproduction project, it may help us understanding the full scope of when the issue reproduces.

expcapitaldev commented 2 weeks ago

breautek hi! thanks a lot for your activity here. FBSDKCoreKit https://github.com/CocoaPods/Specs/blob/master/Specs/9/b/5/FBSDKCoreKit/17.0.0/FBSDKCoreKit.podspec.json#L13 is required 12.0 you can try add in any your plugin and do clean build.

`

        <pods use-frameworks="true">
            <pod name="FBSDKCoreKit" spec="17.0.0"/>
        </pods>
    </podspec>`

Can you share method or line in cordova-ios library which will be copy deployment-target value from config.xml to Podfile ? I see only https://github.com/apache/cordova-ios/blob/master/lib/Podfile.js#L40 which try to copy value but that value will be empty before installation first pod library

breautek commented 1 week ago

breautek hi! thanks a lot for your activity here. FBSDKCoreKit https://github.com/CocoaPods/Specs/blob/master/Specs/9/b/5/FBSDKCoreKit/17.0.0/FBSDKCoreKit.podspec.json#L13 is required 12.0 you can try add in any your plugin and do clean build.

I still could not reproduce the issue. The config.xml gets copied into platform resources pretty early on in the prepare step. I don't quite remember exactly where it happens in the code. But here's what I did:

I've created a test plugin to wrap around the podspec, the test plugin is effectively just has an empty package.json and the plugin.xml as follows:

<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:rim="http://www.blackberry.com/ns/widgets"
xmlns:android="http://schemas.android.com/apk/res/android"
           id="test-plugin"
      version="5.0.1-dev">

    <name>test</name>
    <description>Cordova test Plugin</description>

    <platform name="ios">
        <podspec>
            <config>
                <source url="https://cdn.cocoapods.org/" />
            </config>
            <pods use-frameworks="true">
                <pod name="FBSDKCoreKit" spec="17.0.0"/>
            </pods>
        </podspec>
    </platform>
</plugin>

I npm pack the plugin which gives a .tgz file.

In a test cordova project, which was empty with no platforms or plugins installed with a base config.xml:

<?xml version='1.0' encoding='utf-8'?>
<widget id="io.cordova.hellocordova" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>HelloCordova</name>
    <description>Sample Apache Cordova App</description>
    <author email="dev@cordova.apache.org" href="https://cordova.apache.org">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
</widget>

I added my test-plugin using a command like: cordova plugin add file:../test-plugin/test-plugin-1.0.0.tgz

The plugin gets added successfully. Not installed at this point because there are no platforms. Just added to the project.

Then I add the platform: cordova platform add ios which this installs cordova-ios 7.1.0

The platform is added successfully, but it does fail to completley install the test plugin because the default deployment target is 11.0, as expected since we didn't explicitly declare an alternate deployment-target preference yet.

So we modify our config.xml and add:

<preference name="deployment-target" value="12.0" />

and then run cordova prepare ios, which updates the Podfile and runs pod install, and the FBSDK is installed to the project as expected.

expcapitaldev commented 1 week ago

I think expected if I do clean build and have inside my main config.xml <preference name="deployment-target" value="13.0" /> and do cordova prepare ios platform shoould be successfully added and prepared with all Pod dependencies without any additional changes. And now my platform should be ready for cordova compile ios command. So, if current flow unexpected please close current issue but I think if my config.xml file has <preference name="deployment-target" value="13.0" /> all Pod dependecies should be installed with 13.0 version.

breautek commented 1 week ago

but I think if my config.xml file has all Pod dependecies should be installed with 13.0 version.

I don't think deployment-target does all pod dependencies, but it maintains the app's Podfile and that is what I observed in the steps I've taken as detailed in https://github.com/apache/cordova-ios/issues/1422#issuecomment-2065289819

If you're still reproducing the issue then confirm that you're running the latest cordova CLI and platform version and share sample project that reproduces the issue that we can look at. If a sample reproduction project cannot be provided then I don't think there's anything we can do to move this forward.