From 40416bfdf8058afe8c5ccb135efac2fd4272337b Mon Sep 17 00:00:00 2001 From: devmil Date: Thu, 28 Sep 2023 12:39:40 +0200 Subject: [PATCH] fix: fixes local package refs by not adapting the relative path to the main package --- CHANGELOG.md | 1 + lib/src/cli/commands/command_mixin.dart | 6 +++ .../cli/extract_command_test.dart | 39 +++++++++++++++++++ .../package_a/lib/types/class_a.dart | 3 +- .../package_a/lib/types/mixin_a.dart | 3 ++ 5 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 test/test_packages/path_references/cluster_a/package_a/lib/types/mixin_a.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index e7ded60..bfbd2d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Version 0.16.3 - adds more diff result reporting options (cli, json, markdown) +- fixes broken local packages refs ## Version 0.16.2 - fixes relative path handling in package config (leading to unresolvable types) diff --git a/lib/src/cli/commands/command_mixin.dart b/lib/src/cli/commands/command_mixin.dart index 79a2184..5a0ec4f 100644 --- a/lib/src/cli/commands/command_mixin.dart +++ b/lib/src/cli/commands/command_mixin.dart @@ -201,6 +201,8 @@ OBSOLETE: Has no effect anymore. required String sourcePackageConfigPath, }) async { final sourcePackageConfigDirPath = p.dirname(sourcePackageConfigPath); + final sourcePackageDirPath = + Directory(sourcePackageConfigDirPath).parent.path; final targetPackageConfigContent = jsonDecode(await File(targetPackageConfigPath).readAsString()); // iterate through the package_config.json content and look for relative paths @@ -211,6 +213,10 @@ OBSOLETE: Has no effect anymore. // we make the relative path absolute by using the origin of the source package config as a base final normalizedPackagePath = p.normalize(p.join(sourcePackageConfigDirPath, packagePath)); + // if the relative path is the package path, then don't make it absolute + if (p.equals(sourcePackageDirPath, normalizedPackagePath)) { + continue; + } // and write the new absolute path back to the json structure packageConfig['rootUri'] = p.toUri(normalizedPackagePath).toString(); } diff --git a/test/integration_tests/cli/extract_command_test.dart b/test/integration_tests/cli/extract_command_test.dart index 3835005..4077717 100644 --- a/test/integration_tests/cli/extract_command_test.dart +++ b/test/integration_tests/cli/extract_command_test.dart @@ -75,6 +75,45 @@ void main() { timeout: integrationTestTimeout, ); + test( + 'local packages don\'t break own package refs', + () async { + final tempDir = await Directory.systemTemp.createTemp(); + final tempFilePath = path.join(tempDir.path, 'extract_paths_test.json'); + final exitCode = await runner.run([ + 'extract', + '--input', + path.join( + 'test', + 'test_packages', + 'path_references', + 'cluster_a', + 'package_a', + ), + '--output', + tempFilePath, + ]); + expect(exitCode, 0); + + final jsonReportFile = File(tempFilePath); + expect(await jsonReportFile.exists(), isTrue); + + final jsonReport = jsonDecode(await jsonReportFile.readAsString()); + await tempDir.delete(recursive: true); + + // check if the thisIsAMixinField in ClassA was extracted + final interfaceDeclarations = + jsonReport['packageApi']['interfaceDeclarations'] as List; + final classADeclaration = + interfaceDeclarations.singleWhere((id) => id['name'] == 'ClassA'); + final thisIsAMixinFields = + (classADeclaration['fieldDeclarations'] as List) + .where((fd) => fd['name'] == 'thisIsAMixinField'); + expect(thisIsAMixinFields.length, 1); + }, + timeout: integrationTestTimeout, + ); + test( 'Can handle pub ref', () async { diff --git a/test/test_packages/path_references/cluster_a/package_a/lib/types/class_a.dart b/test/test_packages/path_references/cluster_a/package_a/lib/types/class_a.dart index 634fc53..9939b88 100644 --- a/test/test_packages/path_references/cluster_a/package_a/lib/types/class_a.dart +++ b/test/test_packages/path_references/cluster_a/package_a/lib/types/class_a.dart @@ -1,8 +1,9 @@ import 'package:meta/meta.dart'; import 'package:package_b/package_b.dart'; +import 'package:package_a/types/mixin_a.dart'; @experimental -class ClassA { +class ClassA with MixinA { final ClassB classB; ClassA({required this.classB}); diff --git a/test/test_packages/path_references/cluster_a/package_a/lib/types/mixin_a.dart b/test/test_packages/path_references/cluster_a/package_a/lib/types/mixin_a.dart new file mode 100644 index 0000000..229ae56 --- /dev/null +++ b/test/test_packages/path_references/cluster_a/package_a/lib/types/mixin_a.dart @@ -0,0 +1,3 @@ +mixin MixinA { + int thisIsAMixinField; +}