Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Conditional Exports Do Not Detect Invalid Arguments #59575

Closed
mcmah309 opened this issue Nov 20, 2024 · 5 comments
Closed

Bug: Conditional Exports Do Not Detect Invalid Arguments #59575

mcmah309 opened this issue Nov 20, 2024 · 5 comments
Labels
closed-as-intended Closed as the reported issue is expected behavior type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@mcmah309
Copy link

mcmah309 commented Nov 20, 2024

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.

@dart-github-bot
Copy link
Collaborator

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.

@dart-github-bot dart-github-bot added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Nov 20, 2024
@mcmah309 mcmah309 changed the title Bug: Conditional Exports Do Not Work With Multiple Platforms Bug: Conditional Exports Do Not Detect Invalid Arguments Nov 20, 2024
@lrhn
Copy link
Member

lrhn commented Nov 21, 2024

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

@lrhn lrhn closed this as completed Nov 21, 2024
@lrhn lrhn added closed-as-intended Closed as the reported issue is expected behavior and removed triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. labels Nov 21, 2024
@mcmah309
Copy link
Author

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.

@mcmah309
Copy link
Author

mcmah309 commented Nov 21, 2024

will use src/linux if const String.fromEnvironment("Platform.isLinux") == "true"

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.

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.

@mcmah309
Copy link
Author

A created a PR to improve the docs

dart-lang/site-www#6228

MaryaBelanger added a commit to dart-lang/site-www that referenced this issue Nov 25, 2024
Improve conditional import/export docs

Related to dart-lang/sdk#59575

---

Co-authored-by: Marya <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-as-intended Closed as the reported issue is expected behavior type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

3 participants