Skip to content

proposal: bin_files_must_export_mainΒ #57130

@jonasfj

Description

@jonasfj

bin_files_must_export_main

Could also be called:

  • no_libraries_in_bin
  • executable_without_main

Description

Dart files in bin/ should export a main function that can serve as entrypoint for an executable.

Details

Files in the bin/ folder can be executed using dart run in your package, or any package that has a dependency on your package. Private scripts should be placed in tool/ and private libraries should be placed in lib/src/.

See package layout conventions.

Kind

This aims to guard against errors.

Bad Examples

# bin/foo.dart

const myFoo = 42;

Good Examples

# bin/foo.dart

void main() {
  print('hello world');
}

or

# bin/foo.dart

// You can export 'main' from a library, that's okay too.
export 'package:foo/src/foo.dart' show main;

Discussion

  • dart run foo will run bin/foo.dart from package:foo (requires that package :foo is either a dependency or the name of the root package).
  • dart run foo:bar will run bin/bar.dart from package:foo.
  • dart pub global run foo will run bin/foo.dart from package:foo.
  • The executables section in pubspec.yaml offers the creation of binstubs for files in bin/.
  • pana will assign packages with files in bin/ as has:executable.

It could be argued that this lint should only be enabled for packages that have a version and doesn't feature publish_to: none in the pubspec.yaml.
Though, there is no reason for other packages / apps to put libraries in bin/, this folder should be reserved for executables.

Motiviation

  • Package layout convention
  • has:executable search tag on pub.dev
    • This is granted by pana analysis.
    • We want packages that match this tag to have executable files in bin/.
    • We could make a check for this in pana, and score on this basis. But it's probably better UX to give a lint directly in the editor. That way authors will know it sooner rather than later.

cc @sigurdm, @isoos

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-linterIssues with the analyzer's support for the linter packagelinter-lint-proposallinter-status-pendingtype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions