Closed mcmah309 closed 1 day ago
Summary: Conditional exports in Dart fail when using multiple if
statements for platform checks. A provided example demonstrates the issue and its failure in CI.
This looks like it's working as intended.
You can write any sequence of dot-separated identifiers as the condition. It's not a Dart expression that is evaluated, it's a key into the compilation environment, so the export:
export 'src/stub.dart'
if (Platform.isLinux) 'src/linux.dart'
if (Platform.isWindows) 'src/windows.dart';
will use src/linux
if const String.fromEnvironment("Platform.isLinux") == "true"
. Which it isn't. And "Platform.isWindows"
is also not defined in the compilation environment, so it chooses neither of the conditional URIs.
The conditions are not invalid, they're just not configured.
The conditional imports currently only work with keys of the form dart.library.name
, where dart.library.name
is set to "true"
in the complation environment if dart:name
is an available platform library.
There are no available and usable compilation environment keys that expose the target platform. (Maybe there should be, see #54785.)
Thanks for the explanation. Then maybe this issue should have been turned into a documentation issue rather than being closed. Since I can't find any documentation related to this issue. The only documentation related to conditional imports I can find is here https://dart.dev/guides/libraries/create-packages#conditionally-importing-and-exporting-library-files and it does not go into any detail on what is going on or options available. It just gives a single example.
will use
src/linux
ifconst String.fromEnvironment("Platform.isLinux") == "true"
The conditional imports currently only work with keys of the form
dart.library.name
, wheredart.library.name
is set to"true"
in the complation environment ifdart:name
is an available platform library.
Taking these statements, is it possible to pass keys to the compilation environment?
flutter run --dart-define Platform.isLinux=true -d linux
Does not seem to have any effect.
A created a PR to improve the docs
Conditional exports do not detect invalid
if(..)
statements. I created an example repo to show this:https://github.com/mcmah309/conditional_export_issue
Further:
If you replace the export in this file with either of the other commented out conditional exports, then the test will pass only on one platform instead of failing on both -
Original Failed CI Run: https://github.com/mcmah309/conditional_export_issue/actions/runs/11942744134 Linux Export First: https://github.com/mcmah309/conditional_export_issue/actions/runs/11942753955 Windows Export First: https://github.com/mcmah309/conditional_export_issue/actions/runs/11942761382
The expected behavior is to fail at compilation time for invalid
if(..)
statements. And hopefully allow platform compilation, like what is being attempted.