I have a number of xml files, exported from eclipse, and I would like to stick to a common protocol with my eclipse-using colleagues. It has been suggested to me, that I can simply import the settings into IDEA. Now, I've figured out the files that I could need and how to import them (formatting, for example).
I am a bit confused about cleanupSettings.xml. What does it do? and will I gain any benefit in having the same "cleanup" as my colleagues. If so, how do I import them?
Or a better way to ask this, is : how do I, in intellij, do "cleanup" tasks, such as organise imports, and know that, when I reformat, these things will happen. Imagine I have a very specific action, such as prepend the source file with "asdf"
import "google/protobuf/duration.proto";
For the above import statement, I wonder how protoc knows where to find that proto file, since it's obviously not in somewhere of my proto_path.
I have a shared.proto in some package, and could be refered by many projects, so I cannot put it to some project level to share the same protp_path. My question is how to refer this proto just like import duration.proto?
This is a good question for me because I know why it works but (still) don't fully understand how.
Why? Because the well-known types are bundled with protoc in a lib folder alongside the bin folder. I assume for e.g. Python, the implementation similarly includes these.
How? I think (!?) this works because protoc is configured (!?) to look for these types in that location (where is protoc? Look for lib in a sibling folder).
The documentation would benefit from explaining this because I used to manually add these to proto_path until once, when I didn't and worked without the import.
In Flutter, for importing libraries within our own package's lib directory, should we use relative imports
import 'foo.dart'
or package import?
import 'package:my_app/lib/src/foo.dart'
Dart guidelines advocate to use relative imports :
PREFER relative paths when importing libraries within your own package’s lib directory.
whereas Provider package says to always use packages imports :
Always use package imports. Ex: import 'package:my_app/my_code.dart';
Is there a difference other than conciseness? Why would packages imports would reduce errors over relative imports?
From the same Dart guidelines, further down they give this reason for the relative imports:
There is no profound reason to prefer the former—it’s just shorter, and we want to be consistent.
Personally, I prefer the absolute method, despite it being more verbose, as it means when I'm importing from different dart files (in other folders), I don't have to work out where the file to be imported is, relative to the current file. Made-up example:
I have two dart files, at different folder levels, that need to import themes/style.dart:
One is widgets/animation/box_anim.dart, where the relative path import would be:
import '../../themes/style.dart';
The other is screens/home_screen.dart with the relative import:
import '../themes/style.dart';
This can get confusing, so I find it better to just use the absolute in both files, keeping it consistent:
import 'package:myapp/themes/style.dart';
And just stick that rule throughout. So, basically, whatever method you use - Consistency is key!
The Linter for Dart package, also has something to say about this, but is more about the Don'ts of mixing in the '/lib' folder:
DO avoid relative imports for files in lib/.
When mixing relative and absolute imports it's possible to create
confusion where the same member gets imported in two different ways.
An easy way to avoid that is to ensure you have no relative imports
that include lib/ in their paths.
TLDR; Choose the one you prefer, note that prefer_relative_imports is recommended in official Effective Dart guide
First of all, as mentioned in this answer, Provider do not recommands package imports anymore.
Dart linter provides a list of rules, including some predefined rulesets :
pedantic for rules enforced internally at Google
lints or even flutter_lints (previously effective_dart) for rules corresponding to the Effective Dart style guide
flutter for rules used in flutter analyze
Imports rules
There is actually more than two opposites rules concerning imports :
avoid_relative_lib_imports, enabled in pedantic and lints rulesets, basically recommend to avoid imports that have 'lib' in their paths.
The two following are the one you mention :
prefer_relative_imports, enabled in no predefined rulesets, but recommended in Effective Dart guide in opposition to :
always_use_package_imports, enabled in no predefined rulesets. Which means that it is up to you and to your preferences to enable it (be careful, it is incompatible with the previous rule)
Which one should I choose?
Choose the rule you want ! It will not cause any performance issue, and no rule would reduce errors over the other. Just pick one and make your imports consistent across all your project, thanks to Dart linter.
I personnaly prefer using prefer_relative_imports, as it is recommended by Dart team, with this VSCode extension which automatically fix and sort my imports.
Provider do not need packages imports anymore.
This was a workaround to an old Dart bug: Flutter: Retrieving top-level state from child returns null
TL;DR, by mixing relative and absolute imports, sometimes Dart created a duplicate of the class definition.
This led to the absurd line that is:
import 'package:myApp/test.dart' as absolute;
import './test.dart' as relative;
void main() {
print(relative.Test().runtimeType == absolute.Test().runtimeType); // false
}
Since provider relies on runtimeType to resolve objects, then this bug made provider unable to obtain an object in some situations.
My 5 cents on the topic are that absolute (package:my_app/etc/etc2...) imports cause much less trouble than relative ones (../../etc/etc2...) when you decide to reorganize/cleanup your project`s structure because whenever you move a file from one directory to another you change the "starting point" of every relative import that this file uses thus breaking all the relative imports inside the moved file...
I'd personally always prefer absolute to relative paths for this reason
This question already has good answers, but I wanted to mention an insanely annoying and hard-to-find problem I experienced with unit testing that was caused by a relative import.
The expect fail indicator for an exception-catching expect block
expect(
() => myFunction,
throwsA(isA<InvalidUserDataException>())
);
was showing the actual result as exactly the same as the expected result, and zero indication of why it's failing.
After massive trial-and-error, the issue was because the expected InvalidUserDataException (a custom-made class) was being imported to the test file in RELATIVE format vs PACKAGE format.
To find this, I had to compare side-by-side, line-by-line, call-by-call between this test file and another test file that uses the exact same exception expecters (It's lucky, we had this), and just by chance, I happened to scroll to the top of this file's imports and see the blue underline saying prefer relative imports to /lib directory.
No, they're not preferred; they're necessary, because the moment I changed that to a PACKAGE (absolute) import, everything suddenly started working.
What I learned from this is: Use absolute imports for test files (files outside the lib directory)
e.g. inside of src/test/main_test.dart
DON'T: use import '../lib/main.dart'
DO: use package:my_flutter_app/main.dart
Maybe other people knew this already, but I didn't, and I couldn't find anything online with searches about this issue, so I thought I would share my experience that might help others who got stuck around this.
Does anyone know why this happens?
Edit: For context, this happened while using Flutter 2.1.4 (stable) with Sound Null Safety
Do you use Integration Tests?
If the answer is yes, then in most cases you need to use package imports. When you attempt to run your integration tests on a physical device, any relative imports will not be able to find what they're looking for.
Example: https://github.com/fluttercommunity/get_it/issues/76
You can enforce package imports in your project by using these two linting rules:
always_use_package_imports
avoid_relative_lib_imports
I also prefer package imports because they stick even when rearranging your files and folders. Relative imports frequently break and it's a pain to have to remove them and reimport the offending dependency.
One very simple reason to not use package imports: rename your package without editing every dart file
Renaming can happen a few times while the product does not have a definitive name, and you or your product owner decides to change it.
It is much more painful to rename with package imports as your package name is in every import.
Of course you can change it with a find/replace query, but it's a useless edit on every dart file you can avoid with relative imports.
Plus, vscode allows to automatically update relative imports on file move/rename and I have never had any issue with this feature.
I spent 4 hours, on something simple, trying to figure out why pycharm did not find my pika library when running from inside development environment. The answer became obvious once found but for all you who are suffering from this simple issue try this:
Pycharm -> Run -> Configurations
Uncheck
Add content roots to PYTHONPATH
Add source roots to PYTHONPATH
Run/Debug Configurations
These settings should not result in you not finding the library in your PATH.
It's possible you have files in your project which mirror the names of the library or are otherwise interfering with resolution of the import name. You really should try to fix this issue right here, or you may find yourself having to debug even stranger problems after you send the code along to someone else.
Let's say that you're trying to run :
>>> import foo
This will look for foo.py, or a folder named foo containing __init.py__ in your PYTHONPATH.
If your own code also contains foo.py (or a folder named foo containing __init.py__), python will import your own module instead of the site package you're actually trying to import.
This may seemingly work without error, but if you were instead to do :
>>> from foo import fooclass
This class does not exists in your library, and therefore you're going to get an ImportError.
Similarly, if you did :
>>> import foo
>>> c = foo.fooclass()
You should get an AttributeError
Adding your source roots to PYTHONPATH is a fairly common requirement, and something you may need if your project grows beyond a few files. Not being able to do that can result in some really laborious workarounds in the future.
Please tell me a more describing title :P
I have an SBT project in IntelliJ and its src directory constructed like:
src/main/scala/package1/AAA.scala
src/main/scala/package1/BBB.scala
src/main/scala/package2/CCC.scala
src/main/scala/...
So now these classes are imported like:
import package1.AAA._
But actually I want to import these classes like:
import org.myname.package1.AAA._
At the moment I have two ways; one is to put org.myname to all the class files' package attributes like:
package org.myname.package1
class AAA {...
which works, but doing this manually is time consuming, and makes things complicated.
Another one is to reformat the directories like:
src/main/scala/org/myname/package1/AAA.scala
src/main/scala/org/myname/package1/BBB.scala
src/main/scala/org/myname/package2/CCC.scala
src/main/scala/org/myname/...
which works too, but I'd not like to make the looking of the structure like this.
Is there an automatic way to include all classes into org.myname without changing the structure, with the SBT or IntelliJ features?
Create a new package named org.myname.
Move the package1 inside org.myname
Move package2 inside org.myname
IntellIJ IDEA will fix everything for you.