-
Notifications
You must be signed in to change notification settings - Fork 205
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
Dart imports accepts non Dart files #4184
Comments
Not sure if this is a language request or a tooling request. Let's keep it as language for now. As a language feature, the request would be that it's a compile-time error if any imported, exported or The immediate problem I can see with that is that I have Dart scripts that look like #! dart
// filename dgr
... dart code which I run as That's not an import, export or part file reference, so it technically wouldn't be hit, which suggests that maybe the restriction should be that it's an error if any Dart library or part file has a URI whose path doesn't end in I can always create two files #! /bin/bash
dart dgr.dart "$@" I have a workaround, it's just annoying, not impossible. The errors that this feature would address are:
If you are using an IDE, it's likely that you'll can get a warning about the name anyway. Not sure we have that warning today, but we could definitely add a warning to the analyzer for files with other names. We pretty much expect you to be using an IDE, or at least to run the Dart analyzer on your program, so I think a warning from that would be the easier, and less breaking, way to go. If there isn't one already. |
Thanks for this reply and analysis
In our case, it happened for case 2. We forgot to put the We a re for sure using an IDE but none of the IDE used in the team triggered any warning for this letting everyone consider everything's ok
Sounds good. Adding a simple warning could help a lot. |
In Dart
3.5.4 (stable)
, imports accepts non Dart filesSetup
Create a new Dart project using
dart create dart_puzzler
Project will contain 2 dart files
With
bin/dart_puzzler.dart
containingAnd
lib/dart_puzzler.dart
containingObserved behavior
Create a new file
lib/to_import
omitting the extension and add some dart contentNow replace the import in
bin/dart_puzzler.dart
byIt works with no warning/error.
The code can be run using
dart run bin/dart_puzzler.dart
Strange, but why not. Imported file contains dart code so it can be ok I guess ?
2nd observed behavior
Create a Kotlin file
lib/kotlin_file.kt
with the following contentNow replace the import in
bin/dart_puzzler.dart
byThe dart code accepts the import without any warning/error.
Try to execute the code using
dart run bin/dart_puzzler.dart
Fortunately it fails with many errors on the kotlin syntax which is normal.
But no error is raised on the import itself.
The problem
Dart seems to accept any file in the import directive and tries to parse the file content as Dart code no matter the language of the written code.
This can lead to unexpected behavior with some languages getting similar syntax as Dart like Java. In Some cases, Dart can successfully execute Java code with Dart interpretation.
Expected behavior
Should Dart accept non Dart files in imports ?
Should we get an error like
Only Dart files can be imported
?The text was updated successfully, but these errors were encountered: