78 lines
2.5 KiB
Plaintext
78 lines
2.5 KiB
Plaintext
Referenced packages should be listed as dependencies in `pubspec.yaml`.
|
|
|
|
== Why is this an issue?
|
|
|
|
A package import, such as `import package:my_package/my_file.dart`, requires the `my_package` package to be readily available in the project during build, for the `my_file.dart` source to be imported in the current source file.
|
|
|
|
The standard way to ensure this is the case is to add the dependency to `my_package` in the `pubspec.yaml` file:
|
|
|
|
* in the `dependencies` section, if the package is a runtime dependency, that is, the package is referenced from `lib` or `bin`
|
|
* in the `dev_dependencies` section, if the package is a development dependency, that is, the package is referenced from other non-production directories, such as `test`, `test_resources` etc.
|
|
|
|
=== What is the potential impact?
|
|
|
|
If the package is not listed as a dependency in `pubspec.yaml`, the build will only succeed if the package is somehow provided locally at build time.
|
|
|
|
This can not only lead to build failures, but also to unexpected behavior, as the package may not be the same version as the one the code was developed against, since the version is not locked in the `pubspec.yaml` file and the standard package resolution mechanism of `dart pub get` is not used to deal with the lifecycle of this dependency.
|
|
|
|
== How to fix it
|
|
|
|
Add the missing package as a dependency in the `pubspec.yaml` file, to the relevant section (`dependencies` or `dev_dependencies`).
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,dart,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
import package:my_package/my_file.dart;
|
|
// ...
|
|
----
|
|
|
|
[source,yaml,diff-id=2,diff-type=noncompliant]
|
|
----
|
|
# pubspec.yaml
|
|
name: my_project
|
|
dependencies:
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,dart,diff-id=1,diff-type=compliant]
|
|
----
|
|
import package:my_package/my_file.dart;
|
|
// ...
|
|
----
|
|
|
|
[source,yaml,diff-id=2,diff-type=compliant]
|
|
----
|
|
# pubspec.yaml
|
|
name: my_project
|
|
dependencies:
|
|
my_package: ^1.0.0
|
|
----
|
|
|
|
== Resources
|
|
|
|
=== Documentation
|
|
|
|
* Dart Docs - https://dart.dev/tools/linter-rules/depend_on_referenced_packages[Dart Linter - depend_on_referenced_packages]
|
|
* Dart Docs - https://dart.dev/tools/pub/pubspec[Dart Pubspec]
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
The imported package '<packageName>' isn't a dependency of the importing package.
|
|
|
|
=== Highlighting
|
|
|
|
The full import string: e.g. `package:my_package/my_file.dart` in `import package:my_package/my_file.dart`.
|
|
|
|
endif::env-github,rspecator-view[]
|