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

Dart imports accepts non Dart files #4184

Open
PiotrFLEURY opened this issue Nov 29, 2024 · 2 comments
Open

Dart imports accepts non Dart files #4184

PiotrFLEURY opened this issue Nov 29, 2024 · 2 comments
Labels
request Requests to resolve a particular developer problem

Comments

@PiotrFLEURY
Copy link

In Dart 3.5.4 (stable), imports accepts non Dart files

Setup

Create a new Dart project using dart create dart_puzzler

Project will contain 2 dart files

|
|-- bin
|    |-- dart_puzzler.dart
|-- lib
|    |-- dart_puzzler.dart

With bin/dart_puzzler.dart containing

import 'package:dart_puzzler/dart_puzzler.dart' as dart_puzzler;

void main(List<String> arguments) {
  print('Hello world: ${dart_puzzler.calculate()}!');
}

And lib/dart_puzzler.dart containing

int calculate() {
  return 6 * 7;
}

Observed behavior

Create a new file lib/to_import omitting the extension and add some dart content

int calculate() {
  return 2 + 2;
}

Now replace the import in bin/dart_puzzler.dart by

import 'package:dart_puzzler/to_import' as dart_puzzler;

It 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 content

fun calculate(): Int {
    return 2 + 2
}

Now replace the import in bin/dart_puzzler.dart by

import 'package:dart_puzzler/kotlin_file.kt' as dart_puzzler;

The 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 ?

@PiotrFLEURY PiotrFLEURY added the request Requests to resolve a particular developer problem label Nov 29, 2024
@lrhn
Copy link
Member

lrhn commented Nov 29, 2024

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 part'ed file has a URI whose path does not end with .dart.

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 dgr something something on the command line.

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 .dart, so it also triggers on the entry point.
But then it would break my script.

I can always create two files dgr.dart and dgr where the latter is just

#! /bin/bash
dart dgr.dart "$@"

I have a workaround, it's just annoying, not impossible.

The errors that this feature would address are:

  • Accidentally pointing to a non-Dart file. It'll tell you immediately, rather than after trying to load and parse a 4Gb JSON file. So, early error on something that will likely be an error anyway.
  • Accidentally forgetting to put .dart on a Dart file. And forgetting to write the .dart in the import too (which is easy if you use your IDE to automatically add the import).

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.
Warnings are more malleable than errors, because a warning can be suppressed if you know what you are doing.

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.

@PiotrFLEURY
Copy link
Author

Thanks for this reply and analysis

The errors that this feature would address are:

Accidentally pointing to a non-Dart file. It'll tell you immediately, rather than after trying to load and parse a 4Gb JSON file. > So, early error on something that will likely be an error anyway.
Accidentally forgetting to put .dart on a Dart file. And forgetting to write the .dart in the import too (which is easy if you use > your IDE to automatically add the import).

In our case, it happened for case 2. We forgot to put the .dart file extension

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

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.
Warnings are more malleable than errors, because a warning can be suppressed if you know what you are doing.

Sounds good. Adding a simple warning could help a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
request Requests to resolve a particular developer problem
Projects
None yet
Development

No branches or pull requests

2 participants