Skip to content
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

isUpToDate based on package_config.json #4160

Merged
merged 11 commits into from
Mar 5, 2024
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
2 changes: 0 additions & 2 deletions lib/pub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ Command<int> pubCommand({required bool Function() isVerbose}) =>
Future<void> ensurePubspecResolved(
String dir, {
bool isOffline = false,
bool checkForSdkUpdate = false,
bool summaryOnly = true,
bool onlyOutputWhenTerminal = true,
}) async {
try {
await Entrypoint(dir, SystemCache(isOffline: isOffline)).ensureUpToDate(
checkForSdkUpdate: checkForSdkUpdate,
summaryOnly: summaryOnly,
onlyOutputWhenTerminal: onlyOutputWhenTerminal,
);
Expand Down
288 changes: 153 additions & 135 deletions lib/src/entrypoint.dart

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/src/executable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ Future<DartExecutableWithPackageConfig> getExecutableForCommand(
}
final entrypoint = Entrypoint(root, SystemCache(rootDir: pubCacheDir));
try {
await entrypoint.ensureUpToDate(checkForSdkUpdate: true);
await entrypoint.ensureUpToDate();
} on ApplicationException catch (e) {
throw CommandResolutionFailedException._(
e.toString(),
Expand Down
5 changes: 4 additions & 1 deletion lib/src/io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,10 @@ T _doProcess<T>(
}

/// Updates [path]'s modification time.
void touch(String path) => File(path).setLastModifiedSync(DateTime.now());
void touch(String path) {
log.fine('Touching `$path`');
File(path).setLastModifiedSync(DateTime.now());
}

/// Creates a temporary directory and passes its path to [fn].
///
Expand Down
52 changes: 32 additions & 20 deletions test/add/sdk/sdk_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ void main() {
}),
]).validate();

await d.appPackageConfigFile([
d.packageConfigEntry(
name: 'foo',
path: p.join(d.sandbox, 'flutter', 'packages', 'foo'),
),
d.packageConfigEntry(name: 'bar', version: '1.0.0'),
]).validate();
await d.appPackageConfigFile(
[
d.packageConfigEntry(
name: 'foo',
path: p.join(d.sandbox, 'flutter', 'packages', 'foo'),
),
d.packageConfigEntry(name: 'bar', version: '1.0.0'),
],
flutterRoot: p.join(d.sandbox, 'flutter'),
flutterVersion: '1.2.3',
).validate();
});

test(
Expand All @@ -73,13 +77,17 @@ void main() {
},
}),
]).validate();
await d.appPackageConfigFile([
d.packageConfigEntry(
name: 'foo',
path: p.join(d.sandbox, 'flutter', 'packages', 'foo'),
),
d.packageConfigEntry(name: 'bar', version: '1.0.0'),
]).validate();
await d.appPackageConfigFile(
[
d.packageConfigEntry(
name: 'foo',
path: p.join(d.sandbox, 'flutter', 'packages', 'foo'),
),
d.packageConfigEntry(name: 'bar', version: '1.0.0'),
],
flutterRoot: p.join(d.sandbox, 'flutter'),
flutterVersion: '1.2.3',
).validate();
});

test('adds an SDK dependency from bin/cache/pkg', () async {
Expand All @@ -89,12 +97,16 @@ void main() {
environment: {'FLUTTER_ROOT': p.join(d.sandbox, 'flutter')},
);

await d.appPackageConfigFile([
d.packageConfigEntry(
name: 'baz',
path: p.join(d.sandbox, 'flutter', 'bin', 'cache', 'pkg', 'baz'),
),
]).validate();
await d.appPackageConfigFile(
[
d.packageConfigEntry(
name: 'baz',
path: p.join(d.sandbox, 'flutter', 'bin', 'cache', 'pkg', 'baz'),
),
],
flutterRoot: p.join(d.sandbox, 'flutter'),
flutterVersion: '1.2.3',
).validate();
});

test("fails if the version constraint doesn't match", () async {
Expand Down
19 changes: 18 additions & 1 deletion test/descriptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,27 @@ DirectoryDescriptor appDir({Map? dependencies, Map<String, Object>? pubspec}) =>
Descriptor packageConfigFile(
List<PackageConfigEntry> packages, {
String generatorVersion = '3.1.2+3',
String? pubCache,
String? flutterVersion,
String? flutterRoot,
}) =>
PackageConfigFileDescriptor(packages, generatorVersion);
PackageConfigFileDescriptor(
packages,
generatorVersion,
pubCache ??
p.join(
sandbox,
cachePath,
),
flutterRoot,
flutterVersion,
);

Descriptor appPackageConfigFile(
List<PackageConfigEntry> packages, {
String generatorVersion = '3.1.2+3',
String? flutterRoot,
String? flutterVersion,
}) =>
dir(
appPath,
Expand All @@ -327,6 +342,8 @@ Descriptor appPackageConfigFile(
...packages,
],
generatorVersion: generatorVersion,
flutterRoot: flutterRoot,
flutterVersion: flutterVersion,
),
],
);
Expand Down
18 changes: 16 additions & 2 deletions test/descriptor/package_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import 'package:test_descriptor/test_descriptor.dart';
/// Describes a `.dart_tools/package_config.json` file and its contents.
class PackageConfigFileDescriptor extends Descriptor {
final String _generatorVersion;
final String _pubCache;
final String? _flutterRoot;
final String? _flutterVersion;

/// A map describing the packages in this `package_config.json` file.
final List<PackageConfigEntry> _packages;
Expand All @@ -26,14 +29,25 @@ class PackageConfigFileDescriptor extends Descriptor {
generatorVersion: Version.parse(_generatorVersion),
generator: 'pub',
generated: DateTime.now().toUtc(),
additionalProperties: {
'pubCache': p.toUri(_pubCache).toString(),
if (_flutterRoot != null)
'flutterRoot': p.toUri(_flutterRoot).toString(),
if (_flutterVersion != null) 'flutterVersion': _flutterVersion,
},
);
}

/// Describes a `.dart_tools/package_config.json` file
/// with the given list of package configurations and
/// generated with specified version.
PackageConfigFileDescriptor(this._packages, this._generatorVersion)
: super('.dart_tool/package_config.json');
PackageConfigFileDescriptor(
this._packages,
this._generatorVersion,
this._pubCache,
this._flutterRoot,
this._flutterVersion,
) : super('.dart_tool/package_config.json');

@override
Future<void> create([String? parent]) async {
Expand Down
49 changes: 10 additions & 39 deletions test/embedding/ensure_pubspec_resolved.dart
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,7 @@ void testEnsurePubspecResolved() {
// Ensure that the pubspec looks newer than the lockfile.
await _touch('pubspec.yaml');

await _implicitPubGet('The pubspec.yaml file has changed since the '
'pubspec.lock file was generated');
await _implicitPubGet('`pubspec.yaml` is newer than `pubspec.lock`');
});

test('the package_config.json file points to the wrong place', () async {
Expand Down Expand Up @@ -235,8 +234,7 @@ void testEnsurePubspecResolved() {
// Ensure that the pubspec looks newer than the lockfile.
await _touch('pubspec.lock');

await _implicitPubGet('The pubspec.lock file has changed since the '
'.dart_tool/package_config.json file was generated');
await _implicitPubGet('Could not find `../foo/pubspec.yaml`');
});

test("the lock file's SDK constraint doesn't match the current SDK",
Expand Down Expand Up @@ -270,8 +268,8 @@ void testEnsurePubspecResolved() {
);

await _implicitPubGet(
"Dart 3.1.2+3 is incompatible with your dependencies' "
'SDK constraints');
'The Dart SDK was updated since last package resolution.',
);
});

test(
Expand Down Expand Up @@ -314,7 +312,7 @@ void testEnsurePubspecResolved() {
);

await _implicitPubGet(
'Flutter 0.9.0 is incompatible with your dependencies\' SDK constraints',
'Flutter has updated since last invocation.',
environment: {'FLUTTER_ROOT': p.join(d.sandbox, 'flutter')},
);
});
Expand All @@ -340,6 +338,9 @@ void testEnsurePubspecResolved() {
d.libPubspec('bar', '1.0.0', deps: {'foo': '2.0.0'}),
]).create();

// To ensure the timestamp is strictly later we need to touch again here.
await _touch(p.join(d.sandbox, 'bar', 'pubspec.yaml'));

await _implicitPubGet('../bar/pubspec.yaml has changed '
'since the pubspec.lock file was generated.');
});
Expand Down Expand Up @@ -377,6 +378,8 @@ void testEnsurePubspecResolved() {
sdk: '>= 2.100.0 <=4.0.0', // tests runs with '3.1.2+3'
),
]).create();
// To ensure the timestamp is strictly later we need to touch again here.
await _touch(p.join(d.sandbox, 'bar', 'pubspec.yaml'));

await _implicitPubGet('../bar/pubspec.yaml has changed '
'since the pubspec.lock file was generated.');
Expand Down Expand Up @@ -497,38 +500,6 @@ void testEnsurePubspecResolved() {

await _noImplicitPubGet();
});

test('the lock file has a Flutter SDK but Flutter is unavailable',
() async {
// Avoid using a path dependency because it triggers the full validation
// logic. We want to be sure SDK-validation works without that logic.
server.serve(
'foo',
'3.0.0',
pubspec: {
'environment': {
'flutter': '>=1.0.0 <2.0.0',
'sdk': defaultSdkConstraint,
},
},
);

await d.dir('flutter', [d.flutterVersion('1.2.3')]).create();

await d.dir(appPath, [
d.appPubspec(dependencies: {'foo': '3.0.0'}),
]).create();

await pubGet(
environment: {'FLUTTER_ROOT': p.join(d.sandbox, 'flutter')},
);

await d.dir('flutter', [d.flutterVersion('2.4.6')]).create();

// Run pub manually here because otherwise we don't have access to
// d.sandbox.
await runPub(args: ['run', 'bin/script.dart']);
});
});
});
}
Expand Down
40 changes: 26 additions & 14 deletions test/sdk_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,17 @@ void main() {
command,
environment: {'FLUTTER_ROOT': p.join(d.sandbox, 'flutter')},
);
await d.appPackageConfigFile([
d.packageConfigEntry(
name: 'foo',
path: p.join(d.sandbox, 'flutter', 'packages', 'foo'),
),
d.packageConfigEntry(name: 'bar', version: '1.0.0'),
]).validate();
await d.appPackageConfigFile(
[
d.packageConfigEntry(
name: 'foo',
path: p.join(d.sandbox, 'flutter', 'packages', 'foo'),
),
d.packageConfigEntry(name: 'bar', version: '1.0.0'),
],
flutterRoot: p.join(d.sandbox, 'flutter'),
flutterVersion: '1.2.3',
).validate();
});

test('gets an SDK dependency from bin/cache/pkg', () async {
Expand All @@ -63,12 +67,16 @@ void main() {
environment: {'FLUTTER_ROOT': p.join(d.sandbox, 'flutter')},
);

await d.appPackageConfigFile([
d.packageConfigEntry(
name: 'baz',
path: p.join(d.sandbox, 'flutter', 'bin', 'cache', 'pkg', 'baz'),
),
]).validate();
await d.appPackageConfigFile(
[
d.packageConfigEntry(
name: 'baz',
path: p.join(d.sandbox, 'flutter', 'bin', 'cache', 'pkg', 'baz'),
),
],
flutterRoot: p.join(d.sandbox, 'flutter'),
flutterVersion: '1.2.3',
).validate();
});

test('unlocks an SDK dependency when the version changes', () async {
Expand Down Expand Up @@ -116,7 +124,11 @@ void main() {
command,
environment: {'FLUTTER_ROOT': p.join(d.sandbox, 'flutter')},
);
await d.appPackageConfigFile([]).validate();
await d.appPackageConfigFile(
[],
flutterRoot: p.join(d.sandbox, 'flutter'),
flutterVersion: '1.2.3',
).validate();
});

group('fails if', () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ Got dependencies in myapp/example.

## Section 10
$ pub run -C myapp 'bin/app.dart'
Resolving dependencies in myapp...
Got dependencies in myapp.
Resolving dependencies in myapp...
Got dependencies in myapp.
Building package executable...
Built test_pkg:app.
Hi
Expand Down Expand Up @@ -139,6 +143,8 @@ $ pub uploader -C myapp add [email protected]

## Section 13
$ pub deps -C myapp
Resolving dependencies in myapp...
Got dependencies in myapp.
Dart SDK 3.1.2+3
test_pkg 1.0.0
└── foo 1.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ MSG : Logs written to $SANDBOX/cache/log/pub_log.txt.
[E] | ],
[E] | "generated": "$TIME",
[E] | "generator": "pub",
[E] | "generatorVersion": "3.1.2+3"
[E] | "generatorVersion": "3.1.2+3",
[E] | "pubCache": "file://$SANDBOX/cache"
[E] | }
[E] IO : Writing $N characters to text file $SANDBOX/cache/log/pub_log.txt.

Expand Down Expand Up @@ -283,7 +284,8 @@ FINE: Contents:
| ],
| "generated": "$TIME",
| "generator": "pub",
| "generatorVersion": "3.1.2+3"
| "generatorVersion": "3.1.2+3",
| "pubCache": "file://$SANDBOX/cache"
| }
---- End log transcript ----
-------------------------------- END OF OUTPUT ---------------------------------
Expand Down
Loading