Search code examples
filedartpackageconventionsdart-pub

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


Solution

  • 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).