resolveDartSdkPath function

String? resolveDartSdkPath()

Resolves the Dart SDK root directory, or null to let the analyzer fall back to its default (executable-relative) detection.

Resolution order (first match wins):

  1. DART_SDK environment variable.
  2. DART_HOME environment variable.
  3. Platform.resolvedExecutable — correct under dart run, where it points at the dart binary inside the SDK. For compiled binaries this points at the tool itself and is silently skipped (it won't look like an SDK).
  4. The dart executable on PATH (which/where), with symlinks resolved. Handles both the standalone layout (<sdk>/bin/dart) and the Flutter wrapper (<flutter>/bin/dart<flutter>/bin/cache/dart-sdk).
  5. The flutter executable on PATH<flutter>/bin/cache/dart-sdk.

Every candidate is validated with looksLikeDartSdk before being returned, so a stale environment variable or an unrelated dart on PATH never yields a bogus path. The result is cached for the lifetime of the process.

Implementation

String? resolveDartSdkPath() {
  if (_cacheEnabled && _cachedSdkPath != null) return _cachedSdkPath;

  final resolved = _resolveUncached();
  if (_cacheEnabled) _cachedSdkPath = resolved;
  return resolved;
}