resolveRelativeFileUri static method

Uri resolveRelativeFileUri(
  1. Uri uri
)

Resolves a relative file uri (a relative path without a scheme, as Uri.file('bin/main.dart') and Uri.parse('bin/main.dart') return) against the current working directory (Directory.current).

Isolate.spawnUri resolves a relative Uri against the current isolate's script URI, not the working directory, which is rarely what is meant by a relative file path.

Any other uri is returned unchanged: URIs with a scheme (file:, package:, http:, https:, data:, ...), with an authority, or with an absolute path. (Note that Dart always normalizes the path of a file: URI to an absolute one: Uri.parse('file:bin/main.dart') is file:///bin/main.dart.)

Implementation

static Uri resolveRelativeFileUri(Uri uri) {
  if (uri.hasScheme ||
      uri.hasAuthority ||
      uri.hasAbsolutePath ||
      uri.path.isEmpty) {
    return uri;
  }

  // On Windows `Directory.current.uri` is `file:///C:/...`, and a relative
  // `Uri.file(r'bin\main.dart', windows: true)` has the path `bin/main.dart`.
  return Directory.current.uri.resolveUri(uri);
}