Shubhamhackz
Shubhamhackz

Reputation: 7973

What is the difference between lib/src and /bin in dart?

I know that lib/ is where we put all our library files and /bin is where we put our entrypoint for our command-line app. I know both of them are public lib/ and bin but i'm unable to understand the convention of using lib/src which according to the official docs should contain: implementation code

Upvotes: 13

Views: 4760

Answers (2)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657929

lib/ is the directory that contains shareable code. It can be shared

  • to other top-level directories like bin/, web/, example/, test/, tool/, ... in the same package
  • to other packages that have this package as a dependency.

lib/src by convention contains the private implementation of the public API exposed by lib/ or lib/xxx where xxx is not src.

bin is reserved for command line apps and contains the Dart entry point scripts to execute them (the files that contain main() {...}).

In pubspec.yaml you can define executables https://www.dartlang.org/tools/pub/pubspec#executables that allows you to run scripts from bin/ by just executing foo to have dart somePath/bin/foo.dart executed (using pub global activate my_package_with_foo).

Upvotes: 26

Alexandre Ardhuin
Alexandre Ardhuin

Reputation: 76323

See Pub Package Layout Conventions - Implementation files

The libraries inside lib are publicly visible: other packages are free to import them. But much of a package’s code is internal implementation libraries that should only be imported and used by the package itself. Those go inside a subdirectory of lib called src. You can create subdirectories in there if it helps you organize things.

You are free to import libraries that live in lib/src from within other Dart code in the same package (like other libraries in lib, scripts in bin, and tests) but you should never import from another package’s lib/src directory. Those files are not part of the package’s public API, and they might change in ways that could break your code.

When you use libraries from within your own package, even code in src, you can (and should) still use package: to import them.

Upvotes: 9

Related Questions