Skip to content

Commit

Permalink
[deps] Roll dart-lang/native
Browse files Browse the repository at this point in the history
This CL goes part of the way to support pub workspaces with native
assets.

This CL makes the `runPackageName` explicit in every invocation. This
ensures not too many native assets are built.

Moreover, this CL also makes `packageLayout` explicit, ensuring the
right packages config file is used.

A follow up PR should fix the checks w.r.t. the experiment not being
enabled.

For more info see:
dart-lang/native#1911

TEST=pkg/dartdev/test/native

Change-Id: I3c3123526e320abb8bb094116e58bb4340c4c052
Cq-Include-Trybots: luci.dart.try:pkg-linux-debug-try,pkg-linux-release-arm64-try,pkg-linux-release-try,pkg-mac-release-arm64-try,pkg-mac-release-try,pkg-win-release-arm64-try,pkg-win-release-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/405141
Commit-Queue: Daco Harkes <[email protected]>
Reviewed-by: Ben Konyi <[email protected]>
  • Loading branch information
dcharkes authored and Commit Queue committed Jan 20, 2025
1 parent 667730a commit 762a4c7
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 30 deletions.
2 changes: 1 addition & 1 deletion DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ vars = {
"markdown_rev": "19aaded4300d24bedcbf52ade792b203ddf030b0",
"material_color_utilities_rev": "799b6ba2f3f1c28c67cc7e0b4f18e0c7d7f3c03e",
# dart-native-interop-team@ is rolling breaking changes manually while the assets features are in experimental.
"native_rev": "ba34674d97ffe612a3df782e1695dab4e5010e66", # disable tools/rev_sdk_deps.dart
"native_rev": "d1d9aa5e62d239580b8bf17e36db6d47c308fc89", # disable tools/rev_sdk_deps.dart
"protobuf_rev": "b7dd58cdbd879beee4c3fbf8ee80fce8e97bad26",
"pub_rev": "710265bae23ad5860f33287fba10b5c369f19a93", # disable tools/rev_sdk_deps.dart
"shelf_rev": "bf799519cda2898a7c5af06dcfdd5fe6443afd79",
Expand Down
32 changes: 13 additions & 19 deletions pkg/dartdev/lib/src/commands/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 'package:front_end/src/api_prototype/compiler_options.dart'
import 'package:native_assets_builder/native_assets_builder.dart';
import 'package:native_assets_cli/code_assets_builder.dart';
import 'package:native_assets_cli/data_assets_builder.dart';
import 'package:package_config/package_config.dart' as package_config;
import 'package:path/path.dart' as path;
import 'package:vm/target_os.dart'; // For possible --target-os values.

Expand Down Expand Up @@ -133,7 +134,14 @@ class BuildCommand extends DartdevCommand {

// Start native asset generation here.
stdout.writeln('Building native assets.');
final packageConfig = await findPackageConfigUri(sourceUri);
final runPackageName = await findRootPackageName(sourceUri);
final workingDirectory = Directory.current.uri;
final packageLayout = PackageLayout.fromPackageConfig(
LocalFileSystem(),
await package_config.loadPackageConfigUri(packageConfig!),
packageConfig,
);
final target = Target.current;
final macOSConfig = target.os == OS.macOS
? MacOSConfig(targetVersion: minimumSupportedMacOSVersion)
Expand All @@ -160,7 +168,8 @@ class BuildCommand extends DartdevCommand {
...await validateCodeAssetBuildInput(config),
],
workingDirectory: workingDirectory,

packageLayout: packageLayout,
runPackageName: runPackageName,
linkingEnabled: true,
buildAssetTypes: [
CodeAsset.type,
Expand All @@ -181,7 +190,6 @@ class BuildCommand extends DartdevCommand {

final tempDir = Directory.systemTemp.createTempSync();
try {
final packageConfig = await packageConfigUri(sourceUri);
String? recordedUsagesPath;
if (recordUseEnabled) {
recordedUsagesPath = path.join(tempDir.path, 'recorded_usages.json');
Expand All @@ -193,7 +201,7 @@ class BuildCommand extends DartdevCommand {
verbose: verbose,
verbosity: args.option('verbosity')!,
defines: [],
packages: packageConfig?.toFilePath(),
packages: packageConfig.toFilePath(),
targetOS: targetOS,
enableExperiment: args.enabledExperiments.join(','),
tempDir: tempDir,
Expand All @@ -220,6 +228,8 @@ class BuildCommand extends DartdevCommand {
resourceIdentifiers:
recordUseEnabled ? Uri.file(recordedUsagesPath!) : null,
workingDirectory: workingDirectory,
runPackageName: runPackageName,
packageLayout: packageLayout,
buildResult: buildResult,
buildAssetTypes: [
CodeAsset.type,
Expand Down Expand Up @@ -281,19 +291,3 @@ extension on String {
String makeFolder() => endsWith('\\') || endsWith('/') ? this : '$this/';
String removeDotDart() => replaceFirst(RegExp(r'\.dart$'), '');
}

// TODO(https://github.com/dart-lang/package_config/issues/126): Expose this
// logic in package:package_config.
Future<Uri?> packageConfigUri(Uri uri) async {
while (true) {
final candidate = uri.resolve('.dart_tool/package_config.json');
if (await File.fromUri(candidate).exists()) {
return candidate;
}
final parent = uri.resolve('..');
if (parent == uri) {
return null;
}
uri = parent;
}
}
5 changes: 4 additions & 1 deletion pkg/dartdev/lib/src/commands/compile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,10 @@ Remove debugging information from the output and save it separately to the speci
return 255;
}
} else {
final assets = await compileNativeAssetsJit(verbose: verbose);
final assets = await compileNativeAssetsJit(
verbose: verbose,
runPackageName: await findRootPackageName(Directory.current.uri),
);
if (assets == null) {
stderr.writeln('Native assets build failed.');
return 255;
Expand Down
3 changes: 2 additions & 1 deletion pkg/dartdev/lib/src/commands/run.dart
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,8 @@ class RunCommand extends DartdevCommand {
return errorExitCode;
}
} else {
final runPackageName = getPackageForCommand(mainCommand);
final runPackageName = getPackageForCommand(mainCommand) ??
await findRootPackageName(Directory.current.uri);
final assetsYamlFileUri = await compileNativeAssetsJitYamlFile(
verbose: verbose,
runPackageName: runPackageName,
Expand Down
6 changes: 5 additions & 1 deletion pkg/dartdev/lib/src/commands/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:io';

import 'package:args/args.dart';
import 'package:dartdev/src/experiments.dart';
Expand Down Expand Up @@ -52,7 +53,10 @@ Run "${runner!.executableName} help" to see global options.''');
}
} else {
final assetsYamlFileUri =
await compileNativeAssetsJitYamlFile(verbose: verbose);
await compileNativeAssetsJitYamlFile(
verbose: verbose,
runPackageName: await findRootPackageName(Directory.current.uri),
);
if (assetsYamlFileUri == null) {
log.stderr('Error: Compiling native assets failed.');
return DartdevCommand.errorExitCode;
Expand Down
58 changes: 51 additions & 7 deletions pkg/dartdev/lib/src/native_assets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:native_assets_builder/native_assets_builder.dart';
import 'package:native_assets_cli/code_assets_builder.dart';
import 'package:native_assets_cli/data_assets_builder.dart';
import 'package:native_assets_cli/native_assets_cli_internal.dart';
import 'package:package_config/package_config.dart' as package_config;

import 'core.dart';

Expand All @@ -23,25 +24,31 @@ import 'core.dart';
/// [runPackageName] are built.
Future<List<EncodedAsset>?> compileNativeAssetsJit({
required bool verbose,
String? runPackageName,
required String runPackageName,
}) async {
final workingDirectory = Directory.current.uri;
var packageConfig = await findPackageConfigUri(workingDirectory);
// TODO(https://github.com/dart-lang/package_config/issues/126): Use
// package config resolution from package:package_config.
if (!await File.fromUri(
workingDirectory.resolve('.dart_tool/package_config.json'))
.exists()) {
if (await File.fromUri(workingDirectory.resolve('pubspec.yaml')).exists()) {
if (packageConfig == null) {
final pubspecMaybe = await _findPubspec(workingDirectory);
if (pubspecMaybe != null) {
// Silently run `pub get`, this is what would happen in
// `getExecutableForCommand` later.
final result = await Process.run(sdk.dart, ['pub', 'get']);
if (result.exitCode != 0) {
return null;
}
packageConfig = await findPackageConfigUri(workingDirectory);
} else {
return null;
}
}
final packageLayout = PackageLayout.fromPackageConfig(
LocalFileSystem(),
await package_config.loadPackageConfigUri(packageConfig!),
packageConfig,
);
final nativeAssetsBuildRunner = NativeAssetsBuildRunner(
// This always runs in JIT mode.
dartExecutable: Uri.file(sdk.dart),
Expand Down Expand Up @@ -69,6 +76,7 @@ Future<List<EncodedAsset>?> compileNativeAssetsJit({
],
workingDirectory: workingDirectory,
runPackageName: runPackageName,
packageLayout: packageLayout,
linkingEnabled: false,
buildAssetTypes: [
CodeAsset.type,
Expand All @@ -94,7 +102,7 @@ Future<List<EncodedAsset>?> compileNativeAssetsJit({
/// Used in `dart run` and `dart test`.
Future<Uri?> compileNativeAssetsJitYamlFile({
required bool verbose,
String? runPackageName,
required String runPackageName,
}) async {
final assets = await compileNativeAssetsJit(
verbose: verbose,
Expand Down Expand Up @@ -132,7 +140,7 @@ Future<bool> warnOnNativeAssets() async {
}
try {
final packageLayout =
await PackageLayout.fromRootPackageRoot(
await PackageLayout.fromWorkingDirectory(
const LocalFileSystem(),
workingDirectory,
);
Expand Down Expand Up @@ -199,3 +207,39 @@ CCompilerConfig? getCCompilerConfig() {
}
return null;
}

// TODO(https://github.com/dart-lang/package_config/issues/126): Expose this
// logic in package:package_config.
Future<Uri?> findPackageConfigUri(Uri uri) async {
while (true) {
final candidate = uri.resolve('.dart_tool/package_config.json');
final file = File.fromUri(candidate);
if (await file.exists()) {
return file.uri;
}
final parent = uri.resolve('..');
if (parent == uri) {
return null;
}
uri = parent;
}
}

Future<Uri?> _findPubspec(Uri uri) async {
while (true) {
final candidate = uri.resolve('pubspec.yaml');
if (await File.fromUri(candidate).exists()) {
return candidate;
}
final parent = uri.resolve('..');
if (parent == uri) {
return null;
}
uri = parent;
}
}

Future<String> findRootPackageName(Uri uri) async {
final pubspec = await _findPubspec(uri);
return pubspec!.resolve('./').pathSegments.lastWhere((e) => e.isNotEmpty);
}
1 change: 1 addition & 0 deletions pkg/dartdev/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies:
meta: any
native_assets_builder: any
native_assets_cli: any
package_config: any
path: any
pub: any
unified_analytics: any
Expand Down

0 comments on commit 762a4c7

Please sign in to comment.