Skip to content

Commit 5032bb4

Browse files
committed
Try to initialize Flutter if we don't otherwise find a Dart SDK through normal paths
Fixes #3580.
1 parent b4dad40 commit 5032bb4

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

‎src/extension/sdk/utils.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,16 @@ export class SdkUtils {
285285

286286
// If we're a Flutter workspace but we couldn't get the version, try running Flutter to initialise it first.
287287
// Do this before searching for the Dart SDK, as it might download the Dart SDK we'd like to find.
288+
let hasAttemptedFlutterInitialization = false;
288289
if (hasAnyFlutterProject && flutterSdkPath && !workspaceConfig.skipFlutterInitialization) {
289290
const flutterVersion = workspaceConfig?.flutterVersion ?? getSdkVersion(this.logger, { sdkRoot: flutterSdkPath });
290291
const flutterNeedsInitializing = !flutterVersion
291292
|| !fs.existsSync(path.join(flutterSdkPath, "bin/cache/dart-sdk"));
292293

293-
if (flutterNeedsInitializing)
294+
if (flutterNeedsInitializing) {
295+
hasAttemptedFlutterInitialization = true;
294296
await initializeFlutterSdk(this.logger, path.join(flutterSdkPath, flutterPath));
297+
}
295298
}
296299

297300
const dartSdkSearchPaths = [
@@ -313,12 +316,20 @@ export class SdkUtils {
313316
// Since we just blocked on a lot of sync FS, yield.
314317
await resolvedPromise;
315318

316-
const dartSdkResult = this.findDartSdk(dartSdkSearchPaths);
317-
const dartSdkPath = dartSdkResult.sdkPath;
319+
let dartSdkPath = this.findDartSdk(dartSdkSearchPaths).sdkPath;
318320

319321
// Since we just blocked on a lot of sync FS, yield.
320322
await resolvedPromise;
321323

324+
// If we still don't have a Dart SDK, but we do have a Flutter SDK and we did not already try to initialize, then
325+
// try again here. This could happen if we were not in a Flutter project (so didn't try to initialize before) but
326+
// still need a Dart SDK (for example, we were activated by running Flutter: New Project in an empty workspace.. we
327+
// wouldn't trigger the code above).
328+
if (!hasAttemptedFlutterInitialization && flutterSdkPath && !dartSdkPath) {
329+
await initializeFlutterSdk(this.logger, path.join(flutterSdkPath, flutterPath));
330+
dartSdkPath = this.findDartSdk([path.join(flutterSdkPath, "bin/cache/dart-sdk")]).sdkPath;
331+
}
332+
322333
return new WorkspaceContext(
323334
{
324335
dart: dartSdkPath,

0 commit comments

Comments
 (0)