Skip to content

Commit 50b102b

Browse files
authored
fix: Melos init adding empty string / list in pubspec.yaml (#856)
## Description `melos init` currently adds an empty string to `melos:` and an empty list/array to `workspace:` in pubsspec.yaml when generating it. This is a limitation of the yaml package we're using. A workaround to remove these has been implemented ## Type of Change - [ ] ✨ `feat` -- New feature (non-breaking change which adds functionality) - [x] 🛠️ `fix` -- Bug fix (non-breaking change which fixes an issue) - [ ] ❌ `!` -- Breaking change (fix or feature that would cause existing functionality to change) - [ ] 🧹 `refactor` -- Code refactor - [ ] ✅ `ci` -- Build configuration change - [ ] 📝 `docs` -- Documentation - [ ] 🗑️ `chore` -- Chore
1 parent daba40d commit 50b102b

3 files changed

Lines changed: 41 additions & 12 deletions

File tree

packages/melos/lib/src/commands/init.dart

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ mixin _InitMixin on _Melos {
1515
}
1616

1717
final isCurrentDir = directory == '.';
18-
final dir = Directory(directory);
18+
final dir = isCurrentDir ? Directory.current : Directory(directory);
1919
if (!isCurrentDir && dir.existsSync()) {
2020
throw StateError('Directory $directory already exists');
2121
} else {
@@ -43,14 +43,25 @@ mixin _InitMixin on _Melos {
4343
'dev_dependencies': {
4444
'melos': '^$melosVersion',
4545
},
46-
'workspace': packages.values.map((p) => p.path).toList(),
46+
'workspace': packages.values
47+
.map(
48+
(p) => p.path
49+
.replaceFirst(dir.absolute.path, '.')
50+
.replaceFirst('./', ''),
51+
)
52+
.toList(),
4753
'melos': '',
4854
};
4955

5056
final pubspecFile = File(p.join(dir.absolute.path, 'pubspec.yaml'));
5157

5258
pubspecFile.writeAsStringSync(
53-
(YamlEditor('')..update([], pubspecYaml)).toString(),
59+
// YamlEditor adds empty strings and empty lists to the pubspec.yaml file
60+
// if the value is empty. This is a workaround to remove them.
61+
(YamlEditor('')..update([], pubspecYaml))
62+
.toString()
63+
.replaceFirst('""', '')
64+
.replaceFirst('[]', ''),
5465
);
5566

5667
logger.log(

packages/melos/test/commands/format_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ ${'-' * terminalWidth}
114114
stderrEncoding: utf8,
115115
);
116116

117-
expect(result.exitCode, equals(1));
117+
expect(result.exitCode, isNot(0));
118118
});
119119

120120
test('should run format with --output show flag', () async {

packages/melos/test/commands/init_test.dart

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,24 @@ import 'package:yaml/yaml.dart';
77

88
import '../utils.dart';
99

10+
Future<void> _createPackages(Directory dir) async {
11+
final packageDir = Directory(p.join(dir.absolute.path, 'packages'));
12+
packageDir.createSync(recursive: true);
13+
await Process.run(
14+
'dart',
15+
['create', 'test_package'],
16+
workingDirectory: packageDir.absolute.path,
17+
);
18+
19+
final appDir = Directory(p.join(dir.absolute.path, 'apps'));
20+
appDir.createSync(recursive: true);
21+
await Process.run(
22+
'dart',
23+
['create', 'test_app'],
24+
workingDirectory: appDir.absolute.path,
25+
);
26+
}
27+
1028
void main() {
1129
group('init', () {
1230
late TestLogger logger;
@@ -49,8 +67,7 @@ void main() {
4967
File(p.join(workspaceDir.path, 'pubspec.yaml')).readAsStringSync(),
5068
) as YamlMap;
5169
expect(melosYaml['name'], equals('my_workspace'));
52-
// TODO: Create some packages first that we can test against
53-
expect(melosYaml['workspace'], equals([]));
70+
expect(melosYaml['workspace'], isNull);
5471

5572
// Verify pubspec.yaml content
5673
final pubspecYaml = loadYaml(
@@ -93,9 +110,7 @@ void main() {
93110
final melosYaml = loadYaml(
94111
File(p.join(workspaceDir.path, 'pubspec.yaml')).readAsStringSync(),
95112
) as YamlMap;
96-
// TODO: Create some packages in 'custom/*', 'plugins/**' that we can test
97-
// against.
98-
expect(melosYaml['workspace'], equals([]));
113+
expect(melosYaml['workspace'], isNull);
99114
});
100115

101116
test('creates workspace in current directory when directory is "."',
@@ -109,7 +124,7 @@ void main() {
109124
final originalDir = Directory.current;
110125
try {
111126
Directory.current = tempDir;
112-
127+
await _createPackages(tempDir);
113128
await melos.init(
114129
'.',
115130
directory: '.',
@@ -125,6 +140,10 @@ void main() {
125140
final pubspecYaml =
126141
loadYaml(File('pubspec.yaml').readAsStringSync()) as YamlMap;
127142
expect(pubspecYaml['name'], equals(p.basename(tempDir.path)));
143+
expect(
144+
pubspecYaml['workspace'],
145+
equals(['apps/test_app', 'packages/test_package']),
146+
);
128147
} finally {
129148
Directory.current = originalDir;
130149
}
@@ -180,8 +199,7 @@ void main() {
180199
final melosYaml = loadYaml(
181200
File(p.join(workspaceDir.path, 'pubspec.yaml')).readAsStringSync(),
182201
) as YamlMap;
183-
// TODO: Create some packages first that we can test against
184-
expect(melosYaml['workspace'], equals([]));
202+
expect(melosYaml['workspace'], isNull);
185203
});
186204
});
187205
}

0 commit comments

Comments
 (0)