Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions lib/src/command/add.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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(
Expand Down Expand Up @@ -365,6 +357,7 @@ Specify multiple sdk packages with descriptors.''');
devDependencies: devDependencies,
dependencyOverrides: dependencyOverrides,
workspace: original.workspace,
resolution: original.resolution,
);
}

Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 6 additions & 7 deletions lib/src/command/remove.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
);
}

Expand All @@ -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
Expand All @@ -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());
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/src/pubspec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ class Pubspec extends PubspecBase {
Map<String, SdkConstraint>? sdkConstraints,
this.workspace = const <String>[],
this.dependencyOverridesFromOverridesFile = false,
this.resolution = Resolution.none,
}) : _dependencies = dependencies == null
? null
: {for (final d in dependencies) d.name: d},
Expand Down
98 changes: 98 additions & 0 deletions test/workspace_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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, [
Expand Down