From 000d76e59cdfc610a6e75ba339a25ee1b34da5ef Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Fri, 17 Jan 2025 14:44:17 +0100 Subject: [PATCH 1/3] [native_assets_builder] Support pub workspaces --- .../lib/src/build_runner/build_planner.dart | 30 +++++-- .../lib/src/build_runner/build_runner.dart | 32 +++---- .../src/package_layout/package_layout.dart | 28 +++++- .../test/build_runner/build_planner_test.dart | 8 +- .../build_runner_reusability_test.dart | 5 +- .../concurrency_shared_test_helper.dart | 2 + .../build_runner/concurrency_test_helper.dart | 2 + .../test/build_runner/helpers.dart | 36 +++++++- .../test/build_runner/pub_workspace_test.dart | 87 +++++++++++++++++++ pkgs/native_assets_builder/test/helpers.dart | 21 +++++ .../test_data/cyclic_package_1/pubspec.yaml | 2 +- .../test_data/cyclic_package_2/pubspec.yaml | 2 +- .../test_data/dart_app/pubspec.yaml | 2 +- .../fail_on_os_sdk_version_link/pubspec.yaml | 2 +- .../test_data/native_add/pubspec.yaml | 2 +- .../native_add_version_skew/hook/build.dart | 4 +- .../native_add_version_skew/pubspec.yaml | 2 +- .../test/native_add_test.dart | 2 +- .../test_data/native_subtract/pubspec.yaml | 2 +- .../test_data/wrong_linker/pubspec.yaml | 2 +- 20 files changed, 220 insertions(+), 53 deletions(-) create mode 100644 pkgs/native_assets_builder/test/build_runner/pub_workspace_test.dart diff --git a/pkgs/native_assets_builder/lib/src/build_runner/build_planner.dart b/pkgs/native_assets_builder/lib/src/build_runner/build_planner.dart index 96aff79a8..797e2281f 100644 --- a/pkgs/native_assets_builder/lib/src/build_runner/build_planner.dart +++ b/pkgs/native_assets_builder/lib/src/build_runner/build_planner.dart @@ -47,15 +47,13 @@ class NativeAssetsBuildPlanner { ); } - List? plan({ - String? runPackageName, - }) { - final PackageGraph packageGraph; - if (runPackageName != null) { - packageGraph = this.packageGraph.subGraph(runPackageName); - } else { - packageGraph = this.packageGraph; - } + /// Plans in what order to run build hooks. + /// + /// [runPackageName] provides the entry-point in the graph. The hooks of + /// packages not in the transitive dependencies of [runPackageName] will not + /// be run. + List? plan(String runPackageName) { + final packageGraph = this.packageGraph.subGraph(runPackageName); final packageMap = { for (final package in packagesWithNativeAssets) package.name: package }; @@ -136,4 +134,18 @@ class PackageGraph { ] }); } + + @override + String toString() { + final buffer = StringBuffer(); + buffer.writeln('PackageGraph('); + for (final node in vertices) { + buffer.writeln(' $node ->'); + for (final neighbor in neighborsOf(node)) { + buffer.writeln(' $neighbor'); + } + } + buffer.writeln(')'); + return buffer.toString(); + } } diff --git a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart index 709d0d682..14593c047 100644 --- a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart +++ b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart @@ -101,7 +101,7 @@ class NativeAssetsBuildRunner { required ApplicationAssetValidator applicationAssetValidator, required Uri workingDirectory, PackageLayout? packageLayout, - String? runPackageName, + required String runPackageName, required List buildAssetTypes, required bool linkingEnabled, }) async { @@ -208,7 +208,7 @@ class NativeAssetsBuildRunner { required ApplicationAssetValidator applicationAssetValidator, PackageLayout? packageLayout, Uri? resourceIdentifiers, - String? runPackageName, + required String runPackageName, required List buildAssetTypes, required BuildResult buildResult, }) async { @@ -775,7 +775,7 @@ ${compileResult.stdout} Future<(List? plan, PackageGraph? dependencyGraph)> _makePlan({ required PackageLayout packageLayout, - String? runPackageName, + required String runPackageName, required Hook hook, // TODO(dacoharkes): How to share these two? Make them extend each other? BuildResult? buildResult, @@ -785,24 +785,14 @@ ${compileResult.stdout} final PackageGraph? packageGraph; switch (hook) { case Hook.build: - // Build hooks are run in toplogical order. - if (packagesWithHook.length <= 1 && runPackageName == null) { - final dependencyGraph = PackageGraph({ - for (final p in packagesWithHook) p.name: [], - }); - return (packagesWithHook, dependencyGraph); - } else { - final planner = await NativeAssetsBuildPlanner.fromRootPackageRoot( - rootPackageRoot: packageLayout.rootPackageRoot, - packagesWithNativeAssets: packagesWithHook, - dartExecutable: Uri.file(Platform.resolvedExecutable), - logger: logger, - ); - final plan = planner.plan( - runPackageName: runPackageName, - ); - return (plan, planner.packageGraph); - } + final planner = await NativeAssetsBuildPlanner.fromRootPackageRoot( + rootPackageRoot: packageLayout.rootPackageRoot, + packagesWithNativeAssets: packagesWithHook, + dartExecutable: Uri.file(Platform.resolvedExecutable), + logger: logger, + ); + final plan = planner.plan(runPackageName); + return (plan, planner.packageGraph); case Hook.link: // Link hooks are not run in any particular order. // Link hooks are skipped if no assets for linking are provided. diff --git a/pkgs/native_assets_builder/lib/src/package_layout/package_layout.dart b/pkgs/native_assets_builder/lib/src/package_layout/package_layout.dart index 1e24aa3b2..9c39c66eb 100644 --- a/pkgs/native_assets_builder/lib/src/package_layout/package_layout.dart +++ b/pkgs/native_assets_builder/lib/src/package_layout/package_layout.dart @@ -55,21 +55,41 @@ class PackageLayout { ) async { rootPackageRoot = rootPackageRoot.normalizePath(); final packageConfigUri = - rootPackageRoot.resolve('.dart_tool/package_config.json'); + await findPackageConfig(fileSystem, rootPackageRoot); assert(await fileSystem.file(packageConfigUri).exists()); - final packageConfig = await loadPackageConfigUri(packageConfigUri); + final packageConfig = await loadPackageConfigUri(packageConfigUri!); return PackageLayout._( fileSystem, rootPackageRoot, packageConfig, packageConfigUri); } + static Future findPackageConfig( + FileSystem fileSystem, + Uri rootPackageRoot, + ) async { + final packageConfigUri = + rootPackageRoot.resolve('.dart_tool/package_config.json'); + final file = fileSystem.file(packageConfigUri); + if (await file.exists()) { + return file.uri; + } + final parentUri = rootPackageRoot.resolve('../'); + if (parentUri == rootPackageRoot) { + return null; + } + return findPackageConfig(fileSystem, parentUri); + } + /// The .dart_tool directory is used to store built artifacts and caches. /// - /// `$rootPackageRoot/.dart_tool/`. + /// This is the `.dart_tool/` directory where the package config is. + /// + /// When pub workspaces are used, the hook results are shared across all + /// packages in the workspace. /// /// Each package should only modify the subfolder of `.dart_tool/` with its /// own name. /// https://dart.dev/tools/pub/package-layout#project-specific-caching-for-tools - late final Uri dartTool = rootPackageRoot.resolve('.dart_tool/'); + late final Uri dartTool = packageConfigUri.resolve('./'); /// The directory where `package:native_assets_builder` stores all persistent /// information. diff --git a/pkgs/native_assets_builder/test/build_runner/build_planner_test.dart b/pkgs/native_assets_builder/test/build_runner/build_planner_test.dart index 67780467c..695cfa01c 100644 --- a/pkgs/native_assets_builder/test/build_runner/build_planner_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/build_planner_test.dart @@ -46,7 +46,7 @@ void main() async { dartExecutable: Uri.file(Platform.resolvedExecutable), logger: logger, ); - final buildPlan = planner.plan(); + final buildPlan = planner.plan('native_add'); expect(buildPlan!.length, 1); expect(buildPlan.single.name, 'native_add'); }); @@ -71,7 +71,7 @@ void main() async { dartExecutable: Uri.file(Platform.resolvedExecutable), logger: logger, ); - final buildPlan = nativeAssetsBuildPlanner.plan(); + final buildPlan = nativeAssetsBuildPlanner.plan('native_add'); expect(buildPlan!.length, 1); expect(buildPlan.single.name, 'native_add'); }); @@ -98,9 +98,7 @@ void main() async { dartExecutable: Uri.file(Platform.resolvedExecutable), logger: logger, ); - final buildPlan = nativeAssetsBuildPlanner.plan( - runPackageName: runPackageName, - ); + final buildPlan = nativeAssetsBuildPlanner.plan(runPackageName); expect(buildPlan!.length, 0); }); }); diff --git a/pkgs/native_assets_builder/test/build_runner/build_runner_reusability_test.dart b/pkgs/native_assets_builder/test/build_runner/build_runner_reusability_test.dart index 810a27f26..ac4c86cbd 100644 --- a/pkgs/native_assets_builder/test/build_runner/build_runner_reusability_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/build_runner_reusability_test.dart @@ -15,7 +15,8 @@ void main() async { test('multiple build invocations', timeout: longTimeout, () async { await inTempDir((tempUri) async { await copyTestProjects(targetUri: tempUri); - final packageUri = tempUri.resolve('package_reading_metadata/'); + const packageName = 'package_reading_metadata'; + final packageUri = tempUri.resolve('$packageName/'); // First, run `pub get`, we need pub to resolve our dependencies. await runPubGet( @@ -49,6 +50,7 @@ void main() async { inputValidator: (input) async => [], buildValidator: (input, output) async => [], applicationAssetValidator: (_) async => [], + runPackageName: packageName, ); await buildRunner.build( inputCreator: inputCreator, @@ -58,6 +60,7 @@ void main() async { inputValidator: (input) async => [], buildValidator: (input, output) async => [], applicationAssetValidator: (_) async => [], + runPackageName: packageName, ); }); }); diff --git a/pkgs/native_assets_builder/test/build_runner/concurrency_shared_test_helper.dart b/pkgs/native_assets_builder/test/build_runner/concurrency_shared_test_helper.dart index 9b1707d64..791b104fa 100644 --- a/pkgs/native_assets_builder/test/build_runner/concurrency_shared_test_helper.dart +++ b/pkgs/native_assets_builder/test/build_runner/concurrency_shared_test_helper.dart @@ -11,6 +11,7 @@ import '../helpers.dart'; // Is invoked concurrently multiple times in separate processes. void main(List args) async { final packageUri = Uri.directory(args[0]); + final packageName = packageUri.pathSegments.lastWhere((e) => e.isNotEmpty); final target = Target.fromString(args[1]); final logger = Logger('') @@ -49,6 +50,7 @@ void main(List args) async { ...await validateCodeAssetBuildOutput(input, output), ], applicationAssetValidator: validateCodeAssetInApplication, + runPackageName: packageName, ); if (result == null) { throw Error(); diff --git a/pkgs/native_assets_builder/test/build_runner/concurrency_test_helper.dart b/pkgs/native_assets_builder/test/build_runner/concurrency_test_helper.dart index a04455907..f250e9f51 100644 --- a/pkgs/native_assets_builder/test/build_runner/concurrency_test_helper.dart +++ b/pkgs/native_assets_builder/test/build_runner/concurrency_test_helper.dart @@ -12,6 +12,7 @@ import 'helpers.dart'; // Is invoked concurrently multiple times in separate processes. void main(List args) async { final packageUri = Uri.directory(args[0]); + final packageName = packageUri.pathSegments.lastWhere((e) => e.isNotEmpty); Duration? timeout; if (args.length >= 2) { timeout = Duration(milliseconds: int.parse(args[1])); @@ -50,6 +51,7 @@ void main(List args) async { ...await validateDataAssetBuildOutput(input, output), ], applicationAssetValidator: validateCodeAssetInApplication, + runPackageName: packageName, ); if (result == null) { throw Error(); diff --git a/pkgs/native_assets_builder/test/build_runner/helpers.dart b/pkgs/native_assets_builder/test/build_runner/helpers.dart index d772d1a55..a04f328c2 100644 --- a/pkgs/native_assets_builder/test/build_runner/helpers.dart +++ b/pkgs/native_assets_builder/test/build_runner/helpers.dart @@ -29,6 +29,23 @@ Future runPubGet({ expect(result.exitCode, 0); } +Future buildCodeAssets( + Uri packageUri, { + String? runPackageName, + List? capturedLogs, +}) => + build( + packageUri, + logger, + dartExecutable, + capturedLogs: capturedLogs, + inputValidator: validateCodeAssetBuildInput, + buildAssetTypes: [CodeAsset.type], + buildValidator: validateCodeAssetBuildOutput, + applicationAssetValidator: validateCodeAssetInApplication, + runPackageName: runPackageName, + ); + Future build( Uri packageUri, Logger logger, @@ -51,6 +68,10 @@ Future build( Map? hookEnvironment, }) async { final targetOS = target?.os ?? OS.current; + final runPackageName_ = runPackageName ?? + (packageLayout?.rootPackageRoot ?? packageUri) + .pathSegments + .lastWhere((e) => e.isNotEmpty); return await runWithLog(capturedLogs, () async { final result = await NativeAssetsBuildRunner( logger: logger, @@ -86,7 +107,7 @@ Future build( inputValidator: inputValidator, workingDirectory: packageUri, packageLayout: packageLayout, - runPackageName: runPackageName, + runPackageName: runPackageName_, linkingEnabled: linkingEnabled, buildAssetTypes: buildAssetTypes, buildValidator: buildValidator, @@ -116,6 +137,7 @@ Future link( CCompilerConfig? cCompiler, List? capturedLogs, PackageLayout? packageLayout, + String? runPackageName, required BuildResult buildResult, Uri? resourceIdentifiers, IOSSdk? targetIOSSdk, @@ -126,6 +148,10 @@ Future link( required List buildAssetTypes, }) async { final targetOS = target?.os ?? OS.current; + final runPackageName_ = runPackageName ?? + (packageLayout?.rootPackageRoot ?? packageUri) + .pathSegments + .lastWhere((e) => e.isNotEmpty); return await runWithLog(capturedLogs, () async { final result = await NativeAssetsBuildRunner( logger: logger, @@ -165,6 +191,7 @@ Future link( buildAssetTypes: buildAssetTypes, linkValidator: linkValidator, applicationAssetValidator: applicationAssetValidator, + runPackageName: runPackageName_, ); if (result != null) { @@ -198,6 +225,10 @@ Future<(BuildResult?, LinkResult?)> buildAndLink( required List buildAssetTypes, }) async => await runWithLog(capturedLogs, () async { + final runPackageName_ = runPackageName ?? + (packageLayout?.rootPackageRoot ?? packageUri) + .pathSegments + .lastWhere((e) => e.isNotEmpty); final buildRunner = NativeAssetsBuildRunner( logger: logger, dartExecutable: dartExecutable, @@ -233,7 +264,7 @@ Future<(BuildResult?, LinkResult?)> buildAndLink( inputValidator: buildInputValidator, workingDirectory: packageUri, packageLayout: packageLayout, - runPackageName: runPackageName, + runPackageName: runPackageName_, linkingEnabled: true, buildAssetTypes: buildAssetTypes, buildValidator: buildValidator, @@ -284,6 +315,7 @@ Future<(BuildResult?, LinkResult?)> buildAndLink( buildAssetTypes: buildAssetTypes, linkValidator: linkValidator, applicationAssetValidator: applicationAssetValidator, + runPackageName: runPackageName_, ); if (linkResult != null) { diff --git a/pkgs/native_assets_builder/test/build_runner/pub_workspace_test.dart b/pkgs/native_assets_builder/test/build_runner/pub_workspace_test.dart new file mode 100644 index 000000000..93afb088d --- /dev/null +++ b/pkgs/native_assets_builder/test/build_runner/pub_workspace_test.dart @@ -0,0 +1,87 @@ +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'dart:io'; + +import 'package:test/test.dart'; + +import '../helpers.dart'; +import 'helpers.dart'; + +void main() async { + late Uri tempUri; + setUp(() async { + tempUri = await tempDirForTest(); + await copyTestProjects(targetUri: tempUri); + }); + + Future makePubWorkspace(List packages) async { + for (final package in packages) { + final packageUri = tempUri.resolve('$package/'); + final pubspecUri = packageUri.resolve('pubspec.yaml'); + var pubspec = await File.fromUri(pubspecUri).readAsString(); + pubspec += ''' + +resolution: workspace +'''; + await File.fromUri(pubspecUri).writeAsString(pubspec); + } + + final workspacePubSpecUri = tempUri.resolve('pubspec.yaml'); + var workspacePubSpec = ''' +name: dart_lang_native_workspace + +environment: + sdk: ^3.6.0 + +workspace: +'''; + for (final package in packages) { + workspacePubSpec += ''' + - $package/ +'''; + } + await File.fromUri(workspacePubSpecUri).writeAsString(workspacePubSpec); + + await runPubGet( + workingDirectory: tempUri, + logger: logger, + ); + } + + test('pub workspaces', () async { + final packageUri = tempUri.resolve('named_add_renamed/'); + await Directory.fromUri(tempUri.resolve('native_add/')) + .rename(packageUri.toFilePath()); + await makePubWorkspace([ + 'dart_app', + 'native_subtract', + // Do not fail on package with different name than directory + 'named_add_renamed', + // Do not fail on issues on packages not in dependencies. + 'cyclic_package_1', + 'cyclic_package_2', + ]); + + (await buildCodeAssets(packageUri, runPackageName: 'native_add'))!; + final buildDirectory = tempUri.resolve('.dart_tool/native_assets_builder/'); + final buildDirs = Directory.fromUri(buildDirectory) + .listSync() + .whereType() + .map((e) => e.uri.pathSegments.lastWhere((e) => e.isNotEmpty)) + .toList() + ..sort(); + expect(buildDirs, contains('native_add')); + // Do not build packages not in dependencies of runPackageName. + expect(buildDirs, isNot(contains('native_subtract'))); + + final logs = []; + (await buildCodeAssets( + tempUri.resolve('dart_app/'), + capturedLogs: logs, + ))!; + // Reuse hook results of other packages in the same workspace + expect(logs.join('\n'), contains('Skipping build for native_add')); + }); +} diff --git a/pkgs/native_assets_builder/test/helpers.dart b/pkgs/native_assets_builder/test/helpers.dart index c64a7f79e..76f0056bc 100644 --- a/pkgs/native_assets_builder/test/helpers.dart +++ b/pkgs/native_assets_builder/test/helpers.dart @@ -60,6 +60,27 @@ Future inTempDir( } } +Future tempDirForTest({String? prefix, bool keepTemp = false}) async { + final tempDir = await Directory.systemTemp.createTemp(prefix); + // Deal with Windows temp folder aliases. + final tempUri = + Directory(await tempDir.resolveSymbolicLinks()).uri.normalizePath(); + if ((!Platform.environment.containsKey(keepTempKey) || + Platform.environment[keepTempKey]!.isEmpty) && + !keepTemp) { + addTearDown(() async { + try { + await tempDir.delete(recursive: true); + } on FileSystemException { + // On Windows, the temp dir might still be locked even though all + // process invocations have finished. + if (!Platform.isWindows) rethrow; + } + }); + } + return tempUri; +} + /// Runs a [Process]. /// /// If [logger] is provided, stream stdout and stderr to it. diff --git a/pkgs/native_assets_builder/test_data/cyclic_package_1/pubspec.yaml b/pkgs/native_assets_builder/test_data/cyclic_package_1/pubspec.yaml index 716545293..1203f8e76 100644 --- a/pkgs/native_assets_builder/test_data/cyclic_package_1/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/cyclic_package_1/pubspec.yaml @@ -5,7 +5,7 @@ version: 0.1.0 publish_to: none environment: - sdk: '>=3.3.0 <4.0.0' + sdk: '>=3.6.0 <4.0.0' dependencies: cyclic_package_2: diff --git a/pkgs/native_assets_builder/test_data/cyclic_package_2/pubspec.yaml b/pkgs/native_assets_builder/test_data/cyclic_package_2/pubspec.yaml index 5326c0796..1b1a573fe 100644 --- a/pkgs/native_assets_builder/test_data/cyclic_package_2/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/cyclic_package_2/pubspec.yaml @@ -5,7 +5,7 @@ version: 0.1.0 publish_to: none environment: - sdk: '>=3.3.0 <4.0.0' + sdk: '>=3.6.0 <4.0.0' dependencies: cyclic_package_1: diff --git a/pkgs/native_assets_builder/test_data/dart_app/pubspec.yaml b/pkgs/native_assets_builder/test_data/dart_app/pubspec.yaml index b1a44c2a3..9177503c5 100644 --- a/pkgs/native_assets_builder/test_data/dart_app/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/dart_app/pubspec.yaml @@ -3,7 +3,7 @@ name: dart_app publish_to: none environment: - sdk: '>=3.3.0 <4.0.0' + sdk: '>=3.6.0 <4.0.0' dependencies: native_add: diff --git a/pkgs/native_assets_builder/test_data/fail_on_os_sdk_version_link/pubspec.yaml b/pkgs/native_assets_builder/test_data/fail_on_os_sdk_version_link/pubspec.yaml index 4d3ac3cb8..1f8f60efa 100644 --- a/pkgs/native_assets_builder/test_data/fail_on_os_sdk_version_link/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/fail_on_os_sdk_version_link/pubspec.yaml @@ -1,4 +1,4 @@ -name: fail_on_os_sdk_version +name: fail_on_os_sdk_version_link description: Fails the build due to OS API levels. version: 0.1.0 diff --git a/pkgs/native_assets_builder/test_data/native_add/pubspec.yaml b/pkgs/native_assets_builder/test_data/native_add/pubspec.yaml index 860ccbbad..d4192be56 100644 --- a/pkgs/native_assets_builder/test_data/native_add/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/native_add/pubspec.yaml @@ -5,7 +5,7 @@ version: 0.1.0 publish_to: none environment: - sdk: '>=3.3.0 <4.0.0' + sdk: '>=3.6.0 <4.0.0' dependencies: logging: ^1.1.1 diff --git a/pkgs/native_assets_builder/test_data/native_add_version_skew/hook/build.dart b/pkgs/native_assets_builder/test_data/native_add_version_skew/hook/build.dart index 445348dfb..89f512fdf 100644 --- a/pkgs/native_assets_builder/test_data/native_add_version_skew/hook/build.dart +++ b/pkgs/native_assets_builder/test_data/native_add_version_skew/hook/build.dart @@ -11,9 +11,9 @@ void main(List arguments) async { final packageName = config.packageName; final cbuilder = CBuilder.library( name: packageName, - assetName: 'src/${packageName}_bindings_generated.dart', + assetName: 'src/native_add_bindings_generated.dart', sources: [ - 'src/$packageName.c', + 'src/native_add.c', ], ); await cbuilder.run( diff --git a/pkgs/native_assets_builder/test_data/native_add_version_skew/pubspec.yaml b/pkgs/native_assets_builder/test_data/native_add_version_skew/pubspec.yaml index 310680f5c..db4c69a77 100644 --- a/pkgs/native_assets_builder/test_data/native_add_version_skew/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/native_add_version_skew/pubspec.yaml @@ -1,4 +1,4 @@ -name: native_add +name: native_add_version_skew description: Sums two numbers with native code. version: 0.1.0 diff --git a/pkgs/native_assets_builder/test_data/native_add_version_skew/test/native_add_test.dart b/pkgs/native_assets_builder/test_data/native_add_version_skew/test/native_add_test.dart index f34b5a6ea..16619b330 100644 --- a/pkgs/native_assets_builder/test_data/native_add_version_skew/test/native_add_test.dart +++ b/pkgs/native_assets_builder/test_data/native_add_version_skew/test/native_add_test.dart @@ -2,7 +2,7 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -import 'package:native_add/native_add.dart'; +import 'package:native_add_version_skew/native_add.dart'; import 'package:test/test.dart'; void main() { diff --git a/pkgs/native_assets_builder/test_data/native_subtract/pubspec.yaml b/pkgs/native_assets_builder/test_data/native_subtract/pubspec.yaml index 04f973ff1..d28be456d 100644 --- a/pkgs/native_assets_builder/test_data/native_subtract/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/native_subtract/pubspec.yaml @@ -5,7 +5,7 @@ version: 0.1.0 publish_to: none environment: - sdk: '>=3.3.0 <4.0.0' + sdk: '>=3.6.0 <4.0.0' dependencies: logging: ^1.1.1 diff --git a/pkgs/native_assets_builder/test_data/wrong_linker/pubspec.yaml b/pkgs/native_assets_builder/test_data/wrong_linker/pubspec.yaml index 81cea455c..a292d2b6c 100644 --- a/pkgs/native_assets_builder/test_data/wrong_linker/pubspec.yaml +++ b/pkgs/native_assets_builder/test_data/wrong_linker/pubspec.yaml @@ -1,4 +1,4 @@ -name: wrong_namespace_asset +name: wrong_linker description: Package that tries to add an asset with an id not prefixed by its package name. version: 0.1.0 From f677697f81348db227f0e0667c621370adba2d74 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Fri, 17 Jan 2025 17:03:39 +0100 Subject: [PATCH 2/3] changelog entry --- pkgs/native_assets_builder/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkgs/native_assets_builder/CHANGELOG.md b/pkgs/native_assets_builder/CHANGELOG.md index a0c5ece28..56f3ecf19 100644 --- a/pkgs/native_assets_builder/CHANGELOG.md +++ b/pkgs/native_assets_builder/CHANGELOG.md @@ -1,5 +1,7 @@ ## 0.11.0-wip +- **Breaking change** `runPackageName` is now required to properly support + pub workspaces. - Bump `package:native_assets_cli` to 0.11.0. ## 0.10.2 From 1a889f52fe8f135e75c2f63704f17e4afc77d85a Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Mon, 20 Jan 2025 09:56:15 +0100 Subject: [PATCH 3/3] remove root package root from PackageLayout --- .../lib/src/build_runner/build_planner.dart | 6 ++--- .../lib/src/build_runner/build_runner.dart | 8 +++--- .../src/package_layout/package_layout.dart | 25 ++++++++----------- .../test/build_runner/build_planner_test.dart | 14 +++++------ .../test/build_runner/build_runner_test.dart | 2 +- .../test/build_runner/helpers.dart | 16 ++++-------- .../build_runner/package_layout_test.dart | 6 ++--- 7 files changed, 33 insertions(+), 44 deletions(-) diff --git a/pkgs/native_assets_builder/lib/src/build_runner/build_planner.dart b/pkgs/native_assets_builder/lib/src/build_runner/build_planner.dart index 797e2281f..e9a62b004 100644 --- a/pkgs/native_assets_builder/lib/src/build_runner/build_planner.dart +++ b/pkgs/native_assets_builder/lib/src/build_runner/build_planner.dart @@ -22,8 +22,8 @@ class NativeAssetsBuildPlanner { required this.logger, }); - static Future fromRootPackageRoot({ - required Uri rootPackageRoot, + static Future fromWorkingDirectory({ + required Uri workingDirectory, required List packagesWithNativeAssets, required Uri dartExecutable, required Logger logger, @@ -35,7 +35,7 @@ class NativeAssetsBuildPlanner { 'deps', '--json', ], - workingDirectory: rootPackageRoot.toFilePath(), + workingDirectory: workingDirectory.toFilePath(), ); final packageGraph = PackageGraph.fromPubDepsJsonString(result.stdout as String); diff --git a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart index 14593c047..c63069aa9 100644 --- a/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart +++ b/pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart @@ -106,7 +106,7 @@ class NativeAssetsBuildRunner { required bool linkingEnabled, }) async { packageLayout ??= - await PackageLayout.fromRootPackageRoot(_fileSystem, workingDirectory); + await PackageLayout.fromWorkingDirectory(_fileSystem, workingDirectory); final (buildPlan, packageGraph) = await _makePlan( hook: Hook.build, @@ -213,7 +213,7 @@ class NativeAssetsBuildRunner { required BuildResult buildResult, }) async { packageLayout ??= - await PackageLayout.fromRootPackageRoot(_fileSystem, workingDirectory); + await PackageLayout.fromWorkingDirectory(_fileSystem, workingDirectory); final (buildPlan, packageGraph) = await _makePlan( hook: Hook.link, @@ -785,8 +785,8 @@ ${compileResult.stdout} final PackageGraph? packageGraph; switch (hook) { case Hook.build: - final planner = await NativeAssetsBuildPlanner.fromRootPackageRoot( - rootPackageRoot: packageLayout.rootPackageRoot, + final planner = await NativeAssetsBuildPlanner.fromWorkingDirectory( + workingDirectory: packageLayout.packageConfigUri.resolve('../'), packagesWithNativeAssets: packagesWithHook, dartExecutable: Uri.file(Platform.resolvedExecutable), logger: logger, diff --git a/pkgs/native_assets_builder/lib/src/package_layout/package_layout.dart b/pkgs/native_assets_builder/lib/src/package_layout/package_layout.dart index 9c39c66eb..c228ff5e7 100644 --- a/pkgs/native_assets_builder/lib/src/package_layout/package_layout.dart +++ b/pkgs/native_assets_builder/lib/src/package_layout/package_layout.dart @@ -16,11 +16,6 @@ import 'package:package_config/package_config.dart'; class PackageLayout { final FileSystem _fileSystem; - /// The root folder of the current dart invocation root package. - /// - /// `$rootPackageRoot`. - final Uri rootPackageRoot; - /// Package config containing the information of where to foot the root [Uri]s /// of other packages. /// @@ -30,8 +25,11 @@ class PackageLayout { final Uri packageConfigUri; - PackageLayout._(this._fileSystem, this.rootPackageRoot, this.packageConfig, - this.packageConfigUri); + PackageLayout._( + this._fileSystem, + this.packageConfig, + this.packageConfigUri, + ); factory PackageLayout.fromPackageConfig( FileSystem fileSystem, @@ -40,26 +38,23 @@ class PackageLayout { ) { assert(fileSystem.file(packageConfigUri).existsSync()); packageConfigUri = packageConfigUri.normalizePath(); - final rootPackageRoot = packageConfigUri.resolve('../'); return PackageLayout._( fileSystem, - rootPackageRoot, packageConfig, packageConfigUri, ); } - static Future fromRootPackageRoot( + static Future fromWorkingDirectory( FileSystem fileSystem, - Uri rootPackageRoot, + Uri workingDirectory, ) async { - rootPackageRoot = rootPackageRoot.normalizePath(); + workingDirectory = workingDirectory.normalizePath(); final packageConfigUri = - await findPackageConfig(fileSystem, rootPackageRoot); + await findPackageConfig(fileSystem, workingDirectory); assert(await fileSystem.file(packageConfigUri).exists()); final packageConfig = await loadPackageConfigUri(packageConfigUri!); - return PackageLayout._( - fileSystem, rootPackageRoot, packageConfig, packageConfigUri); + return PackageLayout._(fileSystem, packageConfig, packageConfigUri); } static Future findPackageConfig( diff --git a/pkgs/native_assets_builder/test/build_runner/build_planner_test.dart b/pkgs/native_assets_builder/test/build_runner/build_planner_test.dart index 695cfa01c..6adffd2d0 100644 --- a/pkgs/native_assets_builder/test/build_runner/build_planner_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/build_planner_test.dart @@ -35,7 +35,7 @@ void main() async { final graph = PackageGraph.fromPubDepsJsonString(result.stdout); - final packageLayout = await PackageLayout.fromRootPackageRoot( + final packageLayout = await PackageLayout.fromWorkingDirectory( const LocalFileSystem(), nativeAddUri); final packagesWithNativeAssets = await packageLayout.packagesWithAssets(Hook.build); @@ -60,13 +60,13 @@ void main() async { // First, run `pub get`, we need pub to resolve our dependencies. await runPubGet(workingDirectory: nativeAddUri, logger: logger); - final packageLayout = await PackageLayout.fromRootPackageRoot( + final packageLayout = await PackageLayout.fromWorkingDirectory( const LocalFileSystem(), nativeAddUri); final packagesWithNativeAssets = await packageLayout.packagesWithAssets(Hook.build); final nativeAssetsBuildPlanner = - await NativeAssetsBuildPlanner.fromRootPackageRoot( - rootPackageRoot: nativeAddUri, + await NativeAssetsBuildPlanner.fromWorkingDirectory( + workingDirectory: nativeAddUri, packagesWithNativeAssets: packagesWithNativeAssets, dartExecutable: Uri.file(Platform.resolvedExecutable), logger: logger, @@ -87,13 +87,13 @@ void main() async { // First, run `pub get`, we need pub to resolve our dependencies. await runPubGet(workingDirectory: nativeAddUri, logger: logger); - final packageLayout = await PackageLayout.fromRootPackageRoot( + final packageLayout = await PackageLayout.fromWorkingDirectory( const LocalFileSystem(), nativeAddUri); final packagesWithNativeAssets = await packageLayout.packagesWithAssets(Hook.build); final nativeAssetsBuildPlanner = - await NativeAssetsBuildPlanner.fromRootPackageRoot( - rootPackageRoot: nativeAddUri, + await NativeAssetsBuildPlanner.fromWorkingDirectory( + workingDirectory: nativeAddUri, packagesWithNativeAssets: packagesWithNativeAssets, dartExecutable: Uri.file(Platform.resolvedExecutable), logger: logger, diff --git a/pkgs/native_assets_builder/test/build_runner/build_runner_test.dart b/pkgs/native_assets_builder/test/build_runner/build_runner_test.dart index 39cc77780..55843110f 100644 --- a/pkgs/native_assets_builder/test/build_runner/build_runner_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/build_runner_test.dart @@ -65,7 +65,7 @@ void main() async { for (final passPackageLayout in [true, false]) { PackageLayout? packageLayout; if (passPackageLayout) { - packageLayout = await PackageLayout.fromRootPackageRoot( + packageLayout = await PackageLayout.fromWorkingDirectory( const LocalFileSystem(), packageUri); } final logMessages = []; diff --git a/pkgs/native_assets_builder/test/build_runner/helpers.dart b/pkgs/native_assets_builder/test/build_runner/helpers.dart index a04f328c2..644f3a6a5 100644 --- a/pkgs/native_assets_builder/test/build_runner/helpers.dart +++ b/pkgs/native_assets_builder/test/build_runner/helpers.dart @@ -68,10 +68,8 @@ Future build( Map? hookEnvironment, }) async { final targetOS = target?.os ?? OS.current; - final runPackageName_ = runPackageName ?? - (packageLayout?.rootPackageRoot ?? packageUri) - .pathSegments - .lastWhere((e) => e.isNotEmpty); + final runPackageName_ = + runPackageName ?? packageUri.pathSegments.lastWhere((e) => e.isNotEmpty); return await runWithLog(capturedLogs, () async { final result = await NativeAssetsBuildRunner( logger: logger, @@ -148,10 +146,8 @@ Future link( required List buildAssetTypes, }) async { final targetOS = target?.os ?? OS.current; - final runPackageName_ = runPackageName ?? - (packageLayout?.rootPackageRoot ?? packageUri) - .pathSegments - .lastWhere((e) => e.isNotEmpty); + final runPackageName_ = + runPackageName ?? packageUri.pathSegments.lastWhere((e) => e.isNotEmpty); return await runWithLog(capturedLogs, () async { final result = await NativeAssetsBuildRunner( logger: logger, @@ -226,9 +222,7 @@ Future<(BuildResult?, LinkResult?)> buildAndLink( }) async => await runWithLog(capturedLogs, () async { final runPackageName_ = runPackageName ?? - (packageLayout?.rootPackageRoot ?? packageUri) - .pathSegments - .lastWhere((e) => e.isNotEmpty); + packageUri.pathSegments.lastWhere((e) => e.isNotEmpty); final buildRunner = NativeAssetsBuildRunner( logger: logger, dartExecutable: dartExecutable, diff --git a/pkgs/native_assets_builder/test/build_runner/package_layout_test.dart b/pkgs/native_assets_builder/test/build_runner/package_layout_test.dart index f63cc61aa..577ee9a94 100644 --- a/pkgs/native_assets_builder/test/build_runner/package_layout_test.dart +++ b/pkgs/native_assets_builder/test/build_runner/package_layout_test.dart @@ -10,7 +10,7 @@ import '../helpers.dart'; import 'helpers.dart'; void main() async { - test('fromRootPackageRoot', () async { + test('fromWorkingDirectory', () async { await inTempDir((tempUri) async { await copyTestProjects(targetUri: tempUri); final nativeAddUri = tempUri.resolve('native_add/'); @@ -20,13 +20,13 @@ void main() async { const fileSystem = LocalFileSystem(); final packageLayout = - await PackageLayout.fromRootPackageRoot(fileSystem, nativeAddUri); + await PackageLayout.fromWorkingDirectory(fileSystem, nativeAddUri); final packageLayout2 = PackageLayout.fromPackageConfig( fileSystem, packageLayout.packageConfig, packageLayout.packageConfigUri, ); - expect(packageLayout.rootPackageRoot, packageLayout2.rootPackageRoot); + expect(packageLayout.packageConfigUri, packageLayout2.packageConfigUri); }); }); }