Skip to content

Commit 20c27dc

Browse files
authored
Fix running from a subdirectory. (#4276)
1 parent 98f0405 commit 20c27dc

File tree

7 files changed

+59
-4
lines changed

7 files changed

+59
-4
lines changed

build_runner/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.10.3-wip
2+
3+
- Bug fix: fix crash when you run `dart run build_runner build` in a
4+
subdirectory of a package.
5+
16
## 2.10.2
27

38
- Bug fix: fix issue with webdev failing due to a modification during a build.

build_runner/lib/src/build_plan/package_graph.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:yaml/yaml.dart';
1212

1313
import '../constants.dart';
1414
import '../io/asset_path_provider.dart';
15+
import '../logging/build_log.dart';
1516

1617
/// The SDK package, we filter this to the core libs and dev compiler
1718
/// resources.
@@ -67,6 +68,8 @@ class PackageGraph implements AssetPathProvider {
6768
/// Creates a [PackageGraph] for the package whose top level directory lives
6869
/// at [packagePath] (no trailing slash).
6970
static Future<PackageGraph> forPath(String packagePath) async {
71+
buildLog.debug('forPath $packagePath');
72+
7073
/// Read in the pubspec file and parse it as yaml.
7174
final pubspec = File(p.join(packagePath, 'pubspec.yaml'));
7275
if (!pubspec.existsSync()) {

build_runner/lib/src/build_runner.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,17 @@ class BuildRunner {
6767

6868
// Option parsing depends on the package name in `pubspec.yaml`.
6969
// Fortunately, `dart run build_runner` checks that `pubspec.yaml` is
70-
// present and valid, so there must be a `name`.
70+
// present in the current or a parent directory, and that it's valid, so
71+
// there must be a `name`.
72+
//
73+
// Start by changing the current directory to the package root.
74+
while (!File(p.join(Directory.current.path, 'pubspec.yaml')).existsSync()) {
75+
final parent = Directory.current.parent;
76+
if (parent.path == Directory.current.path) {
77+
throw StateError('Missing pubspec.yaml.');
78+
}
79+
Directory.current = parent;
80+
}
7181
final rootPackage =
7282
(loadYaml(File(p.join(p.current, 'pubspec.yaml')).readAsStringSync())
7383
as YamlMap)['name']!

build_runner/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: build_runner
2-
version: 2.10.2
2+
version: 2.10.3-wip
33
description: A build system for Dart code generation and modular compilation.
44
repository: https://github.com/dart-lang/build/tree/master/build_runner
55
resolution: workspace
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
@Tags(['integration4'])
6+
library;
7+
8+
import 'package:test/test.dart';
9+
10+
import '../common/common.dart';
11+
12+
void main() async {
13+
test('build command packages and paths', () async {
14+
final pubspecs = await Pubspecs.load();
15+
final tester = BuildRunnerTester(pubspecs);
16+
17+
tester.writeFixturePackage(
18+
FixturePackages.copyBuilder(buildToCache: true, applyToAllPackages: true),
19+
);
20+
tester.writePackage(
21+
name: 'root_pkg',
22+
dependencies: ['build_runner'],
23+
pathDependencies: ['builder_pkg'],
24+
files: {'lib/a.txt': 'a'},
25+
);
26+
27+
// Runs in a subdirectory of the package.
28+
await tester.run('root_pkg/lib', 'dart run build_runner build');
29+
expect(tester.readFileTree('root_pkg/.dart_tool/build/generated'), {
30+
'root_pkg/lib/a.txt.copy': 'a',
31+
});
32+
});
33+
}

build_test/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 3.5.3-wip
2+
3+
- Use `build_runner` 2.10.3.
4+
15
## 3.5.2
26

37
- Use `build_runner` 2.10.2.

build_test/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: build_test
22
description: Utilities for writing unit tests of Builders.
3-
version: 3.5.2
3+
version: 3.5.3-wip
44
repository: https://github.com/dart-lang/build/tree/master/build_test
55
resolution: workspace
66

@@ -10,7 +10,7 @@ environment:
1010
dependencies:
1111
build: ^4.0.0
1212
build_config: ^1.0.0
13-
build_runner: '2.10.2'
13+
build_runner: '2.10.3-wip'
1414
built_collection: ^5.1.1
1515
crypto: ^3.0.0
1616
glob: ^2.0.0

0 commit comments

Comments
 (0)