[ Service ] Fix --write-service-info on Windows

Uri.parse(...) interprets the Windows drive letter as a scheme, which
causes File.fromUri(...) to throw an exception when it calls
Uri.toFilePath(). Using the File(...) constructor correctly handles
Windows drive letters.

Partly fixes https://github.com/dart-lang/sdk/issues/55133

TEST=pkg/dartdev/test/command/run_test.dart

Change-Id: Ie8c761289f1126491f4c2266b48922310df5d8da
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/356261
Commit-Queue: Ben Konyi <bkonyi@google.com>
Reviewed-by: Derek Xu <derekx@google.com>
This commit is contained in:
Ben Konyi 2024-03-11 20:16:08 +00:00 committed by Commit Queue
parent b0236fdde5
commit d440c020f8

View file

@ -433,7 +433,15 @@ class Server {
final serviceInfo = <String, dynamic>{
'uri': serverAddress.toString(),
};
final file = File.fromUri(Uri.parse(serviceInfoFilenameLocal));
const kFileScheme = 'file://';
// There's lots of URI parsing weirdness as Uri.parse doesn't do the right
// thing with Windows drive letters. Only use Uri.parse with known file
// URIs, and use Uri.file otherwise to properly handle drive letters in
// paths.
final uri = serviceInfoFilenameLocal.startsWith(kFileScheme)
? Uri.parse(serviceInfoFilenameLocal)
: Uri.file(serviceInfoFilenameLocal);
final file = File.fromUri(uri);
return file.writeAsString(json.encode(serviceInfo));
}