[pkg][dartdev] Error on native assets but not enabled

And only print logs if `-v` is passed.

TEST=pkg/dartdev/test/native_assets/

Bug: https://github.com/dart-lang/sdk/issues/50565
Change-Id: Iccf879c80ea01cb39f9dd0d13c2f515a885d11d2
Cq-Include-Trybots: luci.dart.try:pkg-linux-debug-try,pkg-linux-release-try,pkg-mac-release-arm64-try,pkg-mac-release-try,pkg-win-release-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/314721
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Hossein Yousefi <yousefi@google.com>
This commit is contained in:
Daco Harkes 2023-07-24 11:25:06 +00:00 committed by Commit Queue
parent 11d820890e
commit 72dd200f8a
9 changed files with 151 additions and 61 deletions

View file

@ -105,7 +105,7 @@ class BuildCommand extends DartdevCommand {
final target = Target.current;
final nativeAssets = await NativeAssetsBuildRunner(
dartExecutable: Uri.file(sdk.dart),
logger: logger,
logger: logger(verbose),
).build(
workingDirectory: workingDirectory,
target: target,

View file

@ -329,8 +329,12 @@ Remove debugging information from the output and save it separately to the speci
return compileErrorExitCode;
}
if (nativeAssetsExperimentEnabled) {
final assets = await compileNativeAssetsJit();
if (!nativeAssetsExperimentEnabled) {
if (await warnOnNativeAssets()) {
return 255;
}
} else {
final assets = await compileNativeAssetsJit(verbose: verbose);
if (assets?.isNotEmpty ?? false) {
stderr.writeln(
"'dart compile' does currently not support native assets.");

View file

@ -301,9 +301,14 @@ class RunCommand extends DartdevCommand {
}
String? nativeAssets;
if (nativeAssetsExperimentEnabled) {
if (!nativeAssetsExperimentEnabled) {
if (await warnOnNativeAssets()) {
return errorExitCode;
}
} else {
try {
nativeAssets = (await compileNativeAssetsJitYamlFile())?.toFilePath();
nativeAssets = (await compileNativeAssetsJitYamlFile(verbose: verbose))
?.toFilePath();
} on Exception catch (e, stacktrace) {
log.stderr('Error: Compiling native assets failed.');
log.stderr(e.toString());

View file

@ -46,9 +46,14 @@ Run "${runner!.executableName} help" to see global options.''');
final args = argResults!;
String? nativeAssets;
if (nativeAssetsExperimentEnabled) {
if (!nativeAssetsExperimentEnabled) {
if (await warnOnNativeAssets()) {
return DartdevCommand.errorExitCode;
}
} else {
try {
nativeAssets = (await compileNativeAssetsJitYamlFile())?.toFilePath();
nativeAssets = (await compileNativeAssetsJitYamlFile(verbose: verbose))
?.toFilePath();
} on Exception catch (e, stacktrace) {
log.stderr('Error: Compiling native assets failed.');
log.stderr(e.toString());

View file

@ -12,22 +12,10 @@ import 'package:native_assets_cli/native_assets_cli.dart';
import 'core.dart';
final logger = Logger('')
..onRecord.listen((LogRecord record) {
final levelValue = record.level.value;
if (levelValue >= Level.SEVERE.value) {
log.stderr(record.message);
} else if (levelValue >= Level.INFO.value) {
log.stdout(record.message);
} else {
log.trace(record.message);
}
});
/// Compiles all native assets for host OS in JIT mode.
///
/// Returns `null` on missing package_config.json, failing gracefully.
Future<List<Asset>?> compileNativeAssetsJit() async {
Future<List<Asset>?> compileNativeAssetsJit({required bool verbose}) async {
final workingDirectory = Directory.current.uri;
// TODO(https://github.com/dart-lang/package_config/issues/126): Use
// package config resolution from package:package_config.
@ -39,7 +27,7 @@ Future<List<Asset>?> compileNativeAssetsJit() async {
final assets = await NativeAssetsBuildRunner(
// This always runs in JIT mode.
dartExecutable: Uri.file(sdk.dart),
logger: logger,
logger: logger(verbose),
).build(
workingDirectory: workingDirectory,
// When running in JIT mode, only the host OS needs to be build.
@ -59,8 +47,8 @@ Future<List<Asset>?> compileNativeAssetsJit() async {
/// Used in `dart run` and `dart test`.
///
/// Returns `null` on missing package_config.json, failing gracefully.
Future<Uri?> compileNativeAssetsJitYamlFile() async {
final assets = await compileNativeAssetsJit();
Future<Uri?> compileNativeAssetsJitYamlFile({required bool verbose}) async {
final assets = await compileNativeAssetsJit(verbose: verbose);
if (assets == null) return null;
final workingDirectory = Directory.current.uri;
@ -72,3 +60,39 @@ ${assets.toNativeAssetsFile()}''';
await assetFile.writeAsString(nativeAssetsYaml);
return assetsUri;
}
Future<bool> warnOnNativeAssets() async {
final workingDirectory = Directory.current.uri;
if (!await File.fromUri(
workingDirectory.resolve('.dart_tool/package_config.json'))
.exists()) {
// If `pub get` hasn't run, we can't know, so don't error.
return false;
}
final packageLayout =
await PackageLayout.fromRootPackageRoot(workingDirectory);
final packagesWithNativeAssets = await packageLayout.packagesWithNativeAssets;
if (packagesWithNativeAssets.isEmpty) {
return false;
}
final packageNames = packagesWithNativeAssets.map((p) => p.name).join(' ');
log.stderr(
'Package(s) $packageNames require the native assets feature to be enabled. '
'Enable native assets with `--enable-experiment=native-assets`.',
);
return true;
}
Logger logger(bool verbose) => Logger('')
..onRecord.listen((LogRecord record) {
final levelValue = record.level.value;
if (levelValue >= Level.SEVERE.value) {
log.stderr(record.message);
} else if (levelValue >= Level.WARNING.value ||
verbose && levelValue >= Level.INFO.value) {
log.stdout(record.message);
} else {
// Note, this is ignored by default.
log.trace(record.message);
}
});

View file

@ -13,33 +13,41 @@ import 'helpers.dart';
void main(List<String> args) async {
final bool fromDartdevSource = args.contains('--source');
test('dart build', timeout: longTimeout, () async {
await nativeAssetsTest('dart_app', (dartAppUri) async {
await runDart(
arguments: [
'--enable-experiment=native-assets',
if (fromDartdevSource)
Platform.script.resolve('../../bin/dartdev.dart').toFilePath(),
'build',
'bin/dart_app.dart',
],
workingDirectory: dartAppUri,
logger: logger,
);
final relativeExeUri = Uri.file('./bin/dart_app/dart_app.exe');
final absoluteExeUri = dartAppUri.resolveUri(relativeExeUri);
expect(await File.fromUri(absoluteExeUri).exists(), true);
for (final exeUri in [absoluteExeUri, relativeExeUri]) {
final result = await runProcess(
executable: exeUri,
arguments: [],
for (final verbose in [true, false]) {
final testModifier = ['', if (verbose) 'verbose'].join(' ');
test('dart build$testModifier', timeout: longTimeout, () async {
await nativeAssetsTest('dart_app', (dartAppUri) async {
final result = await runDart(
arguments: [
'--enable-experiment=native-assets',
if (fromDartdevSource)
Platform.script.resolve('../../bin/dartdev.dart').toFilePath(),
'build',
if (verbose) '-v',
'bin/dart_app.dart',
],
workingDirectory: dartAppUri,
logger: logger,
);
expectDartAppStdout(result.stdout);
}
if (verbose) {
expect(result.stdout, contains('build.dart'));
} else {
expect(result.stdout, isNot(contains('build.dart')));
}
final relativeExeUri = Uri.file('./bin/dart_app/dart_app.exe');
final absoluteExeUri = dartAppUri.resolveUri(relativeExeUri);
expect(await File.fromUri(absoluteExeUri).exists(), true);
for (final exeUri in [absoluteExeUri, relativeExeUri]) {
final result = await runProcess(
executable: exeUri,
arguments: [],
workingDirectory: dartAppUri,
logger: logger,
);
expectDartAppStdout(result.stdout);
}
});
});
});
}
}

View file

@ -169,6 +169,7 @@ Future<run_process.RunProcessResult> runDart({
required List<String> arguments,
Uri? workingDirectory,
required Logger? logger,
bool expectExitCodeZero = true,
}) async {
final result = await runProcess(
executable: dartExecutable,
@ -176,6 +177,8 @@ Future<run_process.RunProcessResult> runDart({
workingDirectory: workingDirectory,
logger: logger,
);
expect(result.exitCode, 0);
if (expectExitCodeZero) {
expect(result.exitCode, 0);
}
return result;
}

View file

@ -13,19 +13,28 @@ void main(List<String> args) async {
// No --source option, `dart run` from source does not output target program
// stdout.
test('dart run ', timeout: longTimeout, () async {
await nativeAssetsTest('dart_app', (dartAppUri) async {
final result = await runDart(
arguments: [
'--enable-experiment=native-assets',
'run',
],
workingDirectory: dartAppUri,
logger: logger,
);
expectDartAppStdout(result.stdout);
for (final verbose in [true, false]) {
final testModifier = ['', if (verbose) 'verbose'].join(' ');
test('dart run$testModifier', timeout: longTimeout, () async {
await nativeAssetsTest('dart_app', (dartAppUri) async {
final result = await runDart(
arguments: [
'--enable-experiment=native-assets',
'run',
if (verbose) '-v',
],
workingDirectory: dartAppUri,
logger: logger,
);
expectDartAppStdout(result.stdout);
if (verbose) {
expect(result.stdout, contains('build.dart'));
} else {
expect(result.stdout, isNot(contains('build.dart')));
}
});
});
});
}
test('dart run test/xxx_test.dart', timeout: longTimeout, () async {
await nativeAssetsTest('native_add', (packageUri) async {
@ -49,4 +58,20 @@ void main(List<String> args) async {
);
});
});
test('dart build native assets disabled', timeout: longTimeout, () async {
await nativeAssetsTest('dart_app', (dartAppUri) async {
final result = await runDart(
arguments: [
'run',
],
workingDirectory: dartAppUri,
logger: logger,
expectExitCodeZero: false,
);
expect(result.exitCode, isNot(0));
expect(result.stderr, contains('Enable native assets'));
expect(result.stderr, contains('native_add'));
});
});
}

View file

@ -57,4 +57,20 @@ void main(List<String> args) async {
);
});
});
test('dart build native assets disabled', timeout: longTimeout, () async {
await nativeAssetsTest('dart_app', (dartAppUri) async {
final result = await runDart(
arguments: [
'test',
],
workingDirectory: dartAppUri,
logger: logger,
expectExitCodeZero: false,
);
expect(result.exitCode, isNot(0));
expect(result.stderr, contains('Enable native assets'));
expect(result.stderr, contains('native_add'));
});
});
}