From ea86177090e31565c70eca7f2c3cd778bc6fb71c Mon Sep 17 00:00:00 2001 From: David Morgan Date: Wed, 12 Nov 2025 11:24:15 +0100 Subject: [PATCH] Fix running from a subdirectory. --- build_runner/CHANGELOG.md | 5 +++ .../lib/src/build_plan/package_graph.dart | 3 ++ build_runner/lib/src/build_runner.dart | 12 ++++++- build_runner/pubspec.yaml | 2 +- ...build_command_packages_and_paths_test.dart | 33 +++++++++++++++++++ build_test/CHANGELOG.md | 4 +++ build_test/pubspec.yaml | 4 +-- 7 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 build_runner/test/integration_tests/build_command_packages_and_paths_test.dart diff --git a/build_runner/CHANGELOG.md b/build_runner/CHANGELOG.md index 9c78aefca..7965f308f 100644 --- a/build_runner/CHANGELOG.md +++ b/build_runner/CHANGELOG.md @@ -1,3 +1,8 @@ +## 2.10.3-wip + +- Bug fix: fix crash when you run `dart run build_runner build` in a + subdirectory of a package. + ## 2.10.2 - Bug fix: fix issue with webdev failing due to a modification during a build. diff --git a/build_runner/lib/src/build_plan/package_graph.dart b/build_runner/lib/src/build_plan/package_graph.dart index 9b8128913..48d3f2173 100644 --- a/build_runner/lib/src/build_plan/package_graph.dart +++ b/build_runner/lib/src/build_plan/package_graph.dart @@ -12,6 +12,7 @@ import 'package:yaml/yaml.dart'; import '../constants.dart'; import '../io/asset_path_provider.dart'; +import '../logging/build_log.dart'; /// The SDK package, we filter this to the core libs and dev compiler /// resources. @@ -67,6 +68,8 @@ class PackageGraph implements AssetPathProvider { /// Creates a [PackageGraph] for the package whose top level directory lives /// at [packagePath] (no trailing slash). static Future forPath(String packagePath) async { + buildLog.debug('forPath $packagePath'); + /// Read in the pubspec file and parse it as yaml. final pubspec = File(p.join(packagePath, 'pubspec.yaml')); if (!pubspec.existsSync()) { diff --git a/build_runner/lib/src/build_runner.dart b/build_runner/lib/src/build_runner.dart index c9a8528d3..7849715c9 100644 --- a/build_runner/lib/src/build_runner.dart +++ b/build_runner/lib/src/build_runner.dart @@ -67,7 +67,17 @@ class BuildRunner { // Option parsing depends on the package name in `pubspec.yaml`. // Fortunately, `dart run build_runner` checks that `pubspec.yaml` is - // present and valid, so there must be a `name`. + // present in the current or a parent directory, and that it's valid, so + // there must be a `name`. + // + // Start by changing the current directory to the package root. + while (!File(p.join(Directory.current.path, 'pubspec.yaml')).existsSync()) { + final parent = Directory.current.parent; + if (parent.path == Directory.current.path) { + throw StateError('Missing pubspec.yaml.'); + } + Directory.current = parent; + } final rootPackage = (loadYaml(File(p.join(p.current, 'pubspec.yaml')).readAsStringSync()) as YamlMap)['name']! diff --git a/build_runner/pubspec.yaml b/build_runner/pubspec.yaml index 97f29c808..5de41d091 100644 --- a/build_runner/pubspec.yaml +++ b/build_runner/pubspec.yaml @@ -1,5 +1,5 @@ name: build_runner -version: 2.10.2 +version: 2.10.3-wip description: A build system for Dart code generation and modular compilation. repository: https://github.com/dart-lang/build/tree/master/build_runner resolution: workspace diff --git a/build_runner/test/integration_tests/build_command_packages_and_paths_test.dart b/build_runner/test/integration_tests/build_command_packages_and_paths_test.dart new file mode 100644 index 000000000..830341317 --- /dev/null +++ b/build_runner/test/integration_tests/build_command_packages_and_paths_test.dart @@ -0,0 +1,33 @@ +// 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. + +@Tags(['integration4']) +library; + +import 'package:test/test.dart'; + +import '../common/common.dart'; + +void main() async { + test('build command packages and paths', () async { + final pubspecs = await Pubspecs.load(); + final tester = BuildRunnerTester(pubspecs); + + tester.writeFixturePackage( + FixturePackages.copyBuilder(buildToCache: true, applyToAllPackages: true), + ); + tester.writePackage( + name: 'root_pkg', + dependencies: ['build_runner'], + pathDependencies: ['builder_pkg'], + files: {'lib/a.txt': 'a'}, + ); + + // Runs in a subdirectory of the package. + await tester.run('root_pkg/lib', 'dart run build_runner build'); + expect(tester.readFileTree('root_pkg/.dart_tool/build/generated'), { + 'root_pkg/lib/a.txt.copy': 'a', + }); + }); +} diff --git a/build_test/CHANGELOG.md b/build_test/CHANGELOG.md index e4b563617..874ef8c3c 100644 --- a/build_test/CHANGELOG.md +++ b/build_test/CHANGELOG.md @@ -1,3 +1,7 @@ +## 3.5.3-wip + +- Use `build_runner` 2.10.3. + ## 3.5.2 - Use `build_runner` 2.10.2. diff --git a/build_test/pubspec.yaml b/build_test/pubspec.yaml index 95cc0a7e3..824d712c6 100644 --- a/build_test/pubspec.yaml +++ b/build_test/pubspec.yaml @@ -1,6 +1,6 @@ name: build_test description: Utilities for writing unit tests of Builders. -version: 3.5.2 +version: 3.5.3-wip repository: https://github.com/dart-lang/build/tree/master/build_test resolution: workspace @@ -10,7 +10,7 @@ environment: dependencies: build: ^4.0.0 build_config: ^1.0.0 - build_runner: '2.10.2' + build_runner: '2.10.3-wip' built_collection: ^5.1.1 crypto: ^3.0.0 glob: ^2.0.0