blob: a471e11334d9b34655eab0288ef61ca211ade0f5 [file] [edit]
// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
// 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 'package:path/path.dart' as p;
/// The path to the Dart executable.
///
/// This is usually just Platform.resolvedExecutable. But when running flutter
/// tests, the resolvedExecutable will be flutter_tester, and Dart will be in a
/// directory a few levels up from it.
final String dartExecutable = _findDart();
String? _findDartInternal(String executablePath) {
var path = executablePath;
if (p.basenameWithoutExtension(path) == 'dart') return path;
final dartExe = 'dart${p.extension(path)}';
while (path.isNotEmpty) {
path = p.dirname(path);
final dartPath = p.normalize(p.join(path, dartExe));
if (File(dartPath).existsSync()) return dartPath;
final parent = p.dirname(path);
if (parent == path) break;
}
return null;
}
String _findDart() {
final path = _findDartInternal(Platform.resolvedExecutable) ??
_findDartInternal(Platform.executable);
if (path == null) {
throw Exception(
"Couldn't find Dart executable near ${Platform.resolvedExecutable} "
'or ${Platform.executable}',
);
}
return path;
}