Skip to content

Commit

Permalink
Revert "Use the frontend server to compile pub executables (dart-lang…
Browse files Browse the repository at this point in the history
…#2968)" (dart-lang#3006)

This reverts commit e01e3a4.
  • Loading branch information
jonasfj authored May 20, 2021
1 parent 492b15b commit d2ad13d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ jobs:
cat "$_TESTS_FILE.${{ matrix.shard }}"
shell: bash
- name: Run tests
run: dart test --use-data-isolate-strategy --preset travis $(cat $_TESTS_FILE.${{ matrix.shard }})
run: dart test --preset travis $(cat $_TESTS_FILE.${{ matrix.shard }})
shell: bash
86 changes: 35 additions & 51 deletions lib/src/dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import 'package:analyzer/dart/analysis/session.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/error/error.dart';
import 'package:cli_util/cli_util.dart';
import 'package:frontend_server_client/frontend_server_client.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as p;

import 'exceptions.dart';
Expand All @@ -40,6 +38,41 @@ bool isEntrypoint(CompilationUnit dart) {
});
}

/// Snapshots the Dart executable at [executablePath] to a snapshot at
/// [snapshotPath].
///
/// If [packageConfigFile] is passed, it's used to resolve `package:` URIs in
/// the executable. Otherwise, a `packages/` directory or a package spec is
/// inferred from the executable's location.
///
/// If [name] is passed, it is used to describe the executable in logs and error
/// messages.
Future snapshot(
String executablePath,
String snapshotPath, {
String packageConfigFile,
String name,
}) async {
final args = [
if (packageConfigFile != null) '--packages=$packageConfigFile',
'--snapshot=$snapshotPath',
p.toUri(executablePath).toString()
];

final result = await runProcess(Platform.executable, args);
final highlightedName = name = log.bold(name ?? executablePath.toString());
if (result.success) {
log.message('Precompiled $highlightedName.');
} else {
// Don't leave partial results.
deleteEntry(snapshotPath);

throw ApplicationException(
log.yellow('Failed to precompile $highlightedName:\n') +
result.stderr.join('\n'));
}
}

class AnalysisContextManager {
/// The map from a context root directory to to the context.
final Map<String, AnalysisContext> _contexts = {};
Expand Down Expand Up @@ -148,52 +181,3 @@ class AnalyzerErrorGroup implements Exception {
@override
String toString() => errors.join('\n');
}

/// Precompiles the Dart executable at [executablePath] to a kernel file at
/// [outputPath].
///
/// This file is also cached at [incrementalDillOutputPath] which is used to
/// initialize the compiler on future runs.
///
/// The [packageConfigPath] should point at the package config file to be used
/// for `package:` uri resolution.
///
/// The [name] is used to describe the executable in logs and error messages.
Future<void> precompile({
@required String executablePath,
@required String incrementalDillOutputPath,
@required String name,
@required String outputPath,
@required String packageConfigPath,
}) async {
ensureDir(p.dirname(outputPath));
ensureDir(p.dirname(incrementalDillOutputPath));
const platformDill = 'lib/_internal/vm_platform_strong.dill';
final sdkRoot = p.relative(p.dirname(p.dirname(Platform.resolvedExecutable)));
var client = await FrontendServerClient.start(
executablePath,
incrementalDillOutputPath,
platformDill,
sdkRoot: sdkRoot,
packagesJson: packageConfigPath,
printIncrementalDependencies: false,
);
try {
var result = await client.compile();

final highlightedName = log.bold(name);
if (result?.errorCount == 0) {
log.message('Precompiled $highlightedName.');
await File(incrementalDillOutputPath).copy(outputPath);
} else {
// Don't leave partial results.
deleteEntry(outputPath);

throw ApplicationException(
log.yellow('Failed to precompile $highlightedName:\n') +
(result?.compilerOutputLines?.join('\n') ?? ''));
}
} finally {
client.kill();
}
}
27 changes: 6 additions & 21 deletions lib/src/entrypoint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,6 @@ class Entrypoint {
/// The path to the directory containing dependency executable snapshots.
String get _snapshotPath => p.join(cachePath, 'bin');

/// The path to the directory containing previous dill files for incremental
/// builds.
String get _incrementalDillsPath => p.join(cachePath, 'incremental');

/// Loads the entrypoint from a package at [rootDir].
Entrypoint(String rootDir, this.cache)
: root = Package.load(null, rootDir, cache.sources),
Expand Down Expand Up @@ -338,7 +334,7 @@ class Entrypoint {
ensureDir(_snapshotPath);
}
return waitAndPrintErrors(executables.map((executable) {
var dir = p.dirname(pathOfExecutable(executable));
var dir = p.dirname(snapshotPathOfExecutable(executable));
cleanDir(dir);
return _precompileExecutable(executable);
}));
Expand All @@ -348,18 +344,16 @@ class Entrypoint {
/// Precompiles executable .dart file at [path] to a snapshot.
Future<void> precompileExecutable(Executable executable) async {
return await log.progress('Precompiling executable', () async {
ensureDir(p.dirname(pathOfExecutable(executable)));
ensureDir(p.dirname(snapshotPathOfExecutable(executable)));
return waitAndPrintErrors([_precompileExecutable(executable)]);
});
}

Future<void> _precompileExecutable(Executable executable) async {
final package = executable.package;
await dart.precompile(
executablePath: resolveExecutable(executable),
outputPath: pathOfExecutable(executable),
incrementalDillOutputPath: incrementalDillPathOfExecutable(executable),
packageConfigPath: packageConfigFile,
await dart.snapshot(
resolveExecutable(executable), snapshotPathOfExecutable(executable),
packageConfigFile: packageConfigFile,
name:
'$package:${p.basenameWithoutExtension(executable.relativePath)}');
}
Expand All @@ -371,7 +365,7 @@ class Entrypoint {
/// different sdk.
///
/// [path] must be relative.
String pathOfExecutable(Executable executable) {
String snapshotPathOfExecutable(Executable executable) {
assert(p.isRelative(executable.relativePath));
final versionSuffix = sdk.version;
return isGlobal
Expand All @@ -381,15 +375,6 @@ class Entrypoint {
'${p.basename(executable.relativePath)}-$versionSuffix.snapshot');
}

String incrementalDillPathOfExecutable(Executable executable) {
assert(p.isRelative(executable.relativePath));
return isGlobal
? p.join(_incrementalDillsPath,
'${p.basename(executable.relativePath)}.incremental.dill')
: p.join(_incrementalDillsPath, executable.package,
'${p.basename(executable.relativePath)}.incremental.dill');
}

/// The absolute path of [executable] resolved relative to [this].
String resolveExecutable(Executable executable) {
return p.join(
Expand Down
4 changes: 2 additions & 2 deletions lib/src/executable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Future<int> runExecutable(

entrypoint.migrateCache();

var snapshotPath = entrypoint.pathOfExecutable(executable);
var snapshotPath = entrypoint.snapshotPathOfExecutable(executable);

// Don't compile snapshots for mutable packages, since their code may
// change later on.
Expand Down Expand Up @@ -327,7 +327,7 @@ Future<String> getExecutableForCommand(
if (!allowSnapshot || entrypoint.packageGraph.isPackageMutable(package)) {
return p.relative(path, from: root);
} else {
final snapshotPath = entrypoint.pathOfExecutable(executable);
final snapshotPath = entrypoint.snapshotPathOfExecutable(executable);
if (!fileExists(snapshotPath)) {
await warningsOnlyUnlessTerminal(
() => entrypoint.precompileExecutable(executable),
Expand Down
5 changes: 3 additions & 2 deletions lib/src/global_packages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,8 @@ To recompile executables, first run `global deactivate ${dep.name}`.
deleteEntry(file);
_createBinStub(
entrypoint.root, p.basenameWithoutExtension(file), binStubScript,
overwrite: true, snapshot: entrypoint.pathOfExecutable(executable));
overwrite: true,
snapshot: entrypoint.snapshotPathOfExecutable(executable));
}
}
}
Expand Down Expand Up @@ -640,7 +641,7 @@ To recompile executables, first run `global deactivate ${dep.name}`.
executable,
script,
overwrite: overwriteBinStubs,
snapshot: entrypoint.pathOfExecutable(
snapshot: entrypoint.snapshotPathOfExecutable(
exec.Executable.adaptProgramName(package.name, script),
),
);
Expand Down
1 change: 0 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ dependencies:
cli_util: ^0.3.0
collection: ^1.15.0
crypto: ^3.0.1
frontend_server_client: ^2.0.0
http: ^0.13.3
http_multi_server: ^3.0.1
meta: ^1.3.0
Expand Down

0 comments on commit d2ad13d

Please sign in to comment.