@@ -11,14 +11,22 @@ import 'package:modular_test/src/io_pipeline.dart';
1111import 'package:modular_test/src/pipeline.dart' ;
1212import 'package:modular_test/src/suite.dart' ;
1313import 'package:modular_test/src/runner.dart' ;
14+ import 'package:package_config/package_config.dart' ;
1415
16+ String packageConfigJsonPath = '.dart_tool/package_config.json' ;
1517Uri sdkRoot = Platform .script.resolve ('../../../' );
18+ Uri packageConfigUri = sdkRoot.resolve (packageConfigJsonPath);
1619Options _options;
1720String _dartdevcScript;
1821String _kernelWorkerScript;
1922
23+ // TODO(joshualitt): Figure out a way to support package configs in
24+ // tests/modular.
25+ PackageConfig _packageConfig;
26+
2027void 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');
3543const jsId = DataId ('js' );
3644const 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+
3857class SourceToSummaryDillStep implements IOModularStep {
3958 @override
4059 List <DataId > get resultData => const [dillId];
@@ -301,30 +320,51 @@ String get _d8executable {
301320
302321Future <void > _createPackagesFile (
303322 Module module, Uri root, Set <Module > transitiveDependencies) async {
304- // We create a .packages file which defines the location of this module if
305- // it is a package. The CFE requires that if a `package:` URI of a
306- // dependency is used in an import, then we need that package entry in the
307- // .packages file. However, after it checks that the definition exists, the
308- // CFE will not actually use the resolved URI if a library for the import
309- // URI is already found in one of the provided .dill files of the
310- // dependencies. For that reason, and to ensure that a step only has access
311- // to the files provided in a module, we generate a .packages with invalid
312- // folders for other packages.
323+ // We create both a .packages and package_config.json file which defines
324+ // the location of this module if it is a package. The CFE requires that
325+ // if a `package:` URI of a dependency is used in an import, then we need
326+ // that package entry in the associated file. However, after it checks that
327+ // the definition exists, the CFE will not actually use the resolved URI if
328+ // a library for the import URI is already found in one of the provide
329+ // .dill files of the dependencies. For that reason, and to ensure that
330+ // a step only has access to the files provided in a module, we generate a
331+ // config file with invalid folders for other packages.
313332 // TODO(sigmund): follow up with the CFE to see if we can remove the need
314- // for the .packages entry altogether if they won't need to read the
315- // sources.
333+ // for these dummy entries..
334+ // TODO(joshualitt): Generate just the json file.
335+ var packagesJson = [];
316336 var packagesContents = StringBuffer ();
317337 if (module.isPackage) {
318338 packagesContents.write ('${module .name }:${module .packageBase }\n ' );
339+ packagesJson.add (_packageConfigEntry (
340+ module.name, Uri .parse ('../${module .packageBase }' )));
319341 }
320342 var unusedNum = 0 ;
321343 for (var dependency in transitiveDependencies) {
322344 if (dependency.isPackage) {
345+ // rootUri should be ignored for dependent modules, so we pass in a
346+ // bogus value.
347+ var rootUri = Uri .parse ('unused$unusedNum ' );
323348 unusedNum++ ;
324- packagesContents.write ('${dependency .name }:unused$unusedNum \n ' );
349+ var dependentPackage = _packageConfig[dependency.name];
350+ var packageJson = dependentPackage == null
351+ ? _packageConfigEntry (dependency.name, rootUri)
352+ : _packageConfigEntry (dependentPackage.name, rootUri,
353+ version: dependentPackage.languageVersion);
354+ packagesJson.add (packageJson);
355+ packagesContents.write ('${dependency .name }:$rootUri \n ' );
325356 }
326357 }
327358
359+ if (module.isPackage) {
360+ await File .fromUri (root.resolve (packageConfigJsonPath))
361+ .create (recursive: true );
362+ await File .fromUri (root.resolve (packageConfigJsonPath)).writeAsString ('{'
363+ ' "configVersion": ${_packageConfig .version },'
364+ ' "packages": [ ${packagesJson .join (',' )} ]'
365+ '}' );
366+ }
367+
328368 await File .fromUri (root.resolve ('.packages' ))
329369 .writeAsString ('$packagesContents ' );
330370}
0 commit comments