Skip to content

Update builds for SDK assets in tests #2653

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
145 changes: 28 additions & 117 deletions test_common/lib/sdk_asset_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import 'package:logging/logging.dart';
import 'package:path/path.dart' as p;
import 'package:test_common/test_sdk_layout.dart';

/// Generates sdk.js, sdk.map, sdk full dill, and sdk summary files.
///
/// Generates following missing assets if needed:
/// - js, source map, full dill.
/// Generates sdk.js, sdk.map, files.

class SdkAssetGenerator {
bool _sdkAssetsGenerated = false;
Expand All @@ -37,58 +34,43 @@ class SdkAssetGenerator {
if (!_sdkAssetsGenerated) {
_sdkAssetsGenerated = true;

// SDK contains sound summary, but SDK js and full dill are
// normally generated by setup tools and their builds,
// SDK full and outline .dill files are shipped with the SDK,
// but the JavaScript and sourcemaps are generated by other tooling
// i.e. flutter SDK or build_web_compilers.
// Generate missing files for tests if needed.
await _generateSdkJavaScript(
canaryFeatures: canaryFeatures,
);

// SDK does not contain any weak assets, generate them.
await _generateSdkJavaScript(
canaryFeatures: canaryFeatures,
);
await _generateSdkSummary();
await _generateSdkJavaScript(canaryFeatures: canaryFeatures);
}
}

String resolveSdkJsPath({
required bool canaryFeatures,
}) =>
String resolveSdkJsPath({required bool canaryFeatures}) =>
switch (ddcModuleFormat) {
ModuleFormat.amd => sdkLayout.amdJsPath,
ModuleFormat.ddc => sdkLayout.ddcJsPath,
_ => throw Exception('Unsupported DDC module format $ddcModuleFormat.')
_ => throw Exception('Unsupported DDC module format $ddcModuleFormat.'),
};

String resolveSdkSourcemapPath({
required bool canaryFeatures,
}) =>
String resolveSdkSourcemapPath({required bool canaryFeatures}) =>
switch (ddcModuleFormat) {
ModuleFormat.amd => sdkLayout.amdJsMapPath,
ModuleFormat.ddc => sdkLayout.ddcJsMapPath,
_ => throw Exception('Unsupported DDC module format $ddcModuleFormat.')
_ => throw Exception('Unsupported DDC module format $ddcModuleFormat.'),
};

String resolveSdkJsFilename({
required bool canaryFeatures,
}) =>
String resolveSdkJsFilename({required bool canaryFeatures}) =>
switch (ddcModuleFormat) {
ModuleFormat.amd => sdkLayout.amdJsFileName,
ModuleFormat.ddc => sdkLayout.ddcJsFileName,
_ => throw Exception('Unsupported DDC module format $ddcModuleFormat.')
_ => throw Exception('Unsupported DDC module format $ddcModuleFormat.'),
};

Future<void> _generateSdkJavaScript({
required bool canaryFeatures,
}) async {
Future<void> _generateSdkJavaScript({required bool canaryFeatures}) async {
Directory? outputDir;
try {
// Files to copy generated files to.
final outputJsPath = resolveSdkJsPath(canaryFeatures: canaryFeatures);
final outputJsMapPath =
resolveSdkSourcemapPath(canaryFeatures: canaryFeatures);
final outputJsMapPath = resolveSdkSourcemapPath(
canaryFeatures: canaryFeatures,
);
final outputFullDillPath = sdkLayout.fullDillPath;

final hasJsAsset = _exists(outputJsPath);
Expand All @@ -104,11 +86,12 @@ class SdkAssetGenerator {

// Files to generate
final jsPath = p.join(
outputDir.path, resolveSdkJsFilename(canaryFeatures: canaryFeatures));
outputDir.path,
resolveSdkJsFilename(canaryFeatures: canaryFeatures),
);
final jsMapPath = p.setExtension(jsPath, '.js.map');
final fullDillPath = p.setExtension(jsPath, '.dill');

_logger.info('Generating js and full dill SDK files...');
_logger.info('Generating SDK JavaScript and sourcemap files...');

final sdkDirectoryUri = fileSystem.directory(sdkLayout.sdkDirectory).uri;
final args = <String>[
Expand All @@ -123,7 +106,6 @@ class SdkAssetGenerator {
'org-dartlang-sdk:///lib/libraries.json',
'--modules',
ddcModuleFormat.name,
'--sound-null-safety',
'dart:core',
'-o',
jsPath,
Expand All @@ -132,8 +114,11 @@ class SdkAssetGenerator {

final output = <String>[];
_logger.fine('Executing dart ${args.join(' ')}');
final process = await Process.start(sdkLayout.dartPath, args,
workingDirectory: sdkLayout.sdkDirectory);
final process = await Process.start(
sdkLayout.dartPath,
args,
workingDirectory: sdkLayout.sdkDirectory,
);

process.stdout
.transform<String>(utf8.decoder)
Expand Down Expand Up @@ -164,88 +149,14 @@ class SdkAssetGenerator {
}
await _moveAndValidate(jsPath, outputJsPath);
await _moveAndValidate(jsMapPath, outputJsMapPath);
await _moveAndValidate(fullDillPath, outputFullDillPath);

_logger.info('Done generating js and full dill SDK files.');
_logger.info('Done generating SDK JavaScript and sourcemap files.');
} catch (e, s) {
_logger.severe(
'Failed to generate SDK js, source map, and full dill', e, s);
rethrow;
} finally {
outputDir?.deleteSync(recursive: true);
}
}

Future<void> _generateSdkSummary() async {
Directory? outputDir;
try {
// Files to copy generated files to.
final outputSummaryPath = sdkLayout.summaryPath;
final hasAssets = _exists(outputSummaryPath);

// Files already exist.
if (hasAssets) return;

// Generate missing files.
outputDir = fileSystem.systemTempDirectory.createTempSync();
final summaryPath = p.join(outputDir.path, sdkLayout.summaryFileName);

_logger.info('Generating SDK summary files...');

final sdkDirectoryUri = fileSystem.directory(sdkLayout.sdkDirectory).uri;
final args = <String>[
sdkLayout.kernelWorkerSnapshotPath,
'--target',
'ddc',
'--multi-root',
'$sdkDirectoryUri',
'--multi-root-scheme',
'org-dartlang-sdk',
'--libraries-file',
'org-dartlang-sdk:///lib/libraries.json',
'--source',
'dart:core',
'--summary-only',
'--sound-null-safety',
'--output',
summaryPath,
if (verbose) '--verbose',
];

_logger.fine('Executing dart ${args.join(' ')}');
final process = await Process.start(sdkLayout.dartAotRuntimePath, args,
workingDirectory: sdkLayout.sdkDirectory);

final output = <String>[];
process.stdout
.transform<String>(utf8.decoder)
.transform<String>(const LineSplitter())
.listen((line) {
_logger.fine(line);
output.add(line);
});

process.stderr
.transform<String>(utf8.decoder)
.transform<String>(const LineSplitter())
.listen((line) {
_logger.warning(line);
output.add(line);
});

await process.exitCode.then((int code) {
if (code != 0) {
_logger
.warning('Error generating $summaryPath: ${output.join('\n')}');
throw Exception('The Dart kernel worker exited unexpectedly');
}
});

await _moveAndValidate(summaryPath, outputSummaryPath);

_logger.info('Done generating SDK summary files.');
} catch (e, s) {
_logger.severe('Failed to generate SDK summary', e, s);
'Failed to generate SDK JavaScript and sourcemap files',
e,
s,
);
rethrow;
} finally {
outputDir?.deleteSync(recursive: true);
Expand Down
7 changes: 6 additions & 1 deletion test_common/lib/test_sdk_layout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ class TestSdkLayout {
factory TestSdkLayout.createDefaultFromSdkLayout(SdkLayout sdkLayout) =>
TestSdkLayout(
sdkDirectory: sdkLayout.sdkDirectory,
summaryPath: sdkLayout.summaryPath,
summaryPath: p.join(
sdkLayout.sdkDirectory,
'lib',
'_internal',
'ddc_outline.dill',
),
fullDillPath: p.join(
sdkLayout.sdkDirectory,
'lib',
Expand Down
13 changes: 0 additions & 13 deletions test_common/test/sdk_asset_generator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ void main() {

late Directory tempDir;
late String sdkDirectory;
late String sdkSummaryPath;
late String compilerWorkerPath;

// Missing assets
late String sdkFullDillPath;
late String amdSdkJsPath;
late String amdSdkJsMapPath;
late String ddcSdkJsPath;
Expand All @@ -37,20 +35,17 @@ void main() {
sdkDirectory = tempDir.path;
final copySdkLayout = TestSdkLayout.createDefault(sdkDirectory);

sdkSummaryPath = copySdkLayout.summaryPath;
compilerWorkerPath = copySdkLayout.dartdevcSnapshotPath;

// Copy the SDK directory into a temp directory.
await copyDirectory(TestSdkLayout.defaultSdkDirectory, sdkDirectory);

// Simulate missing assets.
sdkFullDillPath = copySdkLayout.fullDillPath;
amdSdkJsPath = copySdkLayout.amdJsPath;
amdSdkJsMapPath = copySdkLayout.amdJsMapPath;
ddcSdkJsPath = copySdkLayout.ddcJsPath;
ddcSdkJsMapPath = copySdkLayout.ddcJsMapPath;

_deleteIfExists(sdkFullDillPath);
_deleteIfExists(amdSdkJsPath);
_deleteIfExists(amdSdkJsMapPath);
_deleteIfExists(ddcSdkJsPath);
Expand Down Expand Up @@ -79,8 +74,6 @@ void main() {
expect(configuration.sdkDirectory, equals(sdkDirectory));
expect(configuration.compilerWorkerPath, equals(compilerWorkerPath));

expect(sdkLayout.summaryPath, equals(sdkSummaryPath));
expect(sdkLayout.fullDillPath, equals(sdkFullDillPath));
expect(sdkLayout.amdJsPath, equals(amdSdkJsPath));
expect(sdkLayout.amdJsMapPath, equals(amdSdkJsMapPath));

Expand All @@ -89,8 +82,6 @@ void main() {
configuration.validate();

// Validate all assets exist.
expect(sdkLayout.summaryPath, _exists);
expect(sdkLayout.fullDillPath, _exists);
expect(sdkLayout.amdJsPath, _exists);
expect(sdkLayout.amdJsMapPath, _exists);
});
Expand All @@ -113,8 +104,6 @@ void main() {
expect(configuration.sdkDirectory, equals(sdkDirectory));
expect(configuration.compilerWorkerPath, equals(compilerWorkerPath));

expect(sdkLayout.summaryPath, equals(sdkSummaryPath));
expect(sdkLayout.fullDillPath, equals(sdkFullDillPath));
expect(sdkLayout.ddcJsPath, equals(ddcSdkJsPath));
expect(sdkLayout.ddcJsMapPath, equals(ddcSdkJsMapPath));

Expand All @@ -123,8 +112,6 @@ void main() {
configuration.validate();

// Validate all assets exist.
expect(sdkLayout.summaryPath, _exists);
expect(sdkLayout.fullDillPath, _exists);
expect(sdkLayout.ddcJsPath, _exists);
expect(sdkLayout.ddcJsMapPath, _exists);
});
Expand Down
Loading