From 5a8f23c3dcf0042f4ee6a2dc5f19a6e06a474432 Mon Sep 17 00:00:00 2001 From: Matan Lurey Date: Tue, 25 Feb 2025 07:01:51 -0800 Subject: [PATCH] Fix and test an edge case in `findPackageConfigFile`. (#163902) Closes https://github.com/flutter/flutter/issues/163901. I'm not aware of a case where this causes user crashes, but it could, and seems easy to fix/test. --- .../lib/src/dart/package_map.dart | 2 +- .../general.shard/dart/package_map_test.dart | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 packages/flutter_tools/test/general.shard/dart/package_map_test.dart diff --git a/packages/flutter_tools/lib/src/dart/package_map.dart b/packages/flutter_tools/lib/src/dart/package_map.dart index f03e6f5f5f..db1b9a6005 100644 --- a/packages/flutter_tools/lib/src/dart/package_map.dart +++ b/packages/flutter_tools/lib/src/dart/package_map.dart @@ -34,7 +34,7 @@ File? findPackageConfigFile(Directory dir) { return candidatePackageConfigFile; } final Directory parentDir = candidateDir.parent; - if (fileSystem.identicalSync(parentDir.path, candidateDir.path)) { + if (fileSystem.path.equals(parentDir.path, candidateDir.path)) { return null; } candidateDir = parentDir; diff --git a/packages/flutter_tools/test/general.shard/dart/package_map_test.dart b/packages/flutter_tools/test/general.shard/dart/package_map_test.dart new file mode 100644 index 0000000000..7517f6a224 --- /dev/null +++ b/packages/flutter_tools/test/general.shard/dart/package_map_test.dart @@ -0,0 +1,49 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// 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/dart/package_map.dart'; + +import '../../src/common.dart'; + +void main() { + group('findPackageConfigFile', () { + late FileSystem fileSystem; + + setUp(() { + fileSystem = MemoryFileSystem.test(); + }); + + test('should find an immediate .dart_tool/package_config.json', () { + fileSystem.file('.dart_tool/package_config.json').createSync(recursive: true); + expect(findPackageConfigFile(fileSystem.currentDirectory), isNotNull); + }); + + test('should find a parent .dart_tool/package_config.json', () { + fileSystem.file('.dart_tool/package_config.json').createSync(recursive: true); + fileSystem.currentDirectory.childDirectory('child').createSync(recursive: true); + expect(findPackageConfigFile(fileSystem.currentDirectory.childDirectory('child')), isNotNull); + }); + + test('should not find a .dart_tool/package_config.json in an existing directory', () { + fileSystem.currentDirectory.childDirectory('child').createSync(recursive: true); + expect(findPackageConfigFile(fileSystem.currentDirectory.childDirectory('child')), isNull); + }); + + // Regression test: https://github.com/flutter/flutter/issues/163901. + test('should not find a .dart_tool/package_config.json in a missing directory', () { + expect(findPackageConfigFile(fileSystem.currentDirectory.childDirectory('missing')), isNull); + }); + + // Regression test: https://github.com/flutter/flutter/issues/163901. + test('should find a .dart_tool/package_config.json in a parent of a missing directory', () { + fileSystem.file('.dart_tool/package_config.json').createSync(recursive: true); + expect( + findPackageConfigFile(fileSystem.currentDirectory.childDirectory('missing')), + isNotNull, + ); + }); + }); +}