Skip to content

Commit fd67622

Browse files
committed
Find .dart_tool/package_config.json by looking in parent directories
1 parent cbf2d55 commit fd67622

File tree

6 files changed

+127
-53
lines changed

6 files changed

+127
-53
lines changed

packages/devtools_app_shared/lib/src/service/service_manager.dart

+3-1
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,8 @@ class ServiceManager<T extends VmService> {
652652

653653
// TODO(kenz): consider caching this value for the duration of the VM service
654654
// connection.
655+
// TODO(https://github.com/dart-lang/sdk/issues/56784) there should be a
656+
// vm-service call doing this.
655657
/// Returns the root package directory for the main isolate.
656658
///
657659
/// If a non-null value is returned, the value will be a file URI String and
@@ -666,7 +668,7 @@ class ServiceManager<T extends VmService> {
666668
dtd: dtdManager.connection.value,
667669
)
668670
: null;
669-
_log.fine('rootPackageDirectoryForMainIsolate: $packageUriString');
671+
_log.shout('rootPackageDirectoryForMainIsolate: $packageUriString');
670672
return packageUriString;
671673
}
672674
}

packages/devtools_shared/lib/src/extensions/extension_manager.dart

+24-27
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:io';
6+
57
import 'package:collection/collection.dart';
68
import 'package:dtd/dtd.dart';
79
import 'package:extension_discovery/extension_discovery.dart';
@@ -105,13 +107,6 @@ class ExtensionsManager {
105107
if (root.toString() == rootFileUriString) continue;
106108

107109
await _addExtensionsForRoot(
108-
// TODO(https://github.com/flutter/devtools/issues/7944): this logic
109-
// assumes that the .dart_tool folder containing the
110-
// package_config.json file is in the same directory as the
111-
// pubspec.yaml file (since `dartToolingDaemon.getProjectRoots`
112-
// returns all directories within the IDE workspace roots that have
113-
// a pubspec.yaml file). This may be an incorrect assumption for
114-
// pub workspaces.
115110
root.toString(),
116111
logs: logs,
117112
parsingErrors: parsingErrors,
@@ -142,27 +137,33 @@ class ExtensionsManager {
142137
}) async {
143138
_assertUriFormat(rootFileUriString);
144139
final List<Extension> extensions;
145-
try {
146-
// TODO(https://github.com/flutter/devtools/issues/7944): this assumes
147-
// that the .dart_tool/package_config.json file is in the package root,
148-
// which may be an incorrect assumption for pub workspaces.
149-
final packageConfigPath = path.posix.join(
140+
String packageConfigPath;
141+
while (true) {
142+
packageConfigPath = path.url.join(
150143
rootFileUriString,
151144
'.dart_tool',
152145
'package_config.json',
153146
);
154-
extensions = await findExtensions(
155-
'devtools',
156-
packageConfig: Uri.parse(packageConfigPath),
157-
);
158-
logs.add(
159-
'ExtensionsManager._addExtensionsForRoot find extensions for '
160-
'config: $packageConfigPath, result: '
161-
'${extensions.map((e) => e.package).toList()}',
162-
);
163-
} catch (e) {
164-
rethrow;
147+
if (File.fromUri(Uri.parse(packageConfigPath)).existsSync()) {
148+
break;
149+
}
150+
final nextRootFileUriString = path.url.dirname(rootFileUriString);
151+
if (nextRootFileUriString == rootFileUriString) {
152+
// No package config was found.
153+
return;
154+
}
155+
rootFileUriString = nextRootFileUriString;
165156
}
157+
// TODO(sigurdm): Do we need to deduplicate package configs?
158+
extensions = await findExtensions(
159+
'devtools',
160+
packageConfig: Uri.parse(packageConfigPath),
161+
);
162+
logs.add(
163+
'ExtensionsManager._addExtensionsForRoot find extensions for '
164+
'config: $packageConfigPath, result: '
165+
'${extensions.map((e) => e.package).toList()}',
166+
);
166167

167168
for (final extension in extensions) {
168169
final config = extension.config;
@@ -188,10 +189,6 @@ class ExtensionsManager {
188189
final extensionConfig = DevToolsExtensionConfig.parse({
189190
...config,
190191
DevToolsExtensionConfig.extensionAssetsPathKey: location,
191-
// TODO(https://github.com/flutter/devtools/issues/7944): for pub
192-
// workspaces, we may want to store the devtools_options.yaml at the
193-
// same location as the workspace's .dart_tool/package_config.json
194-
// file.
195192
DevToolsExtensionConfig.devtoolsOptionsUriKey:
196193
path.join(rootFileUriString, devtoolsOptionsFileName),
197194
DevToolsExtensionConfig.isPubliclyHostedKey: isPubliclyHosted,

packages/devtools_shared/lib/src/server/server_api.dart

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class ServerApi {
6060
ServerApi? api,
6161
DtdInfo? dtd,
6262
}) {
63+
print('Handling ${request.url.path}');
6364
api ??= ServerApi();
6465
final queryParams = request.requestedUri.queryParameters;
6566
// TODO(kenz): break this switch statement up so that it uses helper methods

packages/devtools_shared/test/helpers/extension_test_manager.dart

+66-4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ class ExtensionTestManager {
4747
/// .dart_tool/ # Generated from 'pub get'
4848
/// package_config.json # Generated from 'pub get'
4949
/// pubspec.yaml
50+
/// workspace_root/
51+
/// .dart_tool/ # Generated from 'pub get'
52+
/// package_config.json # Generated from 'pub get'
53+
/// pubspec.yaml # Defines workspace
54+
/// wokspace_member
55+
/// pubspec.yaml
5056
/// extensions/
5157
/// static_extension_1/
5258
/// extension/
@@ -90,7 +96,7 @@ class ExtensionTestManager {
9096
expect(testDirectoryContents.length, 2);
9197
final packageRoots =
9298
Directory.fromUri(packagesRootUri).listSync().whereType<Directory>();
93-
expect(packageRoots.length, 3);
99+
expect(packageRoots.length, 4);
94100

95101
for (final packageRoot in packageRoots) {
96102
expect(File(p.join(packageRoot.path, 'pubspec.yaml')).existsSync(), true);
@@ -129,6 +135,12 @@ class ExtensionTestManager {
129135
/// .dart_tool/ # Generated from 'pub get'
130136
/// package_config.json # Generated from 'pub get'
131137
/// pubspec.yaml
138+
/// workspace_root/
139+
/// .dart_tool/ # Generated from 'pub get'
140+
/// package_config.json # Generated from 'pub get'
141+
/// pubspec.yaml # Defines workspace
142+
/// workspace_member/
143+
/// pubspec.yaml
132144
void _setupPackages({
133145
required bool includeDependenciesWithExtensions,
134146
required bool includeBadExtension,
@@ -152,6 +164,45 @@ class ExtensionTestManager {
152164
includeDependenciesWithExtensions: includeDependenciesWithExtensions,
153165
),
154166
);
167+
setupWorkspace(
168+
createTestPackageFrom(
169+
workspaceMember,
170+
includeDependenciesWithExtensions: includeDependenciesWithExtensions,
171+
),
172+
);
173+
}
174+
175+
void setupWorkspace(TestPackage package) {
176+
File(
177+
p.join(
178+
testDirectory.path,
179+
'packages',
180+
'workspace_root',
181+
'pubspec.yaml',
182+
),
183+
)
184+
..createSync(recursive: true)
185+
..writeAsStringSync('''
186+
name: _
187+
environment:
188+
sdk: "^3.5.0"
189+
workspace:
190+
- ${package.name}
191+
''');
192+
File(
193+
p.join(
194+
testDirectory.path,
195+
'packages',
196+
'workspace_root',
197+
package.name,
198+
'pubspec.yaml',
199+
),
200+
)
201+
..createSync(recursive: true)
202+
..writeAsStringSync('''
203+
${package.pubspecContent}
204+
resolution: workspace
205+
''');
155206
}
156207

157208
/// extensions/
@@ -239,6 +290,7 @@ TestPackage createTestPackageFrom(
239290
name: originalPackage.name,
240291
dependencies:
241292
includeDependenciesWithExtensions ? originalPackage.dependencies : [],
293+
pathToExtensions: originalPackage.pathToExtensions,
242294
);
243295
}
244296

@@ -258,6 +310,11 @@ final otherRoot2Package = TestPackage(
258310
name: 'other_root_2',
259311
dependencies: [newerStaticExtension1Package],
260312
);
313+
final workspaceMember = TestPackage(
314+
name: 'workspace_member',
315+
dependencies: [staticExtension1Package],
316+
pathToExtensions: '../../../extensions',
317+
);
261318

262319
final driftPackage = TestPackageWithExtension(
263320
name: 'drift',
@@ -355,15 +412,20 @@ environment:
355412
}
356413

357414
class TestPackage {
358-
const TestPackage({required this.name, required this.dependencies});
415+
const TestPackage({
416+
required this.name,
417+
required this.dependencies,
418+
this.pathToExtensions = '../../extensions',
419+
});
359420

360421
final String name;
361422
final List<TestPackageWithExtension> dependencies;
423+
final String pathToExtensions;
362424

363425
String get pubspecContent => '''
364426
name: $name
365427
environment:
366-
sdk: ">=3.4.0-282.1.beta <4.0.0"
428+
sdk: "^3.5.0"
367429
dependencies:
368430
${_dependenciesAsString()}
369431
''';
@@ -378,7 +440,7 @@ ${_dependenciesAsString()}
378440
sb
379441
..writeln() // Add a new line for the path dependency.
380442
..writeln(
381-
' path: ../../extensions/${dep.relativePathFromExtensions}',
443+
' path: $pathToExtensions/${dep.relativePathFromExtensions}',
382444
);
383445
}
384446
}

packages/devtools_shared/test/helpers/extension_test_manager_test.dart

+8-8
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ materialIconCodePoint: 58634
179179
'''
180180
name: my_app
181181
environment:
182-
sdk: ">=3.4.0-282.1.beta <4.0.0"
182+
sdk: "^3.5.0"
183183
dependencies:
184184
drift: 2.16.0
185185
provider: 6.1.2
@@ -198,7 +198,7 @@ dependencies:
198198
'''
199199
name: my_app
200200
environment:
201-
sdk: ">=3.4.0-282.1.beta <4.0.0"
201+
sdk: "^3.5.0"
202202
dependencies:
203203
drift: 2.16.0
204204
provider: 6.1.2
@@ -219,7 +219,7 @@ dependencies:
219219
'''
220220
name: other_root_1
221221
environment:
222-
sdk: ">=3.4.0-282.1.beta <4.0.0"
222+
sdk: "^3.5.0"
223223
dependencies:
224224
static_extension_1:
225225
path: ../../extensions/static_extension_1
@@ -238,7 +238,7 @@ dependencies:
238238
'''
239239
name: other_root_2
240240
environment:
241-
sdk: ">=3.4.0-282.1.beta <4.0.0"
241+
sdk: "^3.5.0"
242242
dependencies:
243243
static_extension_1:
244244
path: ../../extensions/newer/static_extension_1
@@ -257,7 +257,7 @@ dependencies:
257257
'''
258258
name: my_app
259259
environment:
260-
sdk: ">=3.4.0-282.1.beta <4.0.0"
260+
sdk: "^3.5.0"
261261
dependencies:
262262
263263
''',
@@ -271,7 +271,7 @@ dependencies:
271271
'''
272272
name: my_app
273273
environment:
274-
sdk: ">=3.4.0-282.1.beta <4.0.0"
274+
sdk: "^3.5.0"
275275
dependencies:
276276
277277
''',
@@ -285,7 +285,7 @@ dependencies:
285285
'''
286286
name: other_root_1
287287
environment:
288-
sdk: ">=3.4.0-282.1.beta <4.0.0"
288+
sdk: "^3.5.0"
289289
dependencies:
290290
291291
''',
@@ -299,7 +299,7 @@ dependencies:
299299
'''
300300
name: other_root_2
301301
environment:
302-
sdk: ">=3.4.0-282.1.beta <4.0.0"
302+
sdk: "^3.5.0"
303303
dependencies:
304304
305305
''',

0 commit comments

Comments
 (0)