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
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,17 @@ Future<List<String>> createExternalPackagesFolder({
);

overrideResolutionInPubspecOverrides(buildDirectory.path);
writePathDependencyOverrides(
writeDependencyOverrides(
projectDirectory: buildDirectory.path,
pathDependencies: copiedExternalPathDependencies,
overrides: {
for (final externalDependency in copiedExternalPathDependencies)
externalDependency.name: {
'path': path.relative(
externalDependency.path,
from: buildDirectory.path,
),
},
},
);

return copiedExternalPathDependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ VoidCallback disableWorkspaceResolution(
}

try {
overridePathDependenciesInPubspecOverrides(
overrideDependenciesInPubspecOverrides(
projectDirectory: projectDirectory,
packageConfig: packageConfig,
packageGraph: packageGraph,
workspaceRoot: workspaceRoot,
);
} on Exception catch (e) {
restoreWorkspaceResolution();
Expand Down Expand Up @@ -70,11 +71,12 @@ VoidCallback overrideResolutionInPubspecOverrides(String projectDirectory) {
return () => pubspecOverridesFile.writeAsStringSync(contents);
}

/// Add overrides for all path dependencies to `pubspec_overrides.yaml`
void overridePathDependenciesInPubspecOverrides({
/// Add overrides for all necessary dependencies to `pubspec_overrides.yaml`
void overrideDependenciesInPubspecOverrides({
required String projectDirectory,
required PackageConfig packageConfig,
required PackageGraph packageGraph,
required String workspaceRoot,
}) {
final name = getPackageName(projectDirectory: projectDirectory);
if (name == null) {
Expand All @@ -90,29 +92,45 @@ void overridePathDependenciesInPubspecOverrides({
.where((p) => p.relativeRoot && productionDeps.contains(p.name))
.map((p) => PathDependency(name: p.name, path: p.root.path));

writePathDependencyOverrides(
final workspaceRootOverrides = getWorkspaceRootDependencyOverrides(
workspaceRoot: workspaceRoot,
);
final productionOverrides = workspaceRootOverrides.entries.where(
(e) => productionDeps.contains(e.key),
);

final overrides = <String, dynamic>{
for (final pathDependency in pathDependencies)
pathDependency.name: {
'path': path.relative(pathDependency.path, from: projectDirectory),
},
for (final override in productionOverrides)
'${override.key}': override.value,
};

writeDependencyOverrides(
projectDirectory: projectDirectory,
pathDependencies: pathDependencies,
overrides: overrides,
);
}

void writePathDependencyOverrides({
void writeDependencyOverrides({
required String projectDirectory,
required Iterable<PathDependency> pathDependencies,
required Map<String, dynamic> overrides,
}) {
final pubspecOverridesFile = File(
path.join(projectDirectory, 'pubspec_overrides.yaml'),
);
final contents = pubspecOverridesFile.readAsStringSync();
final overrides = loadYaml(contents) as YamlMap;
final pubspecOverrides = loadYaml(contents) as YamlMap;
final editor = YamlEditor(contents);
if (!overrides.containsKey('dependency_overrides')) {
if (!pubspecOverrides.containsKey('dependency_overrides')) {
editor.update(['dependency_overrides'], {});
}
for (final package in pathDependencies) {
for (final override in overrides.entries) {
editor.update(
['dependency_overrides', package.name],
{'path': path.relative(package.path, from: projectDirectory)},
['dependency_overrides', override.key],
override.value,
);
}
pubspecOverridesFile.writeAsStringSync(editor.toString());
Expand All @@ -130,6 +148,21 @@ String? getPackageName({required String projectDirectory}) {
return name;
}

YamlMap getWorkspaceRootDependencyOverrides({required String workspaceRoot}) {
final pubspecOverridesFile = File(
path.join(workspaceRoot, 'pubspec_overrides.yaml'),
);
if (!pubspecOverridesFile.existsSync()) return YamlMap();

final pubspecOverrides = loadYaml(pubspecOverridesFile.readAsStringSync());
if (pubspecOverrides is! YamlMap) return YamlMap();

final overrides = pubspecOverrides['dependency_overrides'];
if (overrides is! YamlMap) return YamlMap();

return overrides;
}

/// Build a complete list of dependencies (direct and transitive).
Set<String> getProductionDependencies({
required String packageName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,17 @@ void main() {
packageGraph = _MockPackageGraph();

when(() => context.logger).thenReturn(logger);
when(() => packageGraph.roots).thenReturn([packageName]);
when(() => packageGraph.roots).thenReturn([packageName, 'dart_frog']);
when(() => packageGraph.packages).thenReturn(
[
const PackageGraphPackage(
name: packageName,
dependencies: ['dart_frog'],
devDependencies: [],
version: '1.0.0',
),
const PackageGraphPackage(
name: 'dart_frog',
dependencies: [],
devDependencies: [],
version: '1.0.0',
Expand Down Expand Up @@ -118,6 +124,50 @@ resolution: null
),
);
});

group('when workspace root contains pubspec_overrides.yaml', () {
const workspaceRootDartFrogOverride = '''
dart_frog:
git:
url: https://github.com/dart-frog-dev/dart_frog
path: packages/dart_frog''';
const workspaceRootPubspecOverrides = '''
dependency_overrides:
$workspaceRootDartFrogOverride
''';

setUp(() {
File(
path.join(rootDirectory.path, 'pubspec_overrides.yaml'),
).writeAsStringSync(workspaceRootPubspecOverrides);
});

test('adds root overrides', () {
disableWorkspaceResolution(
context,
packageConfig: packageConfig,
packageGraph: packageGraph,
projectDirectory: projectDirectory.path,
workspaceRoot: rootDirectory.path,
exit: exitCalls.add,
);
final contents = projectDirectory.listSync();
expect(contents, hasLength(2));
final pubspecOverrides = contents.firstWhere(
(p) => path.basename(p.path) == 'pubspec_overrides.yaml',
) as File;
expect(
pubspecOverrides.readAsStringSync(),
equals(
'''
$originalPubspecOverridesContent
$workspaceRootDartFrogOverride
resolution: null
''',
),
);
});
});
});

group('when unable to read pubspec_overrides', () {
Expand Down