diff --git a/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart b/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart index 2ec6d896b06..9a57f518324 100644 --- a/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart +++ b/packages/flutter_tools/lib/src/runner/flutter_command_runner.dart @@ -352,14 +352,28 @@ class FlutterCommandRunner extends CommandRunner { Uri rootUri = flutterUri.resolve('../../..'); String flutterPath = path.normalize(fs.file(rootUri).absolute.path); - if (!_compareResolvedPaths(flutterPath, Cache.flutterRoot)) { + if (!fs.isDirectorySync(flutterPath)) { printError( - 'Warning: the \'flutter\' tool you are currently running is different from the one referenced in your pubspec.yaml:\n' - ' running Flutter : ${Cache.flutterRoot}\n' - ' pubspec reference: $flutterPath\n' - 'This can happen when you have multiple copies of flutter installed. Please check your system path to verify\n' - 'that you\'re running the expected version (run \'flutter --version\' to see which flutter is on your path). You\n' - 'can also change which flutter your project points to by editing the \'flutter:\' path in your pubspec.yaml file.\n' + 'Warning! This package referenced a Flutter repository via the .packages file that is\n' + 'no longer available. The repository from which the \'flutter\' tool is currently\n' + 'executing will be used instead.\n' + ' running Flutter tool: ${Cache.flutterRoot}\n' + ' previous reference : $flutterPath\n' + 'This can happen if you deleted or moved your copy of the Flutter repository, or\n' + 'if it was on a volume that is no longer mounted or has been mounted at a\n' + 'different location. Please check your system path to verify that you are running\n' + 'the expected version (run \'flutter --version\' to see which flutter is on your path).\n' + ); + } else if (!_compareResolvedPaths(flutterPath, Cache.flutterRoot)) { + printError( + 'Warning! The \'flutter\' tool you are currently running is from a different Flutter\n' + 'repository than the one last used by this package. The repository from which the\n' + '\'flutter\' tool is currently executing will be used instead.\n' + ' running Flutter tool: ${Cache.flutterRoot}\n' + ' previous reference : $flutterPath\n' + 'This can happen when you have multiple copies of flutter installed. Please check\n' + 'your system path to verify that you are running the expected version (run\n' + '\'flutter --version\' to see which flutter is on your path).\n' ); } } diff --git a/packages/flutter_tools/test/data/dart_dependencies_test/changed_sdk_location/.packages b/packages/flutter_tools/test/data/dart_dependencies_test/changed_sdk_location/.packages new file mode 100644 index 00000000000..0b55f79f099 --- /dev/null +++ b/packages/flutter_tools/test/data/dart_dependencies_test/changed_sdk_location/.packages @@ -0,0 +1,2 @@ +flutter:file:///a/wild/non-existent/directory/has/appeared +sdk-move-test:lib/ diff --git a/packages/flutter_tools/test/data/dart_dependencies_test/changed_sdk_location/lib/main.dart b/packages/flutter_tools/test/data/dart_dependencies_test/changed_sdk_location/lib/main.dart new file mode 100644 index 00000000000..bcd11b54394 --- /dev/null +++ b/packages/flutter_tools/test/data/dart_dependencies_test/changed_sdk_location/lib/main.dart @@ -0,0 +1,5 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// No content diff --git a/packages/flutter_tools/test/data/dart_dependencies_test/changed_sdk_location/pubspec.yaml b/packages/flutter_tools/test/data/dart_dependencies_test/changed_sdk_location/pubspec.yaml new file mode 100644 index 00000000000..1b11b4a42f2 --- /dev/null +++ b/packages/flutter_tools/test/data/dart_dependencies_test/changed_sdk_location/pubspec.yaml @@ -0,0 +1,5 @@ +name: sdk-move-test + +dependencies: + flutter: + sdk: flutter diff --git a/packages/flutter_tools/test/dependency_checker_test.dart b/packages/flutter_tools/test/dependency_checker_test.dart index 89d480e1a06..82b1ffd5413 100644 --- a/packages/flutter_tools/test/dependency_checker_test.dart +++ b/packages/flutter_tools/test/dependency_checker_test.dart @@ -2,7 +2,11 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'package:file/memory.dart'; +import 'package:flutter_tools/src/base/file_system.dart'; import 'package:flutter_tools/src/base/platform.dart'; +import 'package:flutter_tools/src/cache.dart'; +import 'package:flutter_tools/src/commands/devices.dart'; import 'package:flutter_tools/src/dart/dependencies.dart'; import 'package:flutter_tools/src/dependency_checker.dart'; import 'package:path/path.dart' as path; @@ -14,6 +18,13 @@ void main() { group('DependencyChecker', () { final String basePath = path.dirname(platform.script.path); final String dataPath = path.join(basePath, 'data', 'dart_dependencies_test'); + MemoryFileSystem testFileSystem; + + setUp(() { + Cache.disableLocking(); + testFileSystem = new MemoryFileSystem(); + }); + testUsingContext('good', () { final String testPath = path.join(dataPath, 'good'); final String mainPath = path.join(testPath, 'main.dart'); @@ -67,5 +78,24 @@ void main() { // the .dart file. expect(dependencyChecker.check(baseTime), isTrue); }); + + /// Test a flutter tool move. + /// + /// Tests that the flutter tool doesn't crash and displays a warning when its own location + /// changed since it was last referenced to in a package's .packages file. + testUsingContext('moved flutter sdk', () async { + String destinationPath = '/some/test/location'; + // Copy the golden input and let the test run in an isolated temporary in-memory file system. + copyDirectorySync( + new LocalFileSystem().directory(path.join(dataPath, 'changed_sdk_location')), + fs.directory(destinationPath)); + fs.currentDirectory = destinationPath; + + // Doesn't matter what commands we run. Arbitrarily list devices here. + await createTestCommandRunner(new DevicesCommand()).run(['devices']); + expect(testLogger.errorText, contains('.packages')); + }, overrides: { + FileSystem: () => testFileSystem, + }); }); } diff --git a/packages/flutter_tools/test/src/base/file_system_test.dart b/packages/flutter_tools/test/src/base/file_system_test.dart index 0b513e15724..cbd6a173f31 100644 --- a/packages/flutter_tools/test/src/base/file_system_test.dart +++ b/packages/flutter_tools/test/src/base/file_system_test.dart @@ -25,7 +25,7 @@ void main() { String targetPath = '/some/non-existent/target'; Directory targetDirectory = targetMemoryFs.directory(targetPath); copyDirectorySync(sourceDirectory, targetDirectory); - + expect(targetDirectory.existsSync(), true); targetMemoryFs.currentDirectory = targetPath; expect(targetMemoryFs.directory('empty_directory').existsSync(), true);