Skip to content

Commit c3a0347

Browse files
authored
Pass manifestPath to assetBundle in ResidentWebRunner and throw ToolExit in computeTransitiveDependencies (flutter#194474)
Passes `manifestPath: flutterProject.pubspecFile.path` to `assetBundle.needsBuild` and `assetBundle.build` in `ResidentWebRunner._updateDevFS` so that commands targeting a nested project (such as `flutter widget-preview start`) do not fall back to reading the root working directory's `pubspec.yaml` and `package_graph.json`. Also updates `computeTransitiveDependencies` to throw a `ToolExit` instead of a null-check error if a package in `package_graph.json` is missing from `package_config.json`. Fixes flutter#194444
1 parent 8980aca commit c3a0347

4 files changed

Lines changed: 113 additions & 6 deletions

File tree

‎packages/flutter_tools/lib/src/isolated/resident_web_runner.dart‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,9 @@ class ResidentWebRunner extends ResidentRunner {
731731
required bool resetCompiler,
732732
}) async {
733733
final bool isFirstUpload = !assetBundle.wasBuiltOnce();
734-
final bool rebuildBundle = assetBundle.needsBuild();
734+
final bool rebuildBundle = assetBundle.needsBuild(
735+
manifestPath: flutterProject.pubspecFile.path,
736+
);
735737
if (rebuildBundle) {
736738
logger.printTrace('Updating assets');
737739
final int result = await assetBundle.build(
@@ -740,6 +742,7 @@ class ResidentWebRunner extends ResidentRunner {
740742
environment: environment,
741743
logger: logger,
742744
),
745+
manifestPath: flutterProject.pubspecFile.path,
743746
packageConfigPath: debuggingOptions.buildInfo.packageConfigPath,
744747
targetPlatform: TargetPlatform.web_javascript,
745748
);

‎packages/flutter_tools/lib/src/package_graph.dart‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,13 @@ Try running `flutter pub get`''');
7171
}
7272
packageNamesToVisit.addAll(dependencies);
7373

74-
result[current] = Dependency(
75-
current,
76-
packageConfig[current]!.root,
77-
isExclusiveDevDependency: true,
78-
);
74+
final Package? package = packageConfig[current];
75+
if (package == null) {
76+
throwToolExit('''
77+
Failed to parse ${resolvedGraph.file.path}: package `$current` is missing from package_config.json.
78+
Try running `flutter pub get`''');
79+
}
80+
result[current] = Dependency(current, package.root, isExclusiveDevDependency: true);
7981
}
8082

8183
// Do a second traversal of only the non-dev-dependencies, to patch up the

‎packages/flutter_tools/test/general.shard/package_graph_test.dart‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,23 @@ ${package.devDependencies.map((String d) => ' $d: {path: ../$d}').join('\n')}
154154
<String>['package_c'],
155155
);
156156
});
157+
158+
// Regression test for https://github.com/flutter/flutter/issues/194444.
159+
test('throws ToolExit when dependency is missing from package_config', () async {
160+
writePubspecs(<Package>[
161+
(name: 'my_app', dependencies: <String>[], devDependencies: <String>[]),
162+
]);
163+
writePackageGraph(<Package>[
164+
(name: 'my_app', dependencies: <String>['package_a'], devDependencies: <String>[]),
165+
(name: 'package_a', dependencies: <String>[], devDependencies: <String>[]),
166+
]);
167+
168+
final FlutterProject project = FlutterProject.fromDirectoryTest(fileSystem.directory('my_app'));
169+
final PackageConfig packageConfig = await loadPackageConfig(project.packageConfig);
170+
171+
expect(
172+
() => computeTransitiveDependencies(project, packageConfig),
173+
throwsToolExit(message: 'package `package_a` is missing from package_config.json'),
174+
);
175+
});
157176
}

‎packages/flutter_tools/test/general.shard/resident_web_runner_test.dart‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2567,6 +2567,56 @@ flutter:
25672567
Pub: ThrowingPub.new,
25682568
},
25692569
);
2570+
2571+
testUsingContext(
2572+
'passes manifestPath to assetBundle when cwd is not project directory',
2573+
() async {
2574+
final Directory projectDir = fileSystem.directory('/other_project')..createSync();
2575+
projectDir.childFile('pubspec.yaml').writeAsStringSync('name: other_project\n');
2576+
projectDir.childFile('lib/main.dart').createSync(recursive: true);
2577+
writePackageConfigFiles(directory: projectDir, mainLibName: 'other_project');
2578+
2579+
final FlutterProject project = FlutterProject.fromDirectoryTest(projectDir);
2580+
2581+
final ResidentRunner residentWebRunner = ResidentWebRunner(
2582+
flutterDevice,
2583+
flutterProject: project,
2584+
debuggingOptions: DebuggingOptions.enabled(
2585+
BuildInfo(
2586+
.debug,
2587+
null,
2588+
treeShakeIcons: false,
2589+
packageConfigPath: projectDir
2590+
.childDirectory('.dart_tool')
2591+
.childFile('package_config.json')
2592+
.path,
2593+
),
2594+
),
2595+
stayResident: false,
2596+
buildSystem: TestBuildSystem.all(BuildResult(success: true)),
2597+
buildTargets: const BuildTargetsImpl(),
2598+
toolContext: test_fakes.FakeToolContext(
2599+
fs: fileSystem,
2600+
systemClock: SystemClock.fixed(DateTime(2001)),
2601+
),
2602+
analytics: fakeAnalytics,
2603+
);
2604+
2605+
expect(fileSystem.currentDirectory.path, isNot(projectDir.path));
2606+
2607+
webDevFS.report = UpdateFSReport(success: true);
2608+
expect(await residentWebRunner.run(), 0);
2609+
final fakeAssetBundle = residentWebRunner.assetBundle as FakeCapturingAssetBundle;
2610+
expect(fakeAssetBundle.lastNeedsBuildManifestPath, project.pubspecFile.path);
2611+
expect(fakeAssetBundle.lastBuildManifestPath, project.pubspecFile.path);
2612+
},
2613+
overrides: <Type, Generator>{
2614+
FileSystem: () => fileSystem,
2615+
ProcessManager: () => processManager,
2616+
Pub: ThrowingPub.new,
2617+
AssetBundleFactory: FakeAssetBundleFactory.new,
2618+
},
2619+
);
25702620
});
25712621
}
25722622

@@ -2991,3 +3041,36 @@ class FakeShaderCompiler implements DevelopmentShaderCompiler {
29913041
@override
29923042
bool areDependenciesModified(DevFSContent shaderContent) => false;
29933043
}
3044+
3045+
class FakeCapturingAssetBundle extends Fake implements AssetBundle {
3046+
String? lastNeedsBuildManifestPath;
3047+
String? lastBuildManifestPath;
3048+
3049+
@override
3050+
bool wasBuiltOnce() => false;
3051+
3052+
@override
3053+
bool needsBuild({String manifestPath = 'pubspec.yaml'}) {
3054+
lastNeedsBuildManifestPath = manifestPath;
3055+
return true;
3056+
}
3057+
3058+
@override
3059+
Future<int> build({
3060+
FlutterHookResult? flutterHookResult,
3061+
String manifestPath = 'pubspec.yaml',
3062+
required String packageConfigPath,
3063+
bool deferredComponentsEnabled = false,
3064+
required TargetPlatform targetPlatform,
3065+
String? flavor,
3066+
bool includeAssetsFromDevDependencies = false,
3067+
}) async {
3068+
lastBuildManifestPath = manifestPath;
3069+
return 0;
3070+
}
3071+
}
3072+
3073+
class FakeAssetBundleFactory implements AssetBundleFactory {
3074+
@override
3075+
AssetBundle createBundle() => FakeCapturingAssetBundle();
3076+
}

0 commit comments

Comments
 (0)