Skip to content

Commit 5a8f23c

Browse files
authored
Fix and test an edge case in findPackageConfigFile. (#163902)
Closes flutter/flutter#163901. I'm not aware of a case where this causes user crashes, but it could, and seems easy to fix/test.
1 parent 641765e commit 5a8f23c

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

packages/flutter_tools/lib/src/dart/package_map.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ File? findPackageConfigFile(Directory dir) {
3434
return candidatePackageConfigFile;
3535
}
3636
final Directory parentDir = candidateDir.parent;
37-
if (fileSystem.identicalSync(parentDir.path, candidateDir.path)) {
37+
if (fileSystem.path.equals(parentDir.path, candidateDir.path)) {
3838
return null;
3939
}
4040
candidateDir = parentDir;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:file/file.dart';
6+
import 'package:file/memory.dart';
7+
import 'package:flutter_tools/src/dart/package_map.dart';
8+
9+
import '../../src/common.dart';
10+
11+
void main() {
12+
group('findPackageConfigFile', () {
13+
late FileSystem fileSystem;
14+
15+
setUp(() {
16+
fileSystem = MemoryFileSystem.test();
17+
});
18+
19+
test('should find an immediate .dart_tool/package_config.json', () {
20+
fileSystem.file('.dart_tool/package_config.json').createSync(recursive: true);
21+
expect(findPackageConfigFile(fileSystem.currentDirectory), isNotNull);
22+
});
23+
24+
test('should find a parent .dart_tool/package_config.json', () {
25+
fileSystem.file('.dart_tool/package_config.json').createSync(recursive: true);
26+
fileSystem.currentDirectory.childDirectory('child').createSync(recursive: true);
27+
expect(findPackageConfigFile(fileSystem.currentDirectory.childDirectory('child')), isNotNull);
28+
});
29+
30+
test('should not find a .dart_tool/package_config.json in an existing directory', () {
31+
fileSystem.currentDirectory.childDirectory('child').createSync(recursive: true);
32+
expect(findPackageConfigFile(fileSystem.currentDirectory.childDirectory('child')), isNull);
33+
});
34+
35+
// Regression test: https://github.com/flutter/flutter/issues/163901.
36+
test('should not find a .dart_tool/package_config.json in a missing directory', () {
37+
expect(findPackageConfigFile(fileSystem.currentDirectory.childDirectory('missing')), isNull);
38+
});
39+
40+
// Regression test: https://github.com/flutter/flutter/issues/163901.
41+
test('should find a .dart_tool/package_config.json in a parent of a missing directory', () {
42+
fileSystem.file('.dart_tool/package_config.json').createSync(recursive: true);
43+
expect(
44+
findPackageConfigFile(fileSystem.currentDirectory.childDirectory('missing')),
45+
isNotNull,
46+
);
47+
});
48+
});
49+
}

0 commit comments

Comments
 (0)