Skip to content

Commit b909542

Browse files
authored
fix(dart_frog_cli): avoid overwriting pubspec_overrides.yaml in dart_frog build (#1895)
1 parent 283d511 commit b909542

File tree

5 files changed

+110
-57
lines changed

5 files changed

+110
-57
lines changed

bricks/dart_frog_prod_server/hooks/lib/dart_frog_prod_server_hooks.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export 'src/get_package_graph.dart';
1010
export 'src/get_pubspec_lock.dart';
1111
export 'src/get_workspace_root.dart';
1212
export 'src/package_graph/package_graph.dart';
13+
export 'src/path_dependency.dart';
1314
export 'src/uses_workspace_resolution.dart';
1415

1516
/// A void callback function (e.g. `void Function()`).

bricks/dart_frog_prod_server/hooks/lib/src/create_external_packages_folder.dart

Lines changed: 6 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ Future<List<String>> createExternalPackagesFolder({
2727
final isExternal = !pathResolver.isWithin('', pathDescription.path);
2828
if (!isExternal) return null;
2929

30-
return _ExternalPathDependency(
30+
return ExternalPathDependency(
3131
name: dependency.name,
3232
path: path.join(projectDirectory.path, pathDescription.path),
3333
);
3434
},
3535
)
36-
.whereType<_ExternalPathDependency>()
36+
.whereType<ExternalPathDependency>()
3737
.toList();
3838

3939
if (externalPathDependencies.isEmpty) return [];
@@ -63,59 +63,13 @@ Future<List<String>> createExternalPackagesFolder({
6363
),
6464
);
6565

66-
File(
67-
pathResolver.join(buildDirectory.path, 'pubspec_overrides.yaml'),
68-
).writeAsStringSync(
69-
'''
70-
resolution: null
71-
dependency_overrides:
72-
${copiedExternalPathDependencies.map(
73-
(dependency) {
74-
final name = dependency.name;
75-
final path =
76-
pathResolver.relative(dependency.path, from: buildDirectory.path);
77-
return ' $name:\n path: $path';
78-
},
79-
).join('\n')}
80-
''',
66+
overrideResolutionInPubspecOverrides(buildDirectory.path);
67+
writePathDependencyOverrides(
68+
projectDirectory: buildDirectory.path,
69+
pathDependencies: copiedExternalPathDependencies,
8170
);
8271

8372
return copiedExternalPathDependencies
8473
.map((dependency) => dependency.path)
8574
.toList();
8675
}
87-
88-
/// {@template external_path_dependency}
89-
/// A path dependency that is not within the bundled Dart Frog project
90-
/// directory.
91-
///
92-
/// For example:
93-
/// ```yaml
94-
/// name: my_dart_frog_project
95-
/// dependencies:
96-
/// my_package:
97-
/// path: ../my_package
98-
/// ```
99-
/// {@endtemplate}
100-
class _ExternalPathDependency {
101-
/// {@macro external_path_dependency}
102-
const _ExternalPathDependency({
103-
required this.name,
104-
required this.path,
105-
});
106-
107-
/// The name of the package.
108-
final String name;
109-
110-
/// The absolute path to the package.
111-
final String path;
112-
113-
/// Copies the [_ExternalPathDependency] to [targetDirectory].
114-
Future<_ExternalPathDependency> copyTo({
115-
required Directory targetDirectory,
116-
CopyPath copyPath = io.copyPath,
117-
}) async {
118-
await copyPath(path, targetDirectory.path);
119-
return _ExternalPathDependency(name: name, path: targetDirectory.path);
120-
}
121-
}

bricks/dart_frog_prod_server/hooks/lib/src/disable_workspace_resolution.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ void overridePathDependenciesInPubspecOverrides({
8686
packageGraph: packageGraph,
8787
);
8888

89-
final pathDependencies = packageConfig.packages.where(
90-
(package) => package.relativeRoot && productionDeps.contains(package.name),
91-
);
89+
final pathDependencies = packageConfig.packages
90+
.where((p) => p.relativeRoot && productionDeps.contains(p.name))
91+
.map((p) => PathDependency(name: p.name, path: p.root.path));
9292

9393
writePathDependencyOverrides(
9494
projectDirectory: projectDirectory,
@@ -98,7 +98,7 @@ void overridePathDependenciesInPubspecOverrides({
9898

9999
void writePathDependencyOverrides({
100100
required String projectDirectory,
101-
required Iterable<Package> pathDependencies,
101+
required Iterable<PathDependency> pathDependencies,
102102
}) {
103103
final pubspecOverridesFile = File(
104104
path.join(projectDirectory, 'pubspec_overrides.yaml'),
@@ -112,7 +112,7 @@ void writePathDependencyOverrides({
112112
for (final package in pathDependencies) {
113113
editor.update(
114114
['dependency_overrides', package.name],
115-
{'path': path.relative(package.root.path, from: projectDirectory)},
115+
{'path': path.relative(package.path, from: projectDirectory)},
116116
);
117117
}
118118
pubspecOverridesFile.writeAsStringSync(editor.toString());
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import 'dart:io';
2+
3+
import 'package:dart_frog_prod_server_hooks/dart_frog_prod_server_hooks.dart';
4+
import 'package:io/io.dart' as io;
5+
6+
/// {@template path_dependency}
7+
/// A path dependency that the Dart Frog project relies on.
8+
///
9+
/// For example:
10+
/// ```yaml
11+
/// name: my_dart_frog_project
12+
/// dependencies:
13+
/// my_package:
14+
/// path: ./my_package
15+
/// ```
16+
/// {@endtemplate}
17+
class PathDependency {
18+
/// {@macro path_dependency}
19+
const PathDependency({required this.name, required this.path});
20+
21+
/// The name of the package.
22+
final String name;
23+
24+
/// The absolute path to the package.
25+
final String path;
26+
}
27+
28+
/// {@template external_path_dependency}
29+
/// A path dependency that is not within the bundled Dart Frog project
30+
/// directory.
31+
///
32+
/// For example:
33+
/// ```yaml
34+
/// name: my_dart_frog_project
35+
/// dependencies:
36+
/// my_package:
37+
/// path: ../my_package
38+
/// ```
39+
/// {@endtemplate}
40+
class ExternalPathDependency extends PathDependency {
41+
/// {@macro external_path_dependency}
42+
const ExternalPathDependency({required super.name, required super.path});
43+
44+
/// Copies the [ExternalPathDependency] to [targetDirectory].
45+
Future<ExternalPathDependency> copyTo({
46+
required Directory targetDirectory,
47+
CopyPath copyPath = io.copyPath,
48+
}) async {
49+
await copyPath(path, targetDirectory.path);
50+
return ExternalPathDependency(name: name, path: targetDirectory.path);
51+
}
52+
}

bricks/dart_frog_prod_server/hooks/test/src/create_external_packages_folder_test.dart

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,51 @@ void main() {
7575
);
7676
expect(copyCalls, ['$from -> $to']);
7777
});
78+
79+
test("doesn't overwrite existing overrides", () async {
80+
const existingOverrides = '''
81+
dependency_overrides:
82+
dart_frog:
83+
git:
84+
url: https://github.com/dart-frog-dev/dart_frog
85+
path: packages/dart_frog
86+
''';
87+
final projectDirectory = Directory.systemTemp.createTempSync();
88+
File(
89+
path.join(projectDirectory.path, 'build', 'pubspec_overrides.yaml'),
90+
)
91+
..createSync(recursive: true)
92+
..writeAsStringSync(existingOverrides);
93+
File(
94+
path.join(projectDirectory.path, 'pubspec.lock'),
95+
).writeAsStringSync(fooPathWithInternalDependency);
96+
final copyCalls = <String>[];
97+
98+
await createExternalPackagesFolder(
99+
projectDirectory: projectDirectory,
100+
buildDirectory: Directory(path.join(projectDirectory.path, 'build')),
101+
copyPath: (from, to) async {
102+
copyCalls.add('$from -> $to');
103+
File(
104+
path.join(to, 'pubspec_overrides.yaml'),
105+
).createSync(recursive: true);
106+
},
107+
);
108+
109+
final from = path.join(projectDirectory.path, '../../foo');
110+
final to = path.join(
111+
projectDirectory.path,
112+
'build',
113+
'.dart_frog_path_dependencies',
114+
'foo',
115+
);
116+
expect(copyCalls, ['$from -> $to']);
117+
expect(
118+
File(
119+
path.join(projectDirectory.path, 'build', 'pubspec_overrides.yaml'),
120+
).readAsStringSync(),
121+
contains(existingOverrides),
122+
);
123+
});
78124
});
79125
}

0 commit comments

Comments
 (0)