resolveUri static method

Future<Uri?> resolveUri(
  1. Uri uri
)

Resolves a package URI to its file system location.

Parameters:

  • uri: The URI to resolve

Returns:

  • The resolved file URI for package: URIs
  • The original URI for file: URIs
  • null for dart: URIs or unresolvable URIs

Example:

final resolved = await ReflectUtils.resolveUri(
  Uri.parse('package:path/path.dart'),
);

Implementation

static Future<Uri?> resolveUri(Uri uri) async {
  // 1. Direct file URIs
  if (uri.scheme == "file") {
    return uri;
  }

  // 2. Resolve package: URIs using Isolate API
  if (uri.scheme == "package") {
    return await Isolate.resolvePackageUri(uri);
  }

  // 3. Resolve dart: URIs (e.g., dart:core/string.dart)
  if (uri.scheme == "dart") {
    // Platform.resolvedExecutable gives path/to/sdk/bin/dart
    // The libraries are located in path/to/sdk/lib/
    final sdkPath = p.dirname(p.dirname(Platform.resolvedExecutable));
    final libDir = p.join(sdkPath, 'lib');

    // Convert 'dart:core/string.dart' to 'core/string.dart'
    String internalPath = uri.path;

    // If the path is just 'core', Dart usually maps it to 'core/core.dart'
    if (!internalPath.contains('/')) {
      internalPath = '$internalPath/$internalPath.dart';
    }

    final file = File(p.join(libDir, internalPath));
    return file.existsSync() ? file.uri : null;
  }

  return null;
}