Skip to content

[hooks_runner] Support building assets for dev deps #2342

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

Merged
merged 3 commits into from
Jun 3, 2025
Merged
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
1 change: 1 addition & 0 deletions pkgs/code_assets/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ analyzer:

linter:
rules:
- avoid_positional_boolean_parameters
- dangling_library_doc_comments
- prefer_const_declarations
- prefer_expression_function_bodies
Expand Down
1 change: 1 addition & 0 deletions pkgs/data_assets/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ analyzer:

linter:
rules:
- avoid_positional_boolean_parameters
- dangling_library_doc_comments
- prefer_const_declarations
- prefer_expression_function_bodies
Expand Down
1 change: 1 addition & 0 deletions pkgs/hooks/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ analyzer:

linter:
rules:
- avoid_positional_boolean_parameters
- dangling_library_doc_comments
- prefer_const_declarations
- prefer_expression_function_bodies
Expand Down
5 changes: 5 additions & 0 deletions pkgs/hooks_runner/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.21.0

* Add `includeDevDependencies` param to `BuildLayout` to enable building the
assets for dev dependencies of the `runPackage`.

## 0.20.2

* Add `dart:developer` `TimelineEvent`s to enable performance tracing for
Expand Down
1 change: 1 addition & 0 deletions pkgs/hooks_runner/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ analyzer:

linter:
rules:
- avoid_positional_boolean_parameters
- dangling_library_doc_comments
- prefer_const_declarations
- prefer_expression_function_bodies
Expand Down
29 changes: 24 additions & 5 deletions pkgs/hooks_runner/lib/src/build_runner/build_planner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class NativeAssetsBuildPlanner {
final packageGraphJson = await packageGraphJsonFile.readAsString();
final packageGraph = PackageGraph.fromPackageGraphJsonString(
packageGraphJson,
packageLayout.runPackageName,
includeDevDependencies: packageLayout.includeDevDependencies,
);
final packageGraphFromRunPackage = packageGraph.subGraph(
packageLayout.runPackageName,
Expand Down Expand Up @@ -182,12 +184,21 @@ class PackageGraph {

PackageGraph(this.map);

factory PackageGraph.fromPackageGraphJsonString(String json) =>
PackageGraph.fromPackageGraphJson(
jsonDecode(json) as Map<dynamic, dynamic>,
);
factory PackageGraph.fromPackageGraphJsonString(
String json,
String runPackageName, {
required bool includeDevDependencies,
}) => PackageGraph.fromPackageGraphJson(
jsonDecode(json) as Map<dynamic, dynamic>,
runPackageName,
includeDevDependencies: includeDevDependencies,
);

factory PackageGraph.fromPackageGraphJson(Map<dynamic, dynamic> map) {
factory PackageGraph.fromPackageGraphJson(
Map<dynamic, dynamic> map,
String runPackageName, {
required bool includeDevDependencies,
}) {
final result = <String, List<String>>{};
final packages = map['packages'] as List<dynamic>;
for (final package in packages) {
Expand All @@ -196,6 +207,14 @@ class PackageGraph {
final dependencies = (package_['dependencies'] as List<dynamic>)
.whereType<String>()
.toList();
if (name == runPackageName && includeDevDependencies) {
final devDependencies =
(package_['devDependencies'] as List<dynamic>?)
?.whereType<String>()
.toList() ??
[];
dependencies.addAll(devDependencies);
}
result[name] = dependencies;
}
return PackageGraph(result);
Expand Down
32 changes: 24 additions & 8 deletions pkgs/hooks_runner/lib/src/package_layout/package_layout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,52 @@ class PackageLayout {
/// Only assets of transitive dependencies of [runPackageName] are built.
final String runPackageName;

/// Include the dev dependencies of [runPackageName].
final bool includeDevDependencies;

PackageLayout._(
this.packageConfig,
this.packageConfigUri,
this.runPackageName,
);
this.runPackageName, {
required this.includeDevDependencies,
});

factory PackageLayout.fromPackageConfig(
FileSystem fileSystem,
PackageConfig packageConfig,
Uri packageConfigUri,
String runPackageName,
) {
String runPackageName, {
required bool includeDevDependencies,
}) {
assert(fileSystem.file(packageConfigUri).existsSync());
packageConfigUri = packageConfigUri.normalizePath();
return PackageLayout._(packageConfig, packageConfigUri, runPackageName);
return PackageLayout._(
packageConfig,
packageConfigUri,
runPackageName,
includeDevDependencies: includeDevDependencies,
);
}

static Future<PackageLayout> fromWorkingDirectory(
FileSystem fileSystem,
Uri workingDirectory,
String runPackgeName,
) async {
String runPackageName, {
required bool includeDevDependencies,
}) async {
workingDirectory = workingDirectory.normalizePath();
final packageConfigUri = await findPackageConfig(
fileSystem,
workingDirectory,
);
assert(await fileSystem.file(packageConfigUri).exists());
final packageConfig = await loadPackageConfigUri(packageConfigUri!);
return PackageLayout._(packageConfig, packageConfigUri, runPackgeName);
return PackageLayout._(
packageConfig,
packageConfigUri,
runPackageName,
includeDevDependencies: includeDevDependencies,
);
}

static Future<Uri?> findPackageConfig(
Expand Down
2 changes: 1 addition & 1 deletion pkgs/hooks_runner/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: hooks_runner
description: >-
This package is the backend that invokes build hooks.

version: 0.20.2
version: 0.21.0

repository: https://github.com/dart-lang/native/tree/main/pkgs/hooks_runner

Expand Down
34 changes: 34 additions & 0 deletions pkgs/hooks_runner/test/build_runner/build_planner_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ void main() async {
const LocalFileSystem(),
nativeAddUri,
'native_add',
includeDevDependencies: false,
);
final nativeAssetsBuildPlanner =
await NativeAssetsBuildPlanner.fromPackageConfigUri(
Expand Down Expand Up @@ -60,6 +61,7 @@ void main() async {
const LocalFileSystem(),
nativeAddUri,
runPackageName,
includeDevDependencies: false,
);
final nativeAssetsBuildPlanner =
await NativeAssetsBuildPlanner.fromPackageConfigUri(
Expand All @@ -76,4 +78,36 @@ void main() async {
});
});
}

for (final includeDevDependencies in [true, false]) {
test('includeDevDependencies $includeDevDependencies', () async {
const runPackageName = 'dev_dependency_with_hook';
await inTempDir((tempUri) async {
await copyTestProjects(targetUri: tempUri);
final nativeAddUri = tempUri.resolve('$runPackageName/');

// First, run `pub get`, we need pub to resolve our dependencies.
await runPubGet(workingDirectory: nativeAddUri, logger: logger);

final packageLayout = await PackageLayout.fromWorkingDirectory(
const LocalFileSystem(),
nativeAddUri,
runPackageName,
includeDevDependencies: includeDevDependencies,
);
final nativeAssetsBuildPlanner =
await NativeAssetsBuildPlanner.fromPackageConfigUri(
packageConfigUri: nativeAddUri.resolve(
'.dart_tool/package_config.json',
),
dartExecutable: Uri.file(Platform.resolvedExecutable),
logger: logger,
packageLayout: packageLayout,
fileSystem: const LocalFileSystem(),
);
final buildPlan = await nativeAssetsBuildPlanner.makeBuildHookPlan();
expect(buildPlan.success.length, includeDevDependencies ? 1 : 0);
});
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ void main() async {
const LocalFileSystem(),
packageUri,
packageName,
includeDevDependencies: false,
);
final buildRunner = NativeAssetsBuildRunner(
logger: logger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ void main(List<String> args) async {
const LocalFileSystem(),
packageUri,
packageName,
includeDevDependencies: false,
);
final result =
await NativeAssetsBuildRunner(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void main(List<String> args) async {
const LocalFileSystem(),
packageUri,
packageName,
includeDevDependencies: false,
);
final result =
await NativeAssetsBuildRunner(
Expand Down
3 changes: 3 additions & 0 deletions pkgs/hooks_runner/test/build_runner/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Future<Result<BuildResult, HooksRunnerFailure>> build(
const LocalFileSystem(),
packageUri,
runPackageName_,
includeDevDependencies: false,
);
return await runWithLog(capturedLogs, () async {
final result =
Expand Down Expand Up @@ -164,6 +165,7 @@ Future<Result<LinkResult, HooksRunnerFailure>> link(
const LocalFileSystem(),
packageUri,
runPackageName_,
includeDevDependencies: false,
);
return await runWithLog(capturedLogs, () async {
final result =
Expand Down Expand Up @@ -235,6 +237,7 @@ Future<(BuildResult?, LinkResult?)> buildAndLink(
const LocalFileSystem(),
packageUri,
runPackageName_,
includeDevDependencies: false,
);
final buildRunner = NativeAssetsBuildRunner(
logger: logger,
Expand Down
2 changes: 2 additions & 0 deletions pkgs/hooks_runner/test/build_runner/package_layout_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ void main() async {
fileSystem,
nativeAddUri,
'native_add',
includeDevDependencies: false,
);
final packageLayout2 = PackageLayout.fromPackageConfig(
fileSystem,
packageLayout.packageConfig,
packageLayout.packageConfigUri,
'native_add',
includeDevDependencies: false,
);
expect(packageLayout.packageConfigUri, packageLayout2.packageConfigUri);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ dependency_overrides:
fileSystem,
packageUri,
runPackageName,
includeDevDependencies: false,
);
final builderNoHook = NativeAssetsBuildRunner(
logger: logger,
Expand Down
19 changes: 19 additions & 0 deletions pkgs/hooks_runner/test_data/dev_dependency_with_hook/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: dev_dependency_with_hook
description: Has a dev dependency with hook
version: 0.1.0

publish_to: none

resolution: workspace

environment:
sdk: '>=3.8.0 <4.0.0'

dependencies: {}

dev_dependencies:
ffigen: ^18.0.0
lints: ^5.1.1
native_add:
path: ../native_add/
test: ^1.25.15
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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 'package:native_add/native_add.dart';
import 'package:test/test.dart';

void main() {
test('native add test', () {
final result = add(4, 6);
expect(result, equals(10));
});
}
2 changes: 2 additions & 0 deletions pkgs/hooks_runner/test_data/manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
- depend_on_fail_build/pubspec.yaml
- depend_on_fail_build_app/bin/depend_on_fail_build_app.dart
- depend_on_fail_build_app/pubspec.yaml
- dev_dependency_with_hook/pubspec.yaml
- dev_dependency_with_hook/test/my_test.dart
- drop_dylib_link/bin/drop_dylib_link.dart
- drop_dylib_link/hook/build.dart
- drop_dylib_link/hook/link.dart
Expand Down
8 changes: 5 additions & 3 deletions pkgs/hooks_runner/test_data/manifest_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import 'dart:io';

void main(List<String> args) async {
final testDataDirectory = Directory.fromUri(Platform.script.resolve('.'));
updateManifest(testDataDirectory, false);
updateManifest(testDataDirectory, allowPartialProjects: false);
final all = testDataDirectory.listSync(recursive: true);
all.whereType<Directory>().forEach((e) => updateManifest(e, true));
all.whereType<Directory>().forEach(
(e) => updateManifest(e, allowPartialProjects: true),
);
}

const denyList = [
Expand All @@ -32,7 +34,7 @@ const partialProjects = [
'simple_link_change_asset',
];

void updateManifest(Directory directory, bool allowPartialProjects) {
void updateManifest(Directory directory, {required bool allowPartialProjects}) {
final manifestFile = File.fromUri(directory.uri.resolve('manifest.yaml'));
if (!manifestFile.existsSync()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ analyzer:

linter:
rules:
- avoid_positional_boolean_parameters
- dangling_library_doc_comments
- prefer_const_declarations
- prefer_expression_function_bodies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ analyzer:

linter:
rules:
- avoid_positional_boolean_parameters
- dangling_library_doc_comments
- prefer_const_declarations
- prefer_expression_function_bodies
Expand Down
1 change: 1 addition & 0 deletions pkgs/json_syntax_generator/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ analyzer:

linter:
rules:
- avoid_positional_boolean_parameters
- dangling_library_doc_comments
- prefer_const_declarations
- prefer_expression_function_bodies
Expand Down
1 change: 1 addition & 0 deletions pkgs/native_toolchain_c/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ analyzer:

linter:
rules:
- avoid_positional_boolean_parameters
- dangling_library_doc_comments
- prefer_const_declarations
- prefer_expression_function_bodies
Expand Down
1 change: 1 addition & 0 deletions pkgs/repo_lint_rules/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ analyzer:

linter:
rules:
- avoid_positional_boolean_parameters
- dangling_library_doc_comments
- prefer_const_declarations
- prefer_expression_function_bodies
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ workspace:
- pkgs/hooks_runner/test_data/dart_app
- pkgs/hooks_runner/test_data/depend_on_fail_build
- pkgs/hooks_runner/test_data/depend_on_fail_build_app
- pkgs/hooks_runner/test_data/dev_dependency_with_hook
- pkgs/hooks_runner/test_data/drop_dylib_link
- pkgs/hooks_runner/test_data/fail_build
- pkgs/hooks_runner/test_data/fail_on_os_sdk_version
Expand Down
Loading