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.
This commit is contained in:
Matan Lurey 2025-02-25 07:01:51 -08:00 committed by GitHub
parent 641765e283
commit 5a8f23c3dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 50 additions and 1 deletions

View File

@ -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;

View File

@ -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,
);
});
});
}