acquireDependencies method

Future<void> acquireDependencies(
  1. SolveType type, {
  2. Iterable<String>? unlock,
  3. bool dryRun = false,
  4. bool precompile = false,
  5. bool summaryOnly = false,
  6. bool enforceLockfile = false,
})

Gets all dependencies of the workspaceRoot package.

Performs version resolution according to SolveType.

The iterable unlock specifies the list of packages whose versions can be changed even if they are locked in the pubspec.lock file.

Shows a report of the changes made relative to the previous lockfile. If this is an upgrade or downgrade, all transitive dependencies are shown in the report. Otherwise, only dependencies that were changed are shown. If dryRun is true, no physical changes are made.

If precompile is true (the default), this snapshots dependencies' executables.

if summaryOnly is true only success or failure will be shown --- in case of failure, a reproduction command is shown.

Updates lockFile and packageRoot accordingly.

If enforceLockfile is true no changes to the current lockfile are allowed. Instead the existing lockfile is loaded, verified against pubspec.yaml and all dependencies downloaded.

Implementation

Future<void> acquireDependencies(
  SolveType type, {
  Iterable<String>? unlock,
  bool dryRun = false,
  bool precompile = false,
  bool summaryOnly = false,
  bool enforceLockfile = false,
}) async {
  workspaceRoot; // This will throw early if pubspec.yaml could not be found.
  summaryOnly = summaryOnly || _summaryOnlyEnvironment;
  final suffix = workspaceRoot.dir == '.' ? '' : ' in `${workspaceRoot.dir}`';

  if (enforceLockfile && !fileExists(lockFilePath)) {
    throw ApplicationException('''
Retrieving dependencies failed$suffix.
Cannot do `--enforce-lockfile` without an existing `pubspec.lock`.

Try running `$topLevelProgram pub get` to create `$lockFilePath`.''');
  }

  SolveResult result;

  try {
    result = await log.progress('Resolving dependencies$suffix', () async {
      // TODO(https://github.com/dart-lang/pub/issues/4127): Check this for
      // all workspace pubspecs.
      _checkSdkConstraint(workspaceRoot.pubspecPath, workspaceRoot.pubspec);
      return resolveVersions(
        type,
        cache,
        workspaceRoot,
        lockFile: lockFile,
        unlock: unlock ?? [],
      );
    });
  } on SolveFailure catch (e) {
    throw SolveFailure(
      e.incompatibility,
      suggestions: await suggestResolutionAlternatives(
        this,
        type,
        e.incompatibility,
        unlock ?? [],
        cache,
      ),
    );
  }

  // We have to download files also with --dry-run to ensure we know the
  // archive hashes for downloaded files.
  final newLockFile = await result.downloadCachedPackages(cache);

  final report = SolveReport(
    type,
    workspaceRoot.dir,
    workspaceRoot.pubspec,
    lockFile,
    newLockFile,
    result.availableVersions,
    cache,
    dryRun: dryRun,
    enforceLockfile: enforceLockfile,
    quiet: summaryOnly,
  );

  await report.show(summary: true);
  if (enforceLockfile && !_lockfilesMatch(lockFile, newLockFile)) {
    dataError('''
Unable to satisfy `${workspaceRoot.pubspecPath}` using `$lockFilePath`$suffix.

To update `$lockFilePath` run `$topLevelProgram pub get`$suffix without
`--enforce-lockfile`.''');
  }

  if (!(dryRun || enforceLockfile)) {
    newLockFile.writeToFile(lockFilePath, cache);
  }

  _lockFile = newLockFile;

  if (!dryRun) {
    _removeStrayLockAndConfigFiles();

    /// Build a package graph from the version solver results so we don't
    /// have to reload and reparse all the pubspecs.
    _packageGraph = Future.value(PackageGraph.fromSolveResult(this, result));

    await writePackageConfigFile();

    try {
      if (precompile) {
        await precompileExecutables();
      } else {
        await _deleteExecutableSnapshots();
      }
    } catch (error, stackTrace) {
      // Just log exceptions here. Since the method is just about acquiring
      // dependencies, it shouldn't fail unless that fails.
      log.exception(error, stackTrace);
    }
  }
}