Skip to content

Added method findPackageConfigFilePath to find the package_config.json file #2587

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 7 commits into from
Feb 11, 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
3 changes: 3 additions & 0 deletions dwds/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 24.3.5
- Allow clients to specify the `packageConfigPath` in `LoadStrategy` class and associated providers.

## 24.3.4

- Added support for some debugging APIs with the DDC library bundle format. - [#2566](https://github.com/dart-lang/webdev/issues/2566), [#2573](https://github.com/dart-lang/webdev/issues/2573)
Expand Down
7 changes: 5 additions & 2 deletions dwds/lib/src/loaders/build_runner_require.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class BuildRunnerRequireStrategyProvider {
final ReloadConfiguration _configuration;
final AssetReader _assetReader;
final BuildSettings _buildSettings;
final String? _packageConfigPath;

late final RequireStrategy _requireStrategy = RequireStrategy(
_configuration,
Expand All @@ -34,14 +35,16 @@ class BuildRunnerRequireStrategyProvider {
_moduleInfoForProvider,
_assetReader,
_buildSettings,
packageConfigPath: _packageConfigPath,
);

BuildRunnerRequireStrategyProvider(
this._assetHandler,
this._configuration,
this._assetReader,
this._buildSettings,
);
this._buildSettings, {
String? packageConfigPath,
}) : _packageConfigPath = packageConfigPath;

RequireStrategy get strategy => _requireStrategy;

Expand Down
4 changes: 2 additions & 2 deletions dwds/lib/src/loaders/ddc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ class DdcStrategy extends LoadStrategy {
this._moduleInfoForProvider,
AssetReader assetReader,
this._buildSettings,
this._g3RelativePath,
this._g3RelativePath, {
String? packageConfigPath,
) : super(assetReader, packageConfigPath: packageConfigPath);
}) : super(assetReader, packageConfigPath: packageConfigPath);

@override
Handler get handler => (request) async {
Expand Down
4 changes: 2 additions & 2 deletions dwds/lib/src/loaders/ddc_library_bundle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ class DdcLibraryBundleStrategy extends LoadStrategy {
this._moduleInfoForProvider,
AssetReader assetReader,
this._buildSettings,
this._g3RelativePath,
this._g3RelativePath, {
String? packageConfigPath,
) : super(assetReader, packageConfigPath: packageConfigPath);
}) : super(assetReader, packageConfigPath: packageConfigPath);

@override
Handler get handler => (request) async {
Expand Down
27 changes: 17 additions & 10 deletions dwds/lib/src/loaders/frontend_server_strategy_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ abstract class FrontendServerStrategyProvider<T extends LoadStrategy> {
final Future<Map<String, String>> Function() _digestsProvider;
final String _basePath;
final BuildSettings _buildSettings;
final String? _packageConfigPath;

FrontendServerStrategyProvider(
this._configuration,
this._assetReader,
this._packageUriMapper,
this._digestsProvider,
this._buildSettings,
) : _basePath = _assetReader.basePath;
this._buildSettings, {
String? packageConfigPath,
}) : _basePath = _assetReader.basePath,
_packageConfigPath = packageConfigPath;

T get strategy;

Expand Down Expand Up @@ -118,16 +121,17 @@ class FrontendServerDdcStrategyProvider
_assetReader,
_buildSettings,
(String _) => null,
null,
packageConfigPath: _packageConfigPath,
);

FrontendServerDdcStrategyProvider(
super._configuration,
super._assetReader,
super._packageUriMapper,
super._digestsProvider,
super._buildSettings,
);
super._buildSettings, {
super.packageConfigPath,
});

@override
DdcStrategy get strategy => _ddcStrategy;
Expand All @@ -150,16 +154,17 @@ class FrontendServerDdcLibraryBundleStrategyProvider
_assetReader,
_buildSettings,
(String _) => null,
null,
packageConfigPath: _packageConfigPath,
);

FrontendServerDdcLibraryBundleStrategyProvider(
super._configuration,
super._assetReader,
super._packageUriMapper,
super._digestsProvider,
super._buildSettings,
);
super._buildSettings, {
super.packageConfigPath,
});

@override
DdcLibraryBundleStrategy get strategy => _libraryBundleStrategy;
Expand All @@ -179,15 +184,17 @@ class FrontendServerRequireStrategyProvider
_moduleInfoForProvider,
_assetReader,
_buildSettings,
packageConfigPath: _packageConfigPath,
);

FrontendServerRequireStrategyProvider(
super._configuration,
super._assetReader,
super._packageUriMapper,
super._digestsProvider,
super._buildSettings,
);
super._buildSettings, {
super.packageConfigPath,
});

@override
RequireStrategy get strategy => _requireStrategy;
Expand Down
5 changes: 3 additions & 2 deletions dwds/lib/src/loaders/require.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,9 @@ class RequireStrategy extends LoadStrategy {
this._serverPathForAppUri,
this._moduleInfoForProvider,
AssetReader assetReader,
this._buildSettings,
) : super(assetReader);
this._buildSettings, {
String? packageConfigPath,
}) : super(assetReader, packageConfigPath: packageConfigPath);

@override
Handler get handler => (request) async {
Expand Down
26 changes: 25 additions & 1 deletion dwds/lib/src/loaders/strategy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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 'dart:io';
import 'dart:typed_data';

import 'package:dwds/src/debugging/dart_runtime_debugger.dart';
Expand All @@ -20,7 +21,7 @@ abstract class LoadStrategy {
LoadStrategy(
this._assetReader, {
String? packageConfigPath,
}) : _packageConfigPath = packageConfigPath;
}) : _packageConfigPath = packageConfigPath ?? _findPackageConfigFilePath();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should instead provide this as an optional named parameter (optional named to avoid a breaking change) to each of the FrontendServerStrategyProviders e.g.:

FrontendServerDdcLibraryBundleStrategyProvider(
and avoid computing this in DWDS.

Flutter already calculates this, so we'd end up calculating it again. Flutter may also choose to configure the locations differently in tests for example.

I'd then imagine webdev calculating this separately and passing it to the provider (I guess in its case it'd use the BuildRunnerRequireStrategyProvider, which should also be updated in this PR).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about this approach and it makes sense to update each of the providers to allow passing a _packageConfigPath. It works for the flutter tools case since the info is already computed, we can simply pass it to the provider. However, for Webdev we don't have this details so what about we allow setting the value of _packageConfigPath for all the providers but in case we don't pass this value explicitly, we can compute it with _findPackageConfigFilePath. That way we can ensure that we always have this value set in all cases. Wdyt?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My original thought was maybe webdev should calculate it separately and then pass it to DWDS, but maybe it doesn't matter. I do like the idea of our default being a bit more robust for the case of monorepos, so I think your plan makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a similar method in Webdev to find the package config path and pass to DWDS and I also kept the default method in the load strategy class in case the value is not being passed.


/// The ID for this strategy.
///
Expand Down Expand Up @@ -83,6 +84,29 @@ abstract class LoadStrategy {
'package_config.json',
);

/// Returns the absolute file path of the `package_config.json` file in the `.dart_tool`
/// directory, searching recursively from the current directory hierarchy.
static String? _findPackageConfigFilePath() {
var candidateDir = Directory(DartUri.currentDirectory).absolute;

while (true) {
final candidatePackageConfigFile =
File(p.join(candidateDir.path, '.dart_tool', 'package_config.json'));

if (candidatePackageConfigFile.existsSync()) {
return candidatePackageConfigFile.path;
}

final parentDir = candidateDir.parent;
if (parentDir.path == candidateDir.path) {
// We've reached the root directory
return null;
}

candidateDir = parentDir;
}
}

/// Returns the bootstrap required for this [LoadStrategy].
///
/// The bootstrap is appended to the end of the entry point module.
Expand Down
2 changes: 1 addition & 1 deletion dwds/lib/src/version.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dwds/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: dwds
# Every time this changes you need to run `dart run build_runner build`.
version: 24.3.4
version: 24.3.5
description: >-
A service that proxies between the Chrome debug protocol and the Dart VM
service protocol.
Expand Down
Loading