Remove stray lockfiles and packageconfigs from workspace (#4194)
diff --git a/lib/src/entrypoint.dart b/lib/src/entrypoint.dart index 45b7cfd..bc809a8 100644 --- a/lib/src/entrypoint.dart +++ b/lib/src/entrypoint.dart
@@ -581,6 +581,8 @@ _lockFile = newLockFile; if (!dryRun) { + _removeStrayLockAndConfigFiles(); + /// Build a package graph from the version solver results so we don't /// have to reload and reparse all the pubspecs. _packageGraph = Future.value(PackageGraph.fromSolveResult(this, result)); @@ -1216,4 +1218,18 @@ } return true; } + + /// Remove any `pubspec.lock` or `.dart_tool/package_config.json` files in + /// workspace packages that are not the root package. + /// + /// This is to avoid surprises if a package is turned into a workspace member + /// but still has an old package config or lockfile. + void _removeStrayLockAndConfigFiles() { + for (final package in workspaceRoot.transitiveWorkspace) { + if (package.pubspec.resolution == Resolution.workspace) { + deleteEntry(p.join(package.dir, 'pubspec.lock')); + deleteEntry(p.join(package.dir, '.dart_tool', 'package_config.json')); + } + } + } }
diff --git a/test/workspace_test.dart b/test/workspace_test.dart index 79ec525..ddf2d3d 100644 --- a/test/workspace_test.dart +++ b/test/workspace_test.dart
@@ -354,4 +354,50 @@ output: contains('Resolving dependencies in `..`...'), ); }); + + test('Removes lock files and package configs from workspace members', + () async { + await dir(appPath, [ + libPubspec( + 'myapp', + '1.2.3', + extras: { + 'workspace': ['pkgs/a'], + }, + sdk: '^3.7.0', + ), + dir('pkgs', [ + dir( + 'a', + [ + libPubspec('a', '1.1.1', resolutionWorkspace: true), + ], + ), + ]), + ]).create(); + final aDir = p.join(sandbox, appPath, 'pkgs', 'a'); + final pkgsDir = p.join(sandbox, appPath, 'pkgs'); + final strayLockFile = File(p.join(aDir, 'pubspec.lock')); + final strayPackageConfig = + File(p.join(aDir, '.dart_tool', 'package_config.json')); + + final unmanagedLockFile = File(p.join(pkgsDir, 'pubspec.lock')); + final unmanagedPackageConfig = + File(p.join(pkgsDir, '.dart_tool', 'package_config.json')); + strayPackageConfig.createSync(recursive: true); + strayLockFile.createSync(recursive: true); + + unmanagedPackageConfig.createSync(recursive: true); + unmanagedLockFile.createSync(recursive: true); + + await pubGet(environment: {'_PUB_TEST_SDK_VERSION': '3.7.0'}); + + expect(strayLockFile.statSync().type, FileSystemEntityType.notFound); + expect(strayPackageConfig.statSync().type, FileSystemEntityType.notFound); + + // We only delete stray files from directories that contain an actual + // package. + expect(unmanagedLockFile.statSync().type, FileSystemEntityType.file); + expect(unmanagedPackageConfig.statSync().type, FileSystemEntityType.file); + }); }