Skip to content

Commit f4b19b8

Browse files
joshualittcommit-bot@chromium.org
authored andcommitted
[dart2js] Support package json files in modular_test_suite.
Change-Id: I6a17b9604d2a3e01d822a4872a2560f0aff0a63c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/152361 Commit-Queue: Joshua Litt <[email protected]> Reviewed-by: Sigmund Cherem <[email protected]>
1 parent 1a27811 commit f4b19b8

File tree

1 file changed

+54
-12
lines changed

1 file changed

+54
-12
lines changed

pkg/compiler/tool/modular_test_suite.dart

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,21 @@ import 'package:modular_test/src/io_pipeline.dart';
1414
import 'package:modular_test/src/pipeline.dart';
1515
import 'package:modular_test/src/suite.dart';
1616
import 'package:modular_test/src/runner.dart';
17+
import 'package:package_config/package_config.dart';
1718

19+
String packageConfigJsonPath = ".dart_tool/package_config.json";
1820
Uri sdkRoot = Platform.script.resolve("../../../");
21+
Uri packageConfigUri = sdkRoot.resolve(packageConfigJsonPath);
1922
Options _options;
2023
String _dart2jsScript;
2124
String _kernelWorkerScript;
25+
26+
// TODO(joshualitt): Figure out a way to support package configs in
27+
// tests/modular.
28+
PackageConfig _packageConfig;
2229
main(List<String> args) async {
2330
_options = Options.parse(args);
31+
_packageConfig = await loadPackageConfigUri(packageConfigUri);
2432
await _resolveScripts();
2533
await runSuite(
2634
sdkRoot.resolve('tests/modular/'),
@@ -45,6 +53,17 @@ const codeId1 = const ShardDataId(codeId, 1);
4553
const jsId = const DataId("js");
4654
const txtId = const DataId("txt");
4755

56+
String _packageConfigEntry(String name, Uri root,
57+
{Uri packageRoot, LanguageVersion version}) {
58+
var fields = [
59+
'"name": "${name}"',
60+
'"rootUri": "$root"',
61+
if (packageRoot != null) '"packageUri": "$packageRoot"',
62+
if (version != null) '"languageVersion": "$version"'
63+
];
64+
return '{${fields.join(',')}}';
65+
}
66+
4867
// Step that compiles sources in a module to a .dill file.
4968
class SourceToDillStep implements IOModularStep {
5069
@override
@@ -88,31 +107,54 @@ class SourceToDillStep implements IOModularStep {
88107
}
89108
}
90109

91-
// We create a .packages file which defines the location of this module if
92-
// it is a package. The CFE requires that if a `package:` URI of a
93-
// dependency is used in an import, then we need that package entry in the
94-
// .packages file. However, after it checks that the definition exists, the
95-
// CFE will not actually use the resolved URI if a library for the import
96-
// URI is already found in one of the provided .dill files of the
97-
// dependencies. For that reason, and to ensure that a step only has access
98-
// to the files provided in a module, we generate a .packages with invalid
99-
// folders for other packages.
110+
// We create both a .packages and package_config.json file which defines
111+
// the location of this module if it is a package. The CFE requires that
112+
// if a `package:` URI of a dependency is used in an import, then we need
113+
// that package entry in the associated file. However, after it checks that
114+
// the definition exists, the CFE will not actually use the resolved URI if
115+
// a library for the import URI is already found in one of the provide
116+
// .dill files of the dependencies. For that reason, and to ensure that
117+
// a step only has access to the files provided in a module, we generate a
118+
// config file with invalid folders for other packages.
100119
// TODO(sigmund): follow up with the CFE to see if we can remove the need
101-
// for the .packages entry altogether if they won't need to read the
102-
// sources.
120+
// for these dummy entries..
121+
// TODO(joshualitt): Generate just the json file.
122+
var packagesJson = [];
103123
var packagesContents = new StringBuffer();
104124
if (module.isPackage) {
105125
packagesContents.write('${module.name}:${module.packageBase}\n');
126+
packagesJson.add(_packageConfigEntry(
127+
module.name, Uri.parse('../${module.packageBase}')));
106128
}
129+
107130
Set<Module> transitiveDependencies = computeTransitiveDependencies(module);
108131
int unusedNum = 0;
109132
for (Module dependency in transitiveDependencies) {
110133
if (dependency.isPackage) {
134+
// rootUri should be ignored for dependent modules, so we pass in a
135+
// bogus value.
136+
var rootUri = Uri.parse('unused$unusedNum');
111137
unusedNum++;
112-
packagesContents.write('${dependency.name}:unused$unusedNum\n');
138+
139+
var dependentPackage = _packageConfig[dependency.name];
140+
var packageJson = dependentPackage == null
141+
? _packageConfigEntry(dependency.name, rootUri)
142+
: _packageConfigEntry(dependentPackage.name, rootUri,
143+
version: dependentPackage.languageVersion);
144+
packagesJson.add(packageJson);
145+
packagesContents.write('${dependency.name}:$rootUri\n');
113146
}
114147
}
115148

149+
if (module.isPackage) {
150+
await File.fromUri(root.resolve(packageConfigJsonPath))
151+
.create(recursive: true);
152+
await File.fromUri(root.resolve(packageConfigJsonPath)).writeAsString('{'
153+
' "configVersion": ${_packageConfig.version},'
154+
' "packages": [ ${packagesJson.join(',')} ]'
155+
'}');
156+
}
157+
116158
await File.fromUri(root.resolve('.packages'))
117159
.writeAsString('$packagesContents');
118160

0 commit comments

Comments
 (0)