From 72914c98397fe0b92cbb1948201a12c399eeb7a5 Mon Sep 17 00:00:00 2001 From: Sigurd Meldgaard Date: Fri, 22 Mar 2024 09:37:42 +0000 Subject: [PATCH] Remove stray lockfiles and packageconfigs from workspace --- lib/src/entrypoint.dart | 16 ++++++++++++++ test/workspace_test.dart | 46 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/lib/src/entrypoint.dart b/lib/src/entrypoint.dart index 45b7cfdf59..bc809a82a0 100644 --- a/lib/src/entrypoint.dart +++ b/lib/src/entrypoint.dart @@ -581,6 +581,8 @@ To update `$lockFilePath` run `$topLevelProgram pub get`$suffix without _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 @@ See https://dart.dev/go/sdk-constraint } 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 d603f5da52..43b98a82ed 100644 --- a/test/workspace_test.dart +++ b/test/workspace_test.dart @@ -259,4 +259,50 @@ void main() { 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); + }); }