Skip to main content

dart_spawner 1.3.1 copy "dart_spawner: ^1.3.1" to clipboard
dart_spawner: ^1.3.1 copied to clipboard

Runs a Dart script/File/Uri inside a new Isolate of the current Dart VM. It also can spawn a Dart File from another Dart project/package into the current Dart VM.

dart_spawner #

pub package Null Safety Codecov Dart CI GitHub Tag New Commits Last Commits Pull Requests Code size License

Runs a Dart script/File/Uri inside a new Isolate of the current Dart VM. It also can spawn a Dart File from another Dart project/package, using its dependencies, into the current Dart VM.

Usage #

You can run a Dart script (from a String) into a new Isolate:

import 'package:dart_spawner/dart_spawner.dart';

void main() async {
  var spawner = DartSpawner();
  print(spawner);

  var script = r'''
    void main(List<String> args) {
      print('>> From Script! Args: $args');
    }
  ''';

  var spawned = await spawner.spawnDart(script, ['a', 'b', 'c']);
  print('Spawned: $spawned');

  var exitCode = await spawned.exitCode;
  print('Exit code: $exitCode');
}

Output:

DartSpawner{ id: 1, spawned: false, finished: false }
>> From Script! Args: [a, b, c]
Spawned: SpawnedIsolate{ id: 1, type: <script>, finished: false, projectDirectory: Directory: '/path/to/current/dart_project' }
Exit code: 0

Spawning a Dart file inside the current Dart project: #

To run a Dart File inside the current Dart project (used in the main Dart entry point).

import 'package:dart_spawner/dart_spawner.dart';

void main() async {
  var spawner = DartSpawner();
  print(spawner);

  var file = await spawner.projectSubFile('test/hello_world.dart');
  print('Spawning file: $file');

  var spawned = await spawner.spawnDart(file, ['x', 'y']);
  print('Spawned: $spawned');

  var exitCode = await spawned.exitCode;
  print('Exit code: $exitCode');
}

Spawning a Dart file in ANOTHER Dart project: #

You can also run a Dart script/file that exists in another Dart project/package, passing the project directory to the DartSpawner constructor:

import 'dart:io';

import 'package:dart_spawner/dart_spawner.dart';

void main() async {
  var spawner = DartSpawner(
    directory: Directory('/path/to/another/dart_project/'),
  );

  print(spawner);

  var file = await spawner.projectSubFile('example/project_example.dart');
  print('Spawning file: $file');

  var spawned = await spawner.spawnDart(file, ['some', 'args']);
  print('Spawned: $spawned');

  var exitCode = await spawned.exitCode;
  print('Exit code: $exitCode');
}
  • NOTE: It will use the dependencies of the target project and not the ones used in the current Isolate.
  • If the target project is a pub workspace member (resolution: workspace), the workspace root's .dart_tool/package_config.json is used.

Errors and exit code #

If the spawned script throws an uncaught error, exitCode completes with DartSpawner.uncaughtErrorExitCode (255) and the error is available at spawner.uncaughtError:

var spawned = await spawner.spawnDart(script, []);
if (await spawned.exitCode != 0) {
  print('Failed: ${spawner.uncaughtError}');
}

The exit is reported by the spawned isolate's onExit/onError ports. When the VM service is enabled and the isolate is paused at exit (--pause-isolates-on-exit, used by debuggers and coverage tools), the exit is detected through the VM service. A busy isolate is never reported as exited.

Relative paths and URIs #

A relative File, file path or file Uri (no scheme, like Uri.file('bin/main.dart')) is resolved against the current working directory. package:, http(s):, data: and absolute URIs are used as they are.

Running processes #

DartProject.runProcess and runDartVM don't consume the process output by default, so you can read processInfo.process.stdout/stderr yourself. If you don't need it, pass one of catchOutput: true, redirectOutput: true or drainOutput: true: a process whose output is never consumed blocks once it fills the OS pipe buffer.

var processInfo = await DartProject().runProcess(
  'dart',
  ['pub', 'get'],
  drainOutput: true,
);
print('Exit code: ${await processInfo.exitCode}');

Dart VM #

Note that DartSpawner uses Isolate.spawnUri to run a Dart Entry Point (script/file/Uri). This is not supported outside the Dart VM or in a compiled dart file (dart compile exe foo.dart)!

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Author #

Graciliano M. Passos: gmpassos@GitHub.

License #

Dart free & open-source license.

12
likes
160
points
6.37k
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Runs a Dart script/File/Uri inside a new Isolate of the current Dart VM. It also can spawn a Dart File from another Dart project/package into the current Dart VM.

Repository (GitHub)
View/report issues

License

BSD-3-Clause (license)

Dependencies

async_extension, path, yaml

More

Packages that depend on dart_spawner