Skip to content

Commit f42889f

Browse files
jakemac53commit-bot@chromium.org
authored andcommitted
create package_config.json files in ddc modular test suite
Copies the same changes as were added for dart2js here https://dart-review.googlesource.com/c/sdk/+/152361/9/pkg/compiler/tool/modular_test_suite.dart Change-Id: I785b275c62c3d3afb9f37c0b0c8c4e4369b4ccf3 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/153362 Auto-Submit: Jake Macdonald <[email protected]> Commit-Queue: Jake Macdonald <[email protected]> Commit-Queue: Sigmund Cherem <[email protected]> Reviewed-by: Sigmund Cherem <[email protected]>
1 parent b4c60e6 commit f42889f

File tree

1 file changed

+52
-12
lines changed

1 file changed

+52
-12
lines changed

pkg/dev_compiler/test/modular_suite.dart

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,22 @@ import 'package:modular_test/src/io_pipeline.dart';
1111
import 'package:modular_test/src/pipeline.dart';
1212
import 'package:modular_test/src/suite.dart';
1313
import 'package:modular_test/src/runner.dart';
14+
import 'package:package_config/package_config.dart';
1415

16+
String packageConfigJsonPath = '.dart_tool/package_config.json';
1517
Uri sdkRoot = Platform.script.resolve('../../../');
18+
Uri packageConfigUri = sdkRoot.resolve(packageConfigJsonPath);
1619
Options _options;
1720
String _dartdevcScript;
1821
String _kernelWorkerScript;
1922

23+
// TODO(joshualitt): Figure out a way to support package configs in
24+
// tests/modular.
25+
PackageConfig _packageConfig;
26+
2027
void main(List<String> args) async {
2128
_options = Options.parse(args);
29+
_packageConfig = await loadPackageConfigUri(packageConfigUri);
2230
await _resolveScripts();
2331
await runSuite(
2432
sdkRoot.resolve('tests/modular/'),
@@ -35,6 +43,17 @@ const dillId = DataId('dill');
3543
const jsId = DataId('js');
3644
const txtId = DataId('txt');
3745

46+
String _packageConfigEntry(String name, Uri root,
47+
{Uri packageRoot, LanguageVersion version}) {
48+
var fields = [
49+
'"name": "${name}"',
50+
'"rootUri": "$root"',
51+
if (packageRoot != null) '"packageUri": "$packageRoot"',
52+
if (version != null) '"languageVersion": "$version"'
53+
];
54+
return '{${fields.join(',')}}';
55+
}
56+
3857
class SourceToSummaryDillStep implements IOModularStep {
3958
@override
4059
List<DataId> get resultData => const [dillId];
@@ -298,30 +317,51 @@ String get _d8executable {
298317

299318
Future<void> _createPackagesFile(
300319
Module module, Uri root, Set<Module> transitiveDependencies) async {
301-
// We create a .packages file which defines the location of this module if
302-
// it is a package. The CFE requires that if a `package:` URI of a
303-
// dependency is used in an import, then we need that package entry in the
304-
// .packages file. However, after it checks that the definition exists, the
305-
// CFE will not actually use the resolved URI if a library for the import
306-
// URI is already found in one of the provided .dill files of the
307-
// dependencies. For that reason, and to ensure that a step only has access
308-
// to the files provided in a module, we generate a .packages with invalid
309-
// folders for other packages.
320+
// We create both a .packages and package_config.json file which defines
321+
// the location of this module if it is a package. The CFE requires that
322+
// if a `package:` URI of a dependency is used in an import, then we need
323+
// that package entry in the associated file. However, after it checks that
324+
// the definition exists, the CFE will not actually use the resolved URI if
325+
// a library for the import URI is already found in one of the provide
326+
// .dill files of the dependencies. For that reason, and to ensure that
327+
// a step only has access to the files provided in a module, we generate a
328+
// config file with invalid folders for other packages.
310329
// TODO(sigmund): follow up with the CFE to see if we can remove the need
311-
// for the .packages entry altogether if they won't need to read the
312-
// sources.
330+
// for these dummy entries..
331+
// TODO(joshualitt): Generate just the json file.
332+
var packagesJson = [];
313333
var packagesContents = StringBuffer();
314334
if (module.isPackage) {
315335
packagesContents.write('${module.name}:${module.packageBase}\n');
336+
packagesJson.add(_packageConfigEntry(
337+
module.name, Uri.parse('../${module.packageBase}')));
316338
}
317339
var unusedNum = 0;
318340
for (var dependency in transitiveDependencies) {
319341
if (dependency.isPackage) {
342+
// rootUri should be ignored for dependent modules, so we pass in a
343+
// bogus value.
344+
var rootUri = Uri.parse('unused$unusedNum');
320345
unusedNum++;
321-
packagesContents.write('${dependency.name}:unused$unusedNum\n');
346+
var dependentPackage = _packageConfig[dependency.name];
347+
var packageJson = dependentPackage == null
348+
? _packageConfigEntry(dependency.name, rootUri)
349+
: _packageConfigEntry(dependentPackage.name, rootUri,
350+
version: dependentPackage.languageVersion);
351+
packagesJson.add(packageJson);
352+
packagesContents.write('${dependency.name}:$rootUri\n');
322353
}
323354
}
324355

356+
if (module.isPackage) {
357+
await File.fromUri(root.resolve(packageConfigJsonPath))
358+
.create(recursive: true);
359+
await File.fromUri(root.resolve(packageConfigJsonPath)).writeAsString('{'
360+
' "configVersion": ${_packageConfig.version},'
361+
' "packages": [ ${packagesJson.join(',')} ]'
362+
'}');
363+
}
364+
325365
await File.fromUri(root.resolve('.packages'))
326366
.writeAsString('$packagesContents');
327367
}

0 commit comments

Comments
 (0)