flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
165.24k stars 27.26k forks source link

pub workspace mixes plugins between projects #155242

Open xvrh opened 2 weeks ago

xvrh commented 2 weeks ago

Steps to reproduce

Expected results

The 2 projects should be separated. The plugins used by one project should not leak to the other projects (for binary size reasons?).

Actual results

After you run flutter run (or flutter test), you notice that various files from project1 contains pointer to the plugins used by project2 (in files GeneratedPluginRegistrant.swift|java)

Example in GeneratedPluginRegistrant.swift in project2:

import url_launcher_macos
import webview_flutter_wkwebview

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
  UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
  FLTWebViewFlutterPlugin.register(with: registry.registrar(forPlugin: "FLTWebViewFlutterPlugin"))
}

Code sample

git clone https://github.com/xvrh/pub_workspace_test

Screenshots or Video

Screenshots / Video demonstration [Upload media here]

Logs

Logs ```console [Paste your logs here] ```

Flutter Doctor output

Doctor output ```console Flutter (Channel main, 3.26.0-1.0.pre.131, on macOS 14.5 23F79 darwin-arm64, locale en-BE) ```
danagbemava-nc commented 2 weeks ago

Reproducible using the repository provided above.

flutter doctor -v ``` [!] Flutter (Channel master, 3.26.0-1.0.pre.129, on macOS 14.6.1 23G93 darwin-arm64, locale en-US) • Flutter version 3.26.0-1.0.pre.129 on channel master at /Users/deanli/dev/master • Upstream repository https://github.com/flutter/flutter.git • Framework revision b4903228eb (9 hours ago), 2024-09-15 22:29:13 -0400 • Engine revision 9aaea5a4bd • Dart version 3.6.0 (build 3.6.0-255.0.dev) • DevTools version 2.40.0-dev.1 ```
bkonyi commented 4 days ago

Is this only happening for macOS / iOS targets or for Android as well?

xvrh commented 3 days ago

@bkonyi It happens for all platforms.

In Android, the GeneratedPluginRegistrant.java will contains references to plugins that are not referenced by the current project.

GeneratedPluginRegistrant.java ```java @Keep public final class GeneratedPluginRegistrant { private static final String TAG = "GeneratedPluginRegistrant"; public static void registerWith(@NonNull FlutterEngine flutterEngine) { try { flutterEngine.getPlugins().add(new io.flutter.plugins.urllauncher.UrlLauncherPlugin()); } catch (Exception e) { Log.e(TAG, "Error registering plugin url_launcher_android, io.flutter.plugins.urllauncher.UrlLauncherPlugin", e); } try { flutterEngine.getPlugins().add(new io.flutter.plugins.webviewflutter.WebViewFlutterPlugin()); } catch (Exception e) { Log.e(TAG, "Error registering plugin webview_flutter_android, io.flutter.plugins.webviewflutter.WebViewFlutterPlugin", e); } } } ```

This is an important bug because it means the final application will be bundled with a bunch of unused native code.

It seems the plugins are derived directly from the package_config.

https://github.com/flutter/flutter/blob/2528fa95eb3f8e107b7e28651a61305d960eef85/packages/flutter_tools/lib/src/flutter_plugins.dart#L91

We should add a filter using the pubspec.yaml

stuartmorgan commented 3 days ago

We should add a filter using the pubspec.yaml

We can't filter with pubspec.yaml directly, because it doesn't contain the transitive dependency tree.

xvrh commented 3 days ago

@stuartmorgan of course, I meant start from the pubspec.yaml and transitively discover all real sub dependencies.

We may even fix an other problem: which is to not include plugins from "dev_dependencies". But this is less important and probably a breaking change to think of separetely.