Skip to content

Stop depending on .packages #2764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions lib/src/dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,36 +38,35 @@ bool isEntrypoint(CompilationUnit dart) {
/// Snapshots the Dart executable at [executablePath] to a snapshot at
/// [snapshotPath].
///
/// If [packagesFile] 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 [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, {
Uri packagesFile,
String packageConfigFile,
String name,
}) async {
name = log.bold(name ?? executablePath.toString());

var args = ['--snapshot=$snapshotPath', p.toUri(executablePath).toString()];

if (packagesFile != null) {
// Resolve [packagesFile] in case it's relative to work around sdk#33177.
args.insert(0, '--packages=${Uri.base.resolveUri(packagesFile)}');
}

var result = await runProcess(Platform.executable, args);
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 $name.');
log.message('Precompiled $highlightedName.');
} else {
// Don't leave partial results.
deleteEntry(snapshotPath);

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

Expand Down
38 changes: 20 additions & 18 deletions lib/src/entrypoint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ class Entrypoint {
/// The package graph for the application and all of its transitive
/// dependencies.
///
/// Throws a [DataError] if the `.packages` file isn't up-to-date relative to
/// the pubspec and the lockfile.
/// Throws a [DataError] if the `.dart_tool/package_config.json` file isn't
/// up-to-date relative to the pubspec and the lockfile.
PackageGraph get packageGraph {
if (_packageGraph != null) return _packageGraph;

Expand All @@ -127,10 +127,11 @@ class Entrypoint {
String get _configRoot =>
isCached ? p.join(cache.rootDir, 'global_packages', root.name) : root.dir;

/// The path to the entrypoint's "packages" directory.
String get packagesPath => root.path('packages');

/// The path to the entrypoint's ".packages" file.
///
/// This file is being slowly deprecated in favor of
/// `.dart_tool/package_config.json`. Pub will still create it, but will
/// not require it or make use of it within pub.
String get packagesFile => p.join(_configRoot, '.packages');

/// The path to the entrypoint's ".dart_tool/package_config.json" file.
Expand Down Expand Up @@ -353,7 +354,7 @@ class Entrypoint {
final package = executable.package;
await dart.snapshot(
resolveExecutable(executable), snapshotPathOfExecutable(executable),
packagesFile: p.toUri(packagesFile),
packageConfigFile: packageConfigFile,
name:
'$package:${p.basenameWithoutExtension(executable.relativePath)}');
}
Expand Down Expand Up @@ -436,9 +437,11 @@ class Entrypoint {
});
}

/// Throws a [DataError] if the `.packages` file or the
/// `.dart_tool/package_config.json` file doesn't exist or if it's out-of-date
/// relative to the lockfile or the pubspec.
/// Throws a [DataError] if the `.dart_tool/package_config.json` file doesn't
/// exist or if it's out-of-date relative to the lockfile or the pubspec.
///
/// A `.packages` file is not required. But if it exists it is checked for
/// consistency with the pubspec.lock.
void assertUpToDate() {
if (isCached) return;

Expand All @@ -453,9 +456,6 @@ class Entrypoint {
'resolution of package import URIs; run "pub get" to generate it.',
);
}
if (!entryExists(packagesFile)) {
dataError('No .packages file found, please run "pub get" first.');
}

// Manually parse the lockfile because a full YAML parse is relatively slow
// and this is on the hot path for "pub run".
Expand Down Expand Up @@ -483,12 +483,14 @@ class Entrypoint {
}
}

var packagesModified = File(packagesFile).lastModifiedSync();
if (packagesModified.isBefore(lockFileModified)) {
_checkPackagesFileUpToDate();
touch(packagesFile);
} else if (touchedLockFile) {
touch(packagesFile);
if (fileExists(packagesFile)) {
var packagesModified = File(packagesFile).lastModifiedSync();
if (packagesModified.isBefore(lockFileModified)) {
_checkPackagesFileUpToDate();
touch(packagesFile);
} else if (touchedLockFile) {
touch(packagesFile);
}
}

var packageConfigModified = File(packageConfigFile).lastModifiedSync();
Expand Down
11 changes: 3 additions & 8 deletions lib/src/executable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,17 @@ List<String> vmArgsFromArgResults(ArgResults argResults) {
///
/// If [enableAsserts] is true, the program is run with assertions enabled.
///
/// If [packagesFile] is passed, it's used as the package config file path for
/// the executable. Otherwise, `entrypoint.packagesFile` is used.
///
/// If the executable is in an immutable package and we pass no [vmArgs], it
/// run from snapshot (and precompiled if the snapshot doesn't already exist).
///
/// Returns the exit code of the spawned app.
Future<int> runExecutable(
Entrypoint entrypoint, Executable executable, Iterable<String> args,
{bool enableAsserts = false,
String packagesFile,
Future<void> Function(Executable) recompile,
List<String> vmArgs = const [],
@required bool alwaysUseSubprocess}) async {
final package = executable.package;
packagesFile ??= entrypoint.packagesFile;

// Make sure the package is an immediate dependency of the entrypoint or the
// entrypoint itself.
Expand Down Expand Up @@ -122,13 +117,13 @@ Future<int> runExecutable(
// helpful for the subprocess to be able to spawn Dart with
// Platform.executableArguments and have that work regardless of the working
// directory.
var packageConfig = p.absolute(packagesFile);
final packageConfigAbsolute = p.absolute(entrypoint.packageConfigFile);

try {
return await _runDartProgram(
executablePath,
args,
packageConfig,
packageConfigAbsolute,
enableAsserts: enableAsserts,
vmArgs: vmArgs,
alwaysUseSubprocess: alwaysUseSubprocess,
Expand All @@ -144,7 +139,7 @@ Future<int> runExecutable(
return await _runDartProgram(
executablePath,
args,
packageConfig,
packageConfigAbsolute,
enableAsserts: enableAsserts,
vmArgs: vmArgs,
alwaysUseSubprocess: alwaysUseSubprocess,
Expand Down
2 changes: 0 additions & 2 deletions lib/src/global_packages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,6 @@ class GlobalPackages {
Future<int> runExecutable(
Entrypoint entrypoint, exec.Executable executable, Iterable<String> args,
{bool enableAsserts = false,
String packagesFile,
Future<void> Function(exec.Executable) recompile,
List<String> vmArgs = const [],
@required bool alwaysUseSubprocess}) async {
Expand All @@ -382,7 +381,6 @@ class GlobalPackages {
executable,
args,
enableAsserts: enableAsserts,
packagesFile: packagesFile,
recompile: (exectuable) async {
await recompile(exectuable);
_refreshBinStubs(entrypoint, executable);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/lock_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class LockFile {
/// Returns the contents of the `.dart_tools/package_config` file generated
/// from this lockfile.
///
/// This file is planned to eventually replace the `.packages` file.
/// This file will replace the `.packages` file.
///
/// If [entrypoint] is passed, an accompanying [entrypointSdkConstraint]
/// should be given, these identify the current package in which this file is
Expand Down
4 changes: 2 additions & 2 deletions test/downgrade/doesnt_change_git_dependencies_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ void main() {

await pubGet();

var originalFooSpec = packageSpecLine('foo');
var originalFooSpec = packageSpec('foo');

await d.git('foo.git',
[d.libDir('foo', 'foo 2'), d.libPubspec('foo', '1.0.0')]).commit();

await pubDowngrade();

expect(packageSpecLine('foo'), originalFooSpec);
expect(packageSpec('foo'), originalFooSpec);
});
}
4 changes: 2 additions & 2 deletions test/get/git/check_out_and_upgrade_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void main() {
])
]).validate();

var originalFooSpec = packageSpecLine('foo');
var originalFooSpec = packageSpec('foo');

await d.git('foo.git',
[d.libDir('foo', 'foo 2'), d.libPubspec('foo', '1.0.0')]).commit();
Expand All @@ -53,6 +53,6 @@ void main() {
])
]).validate();

expect(packageSpecLine('foo'), isNot(originalFooSpec));
expect(packageSpec('foo'), isNot(originalFooSpec));
});
}
2 changes: 1 addition & 1 deletion test/get/git/check_out_revision_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ void main() {
])
]).validate();

expect(packageSpecLine('foo'), contains(commit));
expect(packageSpec('foo')['rootUri'], contains(commit));
});
}
4 changes: 2 additions & 2 deletions test/get/git/check_out_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void main() {
])
]).validate();

expect(packageSpecLine('foo'), isNotNull);
expect(packageSpec('foo'), isNotNull);
}, skip: true);

test(
Expand Down Expand Up @@ -65,7 +65,7 @@ void main() {
])
]).validate();

expect(packageSpecLine('foo'), isNotNull);
expect(packageSpec('foo'), isNotNull);
});
}

Expand Down
4 changes: 2 additions & 2 deletions test/get/git/check_out_transitive_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void main() {
])
]).validate();

expect(packageSpecLine('foo'), isNotNull);
expect(packageSpecLine('bar'), isNotNull);
expect(packageSpec('foo'), isNotNull);
expect(packageSpec('bar'), isNotNull);
});
}
2 changes: 1 addition & 1 deletion test/get/git/check_out_twice_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void main() {
])
]).validate();

expect(packageSpecLine('foo'), isNotNull);
expect(packageSpec('foo'), isNotNull);

// Verify that nothing breaks if we get a Git revision that's already
// in the cache.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void main() {

await pubGet();

var originalFooSpec = packageSpecLine('foo');
var originalFooSpec = packageSpec('foo');

// Switch to a new cache.
renameInSandbox(cachePath, '$cachePath.old');
Expand All @@ -56,6 +56,6 @@ void main() {
])
]).validate();

expect(packageSpecLine('foo'), isNot(originalFooSpec));
expect(packageSpec('foo'), isNot(originalFooSpec));
});
}
2 changes: 1 addition & 1 deletion test/get/git/different_repo_name_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ void main() {

await pubGet();

expect(packageSpecLine('weirdname'), contains('foo'));
expect(packageSpec('weirdname')['rootUri'], contains('foo'));
});
}
4 changes: 2 additions & 2 deletions test/get/git/doesnt_fetch_if_nothing_changes_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ void main() {

await pubGet();

var originalFooSpec = packageSpecLine('foo');
var originalFooSpec = packageSpec('foo');

// Delete the repo. This will cause "pub get" to fail if it tries to
// re-fetch.
deleteEntry(p.join(d.sandbox, 'foo.git'));

await pubGet();

expect(packageSpecLine('foo'), originalFooSpec);
expect(packageSpec('foo'), originalFooSpec);
});
}
6 changes: 3 additions & 3 deletions test/get/git/lock_version_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ void main() {
])
]).validate();

var originalFooSpec = packageSpecLine('foo');
var originalFooSpec = packageSpec('foo');

// Delete the package spec to simulate a new checkout of the application.
deleteEntry(path.join(d.sandbox, packagesFilePath));
deleteEntry(path.join(d.sandbox, packageConfigFilePath));

await d.git('foo.git',
[d.libDir('foo', 'foo 2'), d.libPubspec('foo', '1.0.0')]).commit();

// This get shouldn't upgrade the foo.git dependency due to the lockfile.
await pubGet();

expect(packageSpecLine('foo'), originalFooSpec);
expect(packageSpec('foo'), originalFooSpec);
});
}
6 changes: 3 additions & 3 deletions test/get/git/locked_revision_without_repo_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ void main() {
])
]).validate();

var originalFooSpec = packageSpecLine('foo');
var originalFooSpec = packageSpec('foo');

// Delete the package spec and the cache to simulate a brand new checkout
// of the application.
deleteEntry(path.join(d.sandbox, packagesFilePath));
deleteEntry(path.join(d.sandbox, packageConfigFilePath));
deleteEntry(path.join(d.sandbox, cachePath));

await d.git('foo.git',
Expand All @@ -48,6 +48,6 @@ void main() {
// This get shouldn't upgrade the foo.git dependency due to the lockfile.
await pubGet();

expect(packageSpecLine('foo'), originalFooSpec);
expect(packageSpec('foo'), originalFooSpec);
});
}
4 changes: 2 additions & 2 deletions test/get/git/stay_locked_if_compatible_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void main() {

await pubGet();

var originalFooSpec = packageSpecLine('foo');
var originalFooSpec = packageSpec('foo');

await d.git('foo.git',
[d.libDir('foo', 'foo 1.0.1'), d.libPubspec('foo', '1.0.1')]).commit();
Expand All @@ -33,6 +33,6 @@ void main() {

await pubGet();

expect(packageSpecLine('foo'), originalFooSpec);
expect(packageSpec('foo'), originalFooSpec);
});
}
Loading