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
Feature request: import relative to project root #9289
Comments
This comment was originally written by @seaneagan You can do this with: import 'package:current_package_name/foo.dart'; see http://pub.dartlang.org/doc/#importing-code-from-a-dependency It might be nice to be able to reference the current package name via "this" or "self" though: import 'package:this/foo.dart'; |
This comment was originally written by misja...@gmail.com But that works only when the referenced file is within an external package. |
This comment was originally written by misja.a...@gmail.com I agree that something like a 'this' package would be a good solution. |
Added Area-Language, Triaged labels. |
This comment was originally written by @marcojakob The problem is mainly for files inside the 'web' folder. See http://stackoverflow.com/questions/16147520/how-to-organize-dart-code-inside-web-folder As I'm using many web components, I can't put them into the same library and omit the import statments since every Web Component must have its own library. |
Dart doesn't have any concept of a "project" or "basedir", so I'm not sure how we could actually support this proposal. What it does have is a package root, and For other imports, I think relative ones with |
I know this was closed a couple of years ago, but I was wondering if this was actually hard or problematic for some reason? I'd love to have something like I'm not in love with either of the current options:
I don't know enough about how dart is compiled to know whether this is just impossible or awful for some reason. But if it isn't it feels like this would be a really nice addition to the language. |
True. On the other hand, if you move an entire directory, then all of the relative imports in files within that directory that don't reach outside stay valid.
Also fair, though in practice I think this is a fairly rare operation and a very easily automated one. A global find/replace of "package:old_name/" to "package:new_name/" should get this done in a second.
Dartfmt doesn't do any import reordering. That's a different tool, either part of your IDE or part of the Dart analyzer plug-in.
It's not impossible, but I'm not sure it carries its weight. We already support two ways to import files in packages: relative and "package:". Adding yet another way is more for users to learn, more for tools to support, more for people to argue about in code reviews, etc. |
@munificent, thanks much for the responses. My bad on the dartfmt question, you are absolutely right that my IDE was doing that - still learning my way around the toolchain. And of course you're also right about the package-name change being rare (it was on my mind since my project has a working name that I know I'm changing later, but that's my problem not anyone else's, and this is ultimately a vanishingly small part of the problem that's gonna cause down the road). One point on the "more for users to learn" question - I think a lot of people come to dart with a certain amount of web familiarity, and it's actually a bit confusing to not have the lib-relative option. Imports on the web would map relatively well ( The tool support comment is definitely legit and I have no response. Anyway, sounds like you've done some thinking on this, so I'll stop being a pain. Thanks again for your quick and thorough reply. |
It should be possible to change the relative reference resolution rules for The changes to the |
How about this way? path "package:myProject" as root;
path "$root/models" as models;
path "$root/views" as views;
import "$models/my_model.dart";
import "$views/my_view.dart"; |
+1 on either or
or ideally both |
I copy projects as templates often and have search and replace everytime. Just give us name independant way to self import. Not asking something extraordinary |
You can always use relative paths from inside the same package, so from |
Last thing I like seeing is |
The added value of the proposal is readability (visual hierarchy) and reusability (copy / paste / templates) simultaneously without the mental overhead of doing search / replace. |
Does anyone know if this is addressed? Import "/a.dart" works on all platforms but windows. |
This issue was originally filed by misja.alm...@gmail.com
Right now import are relative to location of the file they are in. This has two disadvantages:
import '../../../common/listutils.dart';
import '../../canvasutils.dart';
I propose to have import statements which are relative to the root directory of their project or basedir, like for instance Java has.
So the import statements above would become something like:
import 'common/listutils.dart';
import 'mypackage/canvasutils.dart';
The text was updated successfully, but these errors were encountered: