diff --git a/lib/src/command/add.dart b/lib/src/command/add.dart index 88f4bf5031..ddf99336bb 100644 --- a/lib/src/command/add.dart +++ b/lib/src/command/add.dart @@ -17,7 +17,6 @@ import '../exceptions.dart'; import '../git.dart'; import '../io.dart'; import '../log.dart' as log; -import '../package.dart'; import '../package_name.dart'; import '../pubspec.dart'; import '../pubspec_utils.dart'; @@ -192,9 +191,7 @@ Specify multiple sdk packages with descriptors.'''); /// Compute a pubspec that will depend on all the given packages, but the /// actual constraint will only be determined after a resolution decides the /// best version. - // TODO(https://github.com/dart-lang/pub/issues/4127): This should - // operate on entrypoint.workPackage. - var resolutionPubspec = entrypoint.workspaceRoot.pubspec; + var resolutionPubspec = entrypoint.workPackage.pubspec; for (final update in updates) { /// Perform version resolution in-memory. resolutionPubspec = await _addPackageToPubspec(resolutionPubspec, update); @@ -211,11 +208,7 @@ Specify multiple sdk packages with descriptors.'''); solveResult = await resolveVersions( SolveType.upgrade, cache, - Package( - resolutionPubspec, - entrypoint.workspaceRoot.dir, - entrypoint.workspaceRoot.workspaceChildren, - ), + entrypoint.withWorkPubspec(resolutionPubspec).workspaceRoot, ); } on GitException { final name = updates.first.ref.name; @@ -252,11 +245,11 @@ Specify multiple sdk packages with descriptors.'''); /// ensure that the modification timestamp on `pubspec.lock` and /// `.dart_tool/package_config.json` is newer than `pubspec.yaml`, /// ensuring that [entrypoint.assertUptoDate] will pass. - writeTextFile(entrypoint.workspaceRoot.pubspecPath, newPubspecText); + writeTextFile(entrypoint.workPackage.pubspecPath, newPubspecText); } String? overridesFileContents; - final overridesPath = entrypoint.workspaceRoot.pubspecOverridesPath; + final overridesPath = entrypoint.workPackage.pubspecOverridesPath; try { overridesFileContents = readTextFile(overridesPath); } on IOException { @@ -271,11 +264,10 @@ Specify multiple sdk packages with descriptors.'''); Pubspec.parse( newPubspecText, cache.sources, - location: Uri.parse(entrypoint.workspaceRoot.pubspecPath), + location: Uri.parse(entrypoint.workPackage.pubspecPath), overridesFileContents: overridesFileContents, overridesLocation: Uri.file(overridesPath), - containingDescription: - RootDescription(entrypoint.workspaceRoot.dir), + containingDescription: RootDescription(entrypoint.workPackage.dir), ), ) .acquireDependencies( @@ -365,6 +357,7 @@ Specify multiple sdk packages with descriptors.'''); devDependencies: devDependencies, dependencyOverrides: dependencyOverrides, workspace: original.workspace, + resolution: original.resolution, ); } @@ -683,8 +676,8 @@ Specify multiple sdk packages with descriptors.'''); List<_ParseResult> updates, ) { final yamlEditor = - YamlEditor(readTextFile(entrypoint.workspaceRoot.pubspecPath)); - log.io('Reading ${entrypoint.workspaceRoot.pubspecPath}.'); + YamlEditor(readTextFile(entrypoint.workPackage.pubspecPath)); + log.io('Reading ${entrypoint.workPackage.pubspecPath}.'); log.fine('Contents:\n$yamlEditor'); for (final update in updates) { diff --git a/lib/src/command/remove.dart b/lib/src/command/remove.dart index 11eea6ba15..c2e7e32050 100644 --- a/lib/src/command/remove.dart +++ b/lib/src/command/remove.dart @@ -88,10 +88,8 @@ To remove a dependency override of a package prefix the package name with /// Update the pubspec. _writeRemovalToPubspec(targets); } - // TODO(https://github.com/dart-lang/pub/issues/4127): This should - // operate on entrypoint.workPackage. - final rootPubspec = entrypoint.workspaceRoot.pubspec; - final newPubspec = _removePackagesFromPubspec(rootPubspec, targets); + final workPubspec = entrypoint.workPackage.pubspec; + final newPubspec = _removePackagesFromPubspec(workPubspec, targets); await entrypoint.withWorkPubspec(newPubspec).acquireDependencies( SolveType.get, @@ -133,6 +131,7 @@ To remove a dependency override of a package prefix the package name with devDependencies: devDependencies.values, dependencyOverrides: overrides.values, workspace: original.workspace, + resolution: original.resolution, ); } @@ -141,7 +140,7 @@ To remove a dependency override of a package prefix the package name with ArgumentError.checkNotNull(packages, 'packages'); final yamlEditor = - YamlEditor(readTextFile(entrypoint.workspaceRoot.pubspecPath)); + YamlEditor(readTextFile(entrypoint.workPackage.pubspecPath)); for (final package in packages) { final dependencyKeys = package.removeFromOverride @@ -168,13 +167,13 @@ To remove a dependency override of a package prefix the package name with } if (!found) { log.warning( - 'Package "$name" was not found in ${entrypoint.workspaceRoot.pubspecPath}!', + 'Package "$name" was not found in ${entrypoint.workPackage.pubspecPath}!', ); } } /// Windows line endings are already handled by [yamlEditor] - writeTextFile(entrypoint.workspaceRoot.pubspecPath, yamlEditor.toString()); + writeTextFile(entrypoint.workPackage.pubspecPath, yamlEditor.toString()); } } diff --git a/lib/src/pubspec.dart b/lib/src/pubspec.dart index dcec55d0ae..3bb2e0aaac 100644 --- a/lib/src/pubspec.dart +++ b/lib/src/pubspec.dart @@ -303,6 +303,7 @@ class Pubspec extends PubspecBase { Map? sdkConstraints, this.workspace = const [], this.dependencyOverridesFromOverridesFile = false, + this.resolution = Resolution.none, }) : _dependencies = dependencies == null ? null : {for (final d in dependencies) d.name: d}, diff --git a/test/workspace_test.dart b/test/workspace_test.dart index ddf2d3d329..1a983086f2 100644 --- a/test/workspace_test.dart +++ b/test/workspace_test.dart @@ -355,6 +355,104 @@ void main() { ); }); + test('`pub add` acts on the work package', () async { + final server = await servePackages(); + server.serve('foo', '1.0.0', sdk: '^3.7.0'); + 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(); + + await pubGet(environment: {'_PUB_TEST_SDK_VERSION': '3.7.0'}); + final aDir = p.join(sandbox, appPath, 'pkgs', 'a'); + await pubAdd( + args: ['foo'], + output: contains('+ foo 1.0.0'), + workingDirectory: aDir, + environment: {'_PUB_TEST_SDK_VERSION': '3.7.0'}, + ); + await dir(appPath, [ + dir('pkgs', [ + dir('a', [ + libPubspec( + 'a', + '1.1.1', + deps: {'foo': '^1.0.0'}, + resolutionWorkspace: true, + ), + ]), + ]), + ]).validate(); + }); + + test('`pub remove` acts on the work package', () async { + final server = await servePackages(); + server.serve('foo', '1.0.0', sdk: '^3.7.0'); + await dir(appPath, [ + libPubspec( + 'myapp', + '1.2.3', + extras: { + 'workspace': ['pkgs/a'], + }, + deps: {'foo': '^1.0.0'}, + sdk: '^3.7.0', + ), + dir('pkgs', [ + dir('a', [ + libPubspec( + 'a', + '1.1.1', + deps: {'foo': '^1.0.0'}, + resolutionWorkspace: true, + ), + ]), + ]), + ]).create(); + + await pubGet(environment: {'_PUB_TEST_SDK_VERSION': '3.7.0'}); + final aDir = p.join(sandbox, appPath, 'pkgs', 'a'); + await pubRemove( + args: ['foo'], + output: isNot(contains('- foo 1.0.0')), + workingDirectory: aDir, + environment: {'_PUB_TEST_SDK_VERSION': '3.7.0'}, + ); + await dir(appPath, [ + dir('pkgs', [ + dir('a', [ + libPubspec( + 'a', + '1.1.1', + resolutionWorkspace: true, + ), + ]), + ]), + ]).validate(); + // Only when removing it from the root it shows the update. + await pubRemove( + args: ['foo'], + output: contains('- foo 1.0.0'), + workingDirectory: path(appPath), + environment: {'_PUB_TEST_SDK_VERSION': '3.7.0'}, + ); + }); + test('Removes lock files and package configs from workspace members', () async { await dir(appPath, [