Skip to content
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

extension_discovery: add findPackageConfig() #590

Merged
merged 6 commits into from
Sep 27, 2024
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
2 changes: 2 additions & 0 deletions pkgs/extension_discovery/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- Require Dart 3.4.
- Update to the latest version of `package:dart_flutter_team_lints`.
- Add `findPackageConfig()` allowing for automatic discovery of the
packageConfig based on a package directory.

## 2.0.0

Expand Down
4 changes: 2 additions & 2 deletions pkgs/extension_discovery/lib/extension_discovery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'src/package_config.dart';
import 'src/registry.dart';
import 'src/yaml_config_format.dart';

export 'src/package_config.dart' show PackageConfigException;
export 'src/package_config.dart' show PackageConfigException, findPackageConfig;

/// Information about an extension for target package.
final class Extension {
Expand Down Expand Up @@ -101,7 +101,7 @@ final class Extension {
/// ### Caching results
///
/// When [useCache] is `true` then the detected extensions will be cached
/// in `.dart_tool/extension_discovery/<targetPackage>.yaml`.
/// in `.dart_tool/extension_discovery/<targetPackage>.json`.
/// This function will compare modification timestamps of
/// `.dart_tool/package_config.json` with the cache file, before reusing cached
/// results.
Expand Down
30 changes: 30 additions & 0 deletions pkgs/extension_discovery/lib/src/package_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,36 @@ import 'expect_json.dart';
typedef PackageConfigEntry = ({String name, Uri rootUri, Uri packageUri});
typedef PackageConfig = List<PackageConfigEntry>;

/// Searches in [packageDir] and all directories above for a
/// `.dart_tool/package_config.json` file. Returns the Uri of that file if
/// found.
///
/// Returns `null` if no file was found.
Uri? findPackageConfig(Uri packageDir) {
if (!packageDir.isScheme('file')) {
throw ArgumentError(
'Expected [packageDir] to be a file URI, got $packageDir',
);
}
if (!packageDir.path.endsWith('/')) {
packageDir = packageDir.replace(path: '${packageDir.path}/');
}
while (true) {
final packageConfigCandidate =
packageDir.resolve('.dart_tool/package_config.json');
Copy link
Contributor

Choose a reason for hiding this comment

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

this logic is assuming / is the separator, which won't be true for Windows. Should we be using the path package instead to support Windows paths?

CC @DanTup since you have resolved many windows path issues in the past.

Copy link
Contributor Author

@sigurdm sigurdm Sep 30, 2024

Choose a reason for hiding this comment

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

Good point - I think we are good here though.

These are file urls not file paths. Urls always use / as a separator.

Eg. this program:

import 'dart:io';

void main(List<String> args) {
  print(Uri.file('c:\\abc\\def\\', windows: true));
}

Prints:

file:///c:/abc/def/

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, this looks reasonable to me.

The things that usually cause issues are reading .path from a File URI, assuming it's correct for Windows (it's not, but it is correct for non-Windows), and going to/from Windows paths when running in the browser (because Dart always treats web as non-Windows even if the file:/// URI is a Windows path).

I note the code below is doing File.fromUri() which will call toFilePath() but since it's calling existsSync() I assume that this code only runs on the host machine (not in a browser) and therefore I don't think any of those gotchas apply here.

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 assume that this code only runs on the host machine (not in a browser)

Yes, this is running on the host.

try {
if (File.fromUri(packageConfigCandidate).existsSync()) {
return packageConfigCandidate;
}
} on IOException {
return null; // if we get a permission error, etc, we return null
}
final next = packageDir.resolve('..');
if (next == packageDir) return null;
packageDir = next;
}
}

/// Load list of packages and associated URIs from the
/// `.dart_tool/package_config.json` file in [packageConfigFile].
Future<PackageConfig> loadPackageConfig(
Expand Down
30 changes: 30 additions & 0 deletions pkgs/extension_discovery/test/package_config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,34 @@ void main() {
throwsA(isA<PackageConfigException>()),
);
});

test('`findPackageConfig()`', () async {
await d.dir('workspace', [
d.dir('.dart_tool', [
d.file('package_config.json'),
]),
d.dir('myapp', [])
]).create();

expect(
findPackageConfig(d.fileUri('workspace')),
d.fileUri('workspace/.dart_tool/package_config.json'),
);
expect(
findPackageConfig(d.fileUri('workspace/')),
d.fileUri('workspace/.dart_tool/package_config.json'),
);
expect(
findPackageConfig(d.fileUri('workspace/myapp')),
d.fileUri('workspace/.dart_tool/package_config.json'),
);
expect(
findPackageConfig(d.fileUri('.')),
isNull,
);
expect(
findPackageConfig(d.fileUri('foo')),
isNull,
);
});
}
2 changes: 1 addition & 1 deletion pkgs/extension_discovery/test/test_descriptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ Future<String> dart([
}

Future<String> dartPubGet(String folder) async =>
await dart('pub', 'get', '-C', d.path('myapp'));
await dart('pub', 'get', '-C', folder);