From f8750b16bb9d7d51abe59a7695ffcbf506045202 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Wed, 30 Sep 2020 08:41:31 -0700 Subject: [PATCH] [flutter_tools] do not error doctor on missing vs code extension (#66780) In cases where the VSCode plugins are not located, display links to where they can be downloaded but do not surface an error. --- packages/flutter_tools/lib/src/doctor.dart | 2 +- .../flutter_tools/lib/src/vscode/vscode.dart | 59 ++++++++++--------- .../lib/src/vscode/vscode_validator.dart | 13 ++-- .../commands.shard/hermetic/doctor_test.dart | 9 +-- .../general.shard/vscode/vscode_test.dart | 17 ++---- 5 files changed, 45 insertions(+), 55 deletions(-) diff --git a/packages/flutter_tools/lib/src/doctor.dart b/packages/flutter_tools/lib/src/doctor.dart index 721192c99e..e9257002df 100644 --- a/packages/flutter_tools/lib/src/doctor.dart +++ b/packages/flutter_tools/lib/src/doctor.dart @@ -78,7 +78,7 @@ class _DefaultDoctorValidatorsProvider implements DoctorValidatorsProvider { final List ideValidators = [ ...AndroidStudioValidator.allValidators(globals.config, globals.platform, globals.fs, globals.userMessages), ...IntelliJValidator.installedValidators(globals.fs, globals.platform), - ...VsCodeValidator.installedValidators, + ...VsCodeValidator.installedValidators(globals.fs, globals.platform), ]; final ProxyValidator proxyValidator = ProxyValidator(platform: globals.platform); _validators = [ diff --git a/packages/flutter_tools/lib/src/vscode/vscode.dart b/packages/flutter_tools/lib/src/vscode/vscode.dart index 89ba0a1c34..1282a7d49f 100644 --- a/packages/flutter_tools/lib/src/vscode/vscode.dart +++ b/packages/flutter_tools/lib/src/vscode/vscode.dart @@ -2,27 +2,27 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'package:meta/meta.dart'; + import '../base/file_system.dart'; import '../base/platform.dart'; import '../base/utils.dart'; import '../base/version.dart'; import '../convert.dart'; import '../doctor.dart'; -import '../globals.dart' as globals; // Include VS Code insiders (useful for debugging). const bool _includeInsiders = false; - const String extensionIdentifier = 'Dart-Code.flutter'; const String extensionMarketplaceUrl = 'https://marketplace.visualstudio.com/items?itemName=$extensionIdentifier'; class VsCode { - VsCode._(this.directory, this.extensionDirectory, { Version version, this.edition }) + VsCode._(this.directory, this.extensionDirectory, { Version version, this.edition, @required FileSystem fileSystem}) : version = version ?? Version.unknown { - if (!globals.fs.isDirectorySync(directory)) { + if (!fileSystem.isDirectorySync(directory)) { _validationMessages.add(ValidationMessage.error('VS Code not found at $directory')); return; } else { @@ -31,16 +31,18 @@ class VsCode { // If the extensions directory doesn't exist at all, the listSync() // below will fail, so just bail out early. - const ValidationMessage notInstalledMessage = ValidationMessage.error( - 'Flutter extension not installed; install from\n$extensionMarketplaceUrl'); - if (!globals.fs.isDirectorySync(extensionDirectory)) { + const ValidationMessage notInstalledMessage = ValidationMessage( + 'Flutter extension can be installed from:', + contextUrl: extensionMarketplaceUrl, + ); + if (!fileSystem.isDirectorySync(extensionDirectory)) { _validationMessages.add(notInstalledMessage); return; } // Check for presence of extension. final String extensionIdentifierLower = extensionIdentifier.toLowerCase(); - final Iterable extensionDirs = globals.fs + final Iterable extensionDirs = fileSystem .directory(extensionDirectory) .listSync() .whereType() @@ -49,7 +51,6 @@ class VsCode { if (extensionDirs.isNotEmpty) { final FileSystemEntity extensionDir = extensionDirs.first; - _isValid = true; _extensionVersion = Version.parse( extensionDir.basename.substring('$extensionIdentifier-'.length)); _validationMessages.add(ValidationMessage('Flutter extension version $_extensionVersion')); @@ -62,15 +63,16 @@ class VsCode { String installPath, String extensionDirectory, { String edition, + @required FileSystem fileSystem, }) { final String packageJsonPath = - globals.fs.path.join(installPath, 'resources', 'app', 'package.json'); - final String versionString = _getVersionFromPackageJson(packageJsonPath); + fileSystem.path.join(installPath, 'resources', 'app', 'package.json'); + final String versionString = _getVersionFromPackageJson(packageJsonPath, fileSystem); Version version; if (versionString != null) { version = Version.parse(versionString); } - return VsCode._(installPath, extensionDirectory, version: version, edition: edition); + return VsCode._(installPath, extensionDirectory, version: version, edition: edition, fileSystem: fileSystem); } final String directory; @@ -78,11 +80,9 @@ class VsCode { final Version version; final String edition; - bool _isValid = false; Version _extensionVersion; final List _validationMessages = []; - bool get isValid => _isValid; String get productName => 'VS Code' + (edition != null ? ', $edition' : ''); Iterable get validationMessages => _validationMessages; @@ -92,13 +92,13 @@ class VsCode { Platform platform, ) { if (platform.isMacOS) { - return _installedMacOS(fileSystem); + return _installedMacOS(fileSystem, platform); } if (platform.isWindows) { return _installedWindows(fileSystem, platform); } if (platform.isLinux) { - return _installedLinux(fileSystem); + return _installedLinux(fileSystem, platform); } // VS Code isn't supported on the other platforms. return []; @@ -112,7 +112,7 @@ class VsCode { // macOS Extensions: // $HOME/.vscode/extensions // $HOME/.vscode-insiders/extensions - static List _installedMacOS(FileSystem fileSystem) { + static List _installedMacOS(FileSystem fileSystem, Platform platform) { return _findInstalled(<_VsCodeInstallLocation>[ _VsCodeInstallLocation( fileSystem.path.join('/Applications', 'Visual Studio Code.app', 'Contents'), @@ -120,7 +120,7 @@ class VsCode { ), _VsCodeInstallLocation( fileSystem.path.join( - globals.fsUtils.homeDirPath, + FileSystemUtils(fileSystem: fileSystem, platform: platform).homeDirPath, 'Applications', 'Visual Studio Code.app', 'Contents', @@ -134,7 +134,7 @@ class VsCode { ), _VsCodeInstallLocation( fileSystem.path.join( - globals.fsUtils.homeDirPath, + FileSystemUtils(fileSystem: fileSystem, platform: platform).homeDirPath, 'Applications', 'Visual Studio Code - Insiders.app', 'Contents', @@ -142,7 +142,7 @@ class VsCode { '.vscode-insiders', isInsiders: true, ), - ], fileSystem); + ], fileSystem, platform); } // Windows: @@ -206,7 +206,7 @@ class VsCode { isInsiders: true, ), ]; - return _findInstalled(searchLocations, fileSystem); + return _findInstalled(searchLocations, fileSystem, platform); } // Linux: @@ -215,7 +215,7 @@ class VsCode { // Linux Extensions: // $HOME/.vscode/extensions // $HOME/.vscode-insiders/extensions - static List _installedLinux(FileSystem fileSystem) { + static List _installedLinux(FileSystem fileSystem, Platform platform) { return _findInstalled(<_VsCodeInstallLocation>[ const _VsCodeInstallLocation('/usr/share/code', '.vscode'), const _VsCodeInstallLocation( @@ -223,12 +223,13 @@ class VsCode { '.vscode-insiders', isInsiders: true, ), - ], fileSystem); + ], fileSystem, platform); } static List _findInstalled( List<_VsCodeInstallLocation> allLocations, FileSystem fileSystem, + Platform platform, ) { final Iterable<_VsCodeInstallLocation> searchLocations = _includeInsiders @@ -240,7 +241,7 @@ class VsCode { for (final _VsCodeInstallLocation searchLocation in searchLocations) { if (fileSystem.isDirectorySync(searchLocation.installPath)) { final String extensionDirectory = fileSystem.path.join( - globals.fsUtils.homeDirPath, + FileSystemUtils(fileSystem: fileSystem, platform: platform).homeDirPath, searchLocation.extensionsFolder, 'extensions', ); @@ -248,6 +249,7 @@ class VsCode { searchLocation.installPath, extensionDirectory, edition: searchLocation.edition, + fileSystem: fileSystem, )); } } @@ -259,16 +261,15 @@ class VsCode { String toString() => 'VS Code ($version)${_extensionVersion != Version.unknown ? ', Flutter ($_extensionVersion)' : ''}'; - static String _getVersionFromPackageJson(String packageJsonPath) { - if (!globals.fs.isFileSync(packageJsonPath)) { + static String _getVersionFromPackageJson(String packageJsonPath, FileSystem fileSystem) { + if (!fileSystem.isFileSync(packageJsonPath)) { return null; } - final String jsonString = globals.fs.file(packageJsonPath).readAsStringSync(); + final String jsonString = fileSystem.file(packageJsonPath).readAsStringSync(); try { final Map jsonObject = castStringKeyedMap(json.decode(jsonString)); return jsonObject['version'] as String; - } on FormatException catch (err) { - globals.printTrace('Error parsing VSCode $packageJsonPath:\n$err'); + } on FormatException { return null; } } diff --git a/packages/flutter_tools/lib/src/vscode/vscode_validator.dart b/packages/flutter_tools/lib/src/vscode/vscode_validator.dart index 968bdad0e7..3d9a3f68b7 100644 --- a/packages/flutter_tools/lib/src/vscode/vscode_validator.dart +++ b/packages/flutter_tools/lib/src/vscode/vscode_validator.dart @@ -2,10 +2,11 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import '../base/file_system.dart'; +import '../base/platform.dart'; import '../base/user_messages.dart'; import '../base/version.dart'; import '../doctor.dart'; -import '../globals.dart' as globals; import 'vscode.dart'; class VsCodeValidator extends DoctorValidator { @@ -13,9 +14,9 @@ class VsCodeValidator extends DoctorValidator { final VsCode _vsCode; - static Iterable get installedValidators { + static Iterable installedValidators(FileSystem fileSystem, Platform platform) { return VsCode - .allInstalled(globals.fs, globals.platform) + .allInstalled(fileSystem, platform) .map((VsCode vsCode) => VsCodeValidator(vsCode)); } @@ -25,12 +26,8 @@ class VsCodeValidator extends DoctorValidator { ? null : userMessages.vsCodeVersion(_vsCode.version.toString()); - final ValidationType validationType = _vsCode.isValid - ? ValidationType.installed - : ValidationType.partial; - return ValidationResult( - validationType, + ValidationType.installed, _vsCode.validationMessages.toList(), statusInfo: vsCodeVersionText, ); diff --git a/packages/flutter_tools/test/commands.shard/hermetic/doctor_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/doctor_test.dart index 2e51f80b68..a03ba5dd70 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/doctor_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/doctor_test.dart @@ -188,7 +188,7 @@ void main() { testUsingContext('vs code validator when extension missing', () async { final ValidationResult result = await VsCodeValidatorTestTargets.installedWithoutExtension.validate(); - expect(result.type, ValidationType.partial); + expect(result.type, ValidationType.installed); expect(result.statusInfo, 'version 1.2.3'); expect(result.messages, hasLength(2)); @@ -198,8 +198,9 @@ void main() { message = result.messages .firstWhere((ValidationMessage m) => m.message.startsWith('Flutter ')); - expect(message.message, startsWith('Flutter extension not installed')); - expect(message.isError, isTrue); + expect(message.message, startsWith('Flutter extension can be installed from')); + expect(message.contextUrl, 'https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter'); + expect(message.isError, false); }, overrides: noColorTerminalOverride); group('device validator', () { @@ -1221,7 +1222,7 @@ class FakeSmallGroupDoctor extends Doctor { class VsCodeValidatorTestTargets extends VsCodeValidator { VsCodeValidatorTestTargets._(String installDirectory, String extensionDirectory, {String edition}) - : super(VsCode.fromDirectory(installDirectory, extensionDirectory, edition: edition)); + : super(VsCode.fromDirectory(installDirectory, extensionDirectory, edition: edition, fileSystem: globals.fs)); static VsCodeValidatorTestTargets get installedWithExtension => VsCodeValidatorTestTargets._(validInstall, validExtensions); diff --git a/packages/flutter_tools/test/general.shard/vscode/vscode_test.dart b/packages/flutter_tools/test/general.shard/vscode/vscode_test.dart index 2b505c7997..e43b72030c 100644 --- a/packages/flutter_tools/test/general.shard/vscode/vscode_test.dart +++ b/packages/flutter_tools/test/general.shard/vscode/vscode_test.dart @@ -2,31 +2,22 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'package:file/file.dart'; import 'package:file/memory.dart'; -import 'package:flutter_tools/src/base/file_system.dart'; -import 'package:flutter_tools/src/base/logger.dart'; import 'package:flutter_tools/src/base/version.dart'; -import 'package:flutter_tools/src/globals.dart' as globals; import 'package:flutter_tools/src/vscode/vscode.dart'; import '../../src/common.dart'; -import '../../src/context.dart'; void main() { - testUsingContext('VsCode.fromDirectory does not crash when packages.json is malformed', () { - final BufferLogger bufferLogger = globals.logger as BufferLogger; + testWithoutContext('VsCode.fromDirectory does not crash when packages.json is malformed', () { + final MemoryFileSystem fileSystem = MemoryFileSystem.test(); // Create invalid JSON file. - globals.fs.file(globals.fs.path.join('', 'resources', 'app', 'package.json')) + fileSystem.file(fileSystem.path.join('', 'resources', 'app', 'package.json')) ..createSync(recursive: true) ..writeAsStringSync('{'); - final VsCode vsCode = VsCode.fromDirectory('', ''); + final VsCode vsCode = VsCode.fromDirectory('', '', fileSystem: fileSystem); expect(vsCode.version, Version.unknown); - expect(bufferLogger.traceText, contains('Error parsing VSCode')); - }, overrides: { - FileSystem: () => MemoryFileSystem.test(), - ProcessManager: () => FakeProcessManager.any(), }); }