Skip to content

Commit

Permalink
fix: Simple hosted dependencies should be inlined (#795)
Browse files Browse the repository at this point in the history
<!--
  Thanks for contributing!

Provide a description of your changes below and a general summary in the
title

Please look at the following checklist to ensure that your PR can be
accepted quickly:
-->

## Description

Before this fix (after moving to pubspec_parse), when we updated shared
dependencies they would explicitly use the version field.

Example before this PR:
```yaml
dependencies:
  flame:
    version: ^1.21.0
```

After this PR:
```yaml
dependencies:
  flame: ^1.21.0
```

## Type of Change

<!--- Put an `x` in all the boxes that apply: -->

- [ ] ✨ `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
  • Loading branch information
spydon authored Nov 21, 2024
1 parent df0c294 commit 83909a7
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 10 deletions.
32 changes: 23 additions & 9 deletions packages/melos/lib/src/common/extensions/dependency.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ extension DependencyExtension on Dependency {
return null;
}

/// Whether the json can be inlined with its parent.
///
/// For example for [HostedDependency] the version shouldn't be on a separate
/// line when only the version is defined.
bool get inlineVersion {
if (this is HostedDependency) {
return (this as HostedDependency).hosted == null &&
versionConstraint != null;
}
return false;
}

Object toJson() {
final self = this;
if (self is PathDependency) {
Expand All @@ -34,15 +46,17 @@ extension PathDependencyExtension on PathDependency {
}

extension HostedDependencyExtension on HostedDependency {
Map<String, dynamic> toJson() {
return {
'version': version.toString(),
if (hosted != null)
'hosted': {
'name': hosted!.declaredName,
'url': hosted!.url?.toString(),
},
};
Object toJson() {
return inlineVersion
? version.toString()
: {
'version': version.toString(),
if (hosted != null)
'hosted': {
'name': hosted!.declaredName,
'url': hosted!.url?.toString(),
},
};
}
}

Expand Down
54 changes: 53 additions & 1 deletion packages/melos/test/commands/bootstrap_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import 'package:melos/melos.dart';
import 'package:melos/src/command_configs/command_configs.dart';
import 'package:melos/src/commands/runner.dart';
import 'package:melos/src/common/glob.dart';
import 'package:melos/src/common/io.dart';
import 'package:melos/src/common/utils.dart';
import 'package:path/path.dart' as p;
import 'package:pub_semver/pub_semver.dart';
import 'package:pubspec_parse/pubspec_parse.dart';
import 'package:test/test.dart';
import 'package:yaml/yaml.dart';

import '../matchers.dart';
import '../utils.dart';
Expand Down Expand Up @@ -745,7 +747,7 @@ Generating IntelliJ IDE files...
});

test(
'applies dependencies from melos config',
'applies shared dependencies from melos config',
() async {
final workspaceDir = await createTemporaryWorkspace(
configBuilder: (path) => MelosWorkspaceConfig(
Expand Down Expand Up @@ -910,6 +912,51 @@ Generating IntelliJ IDE files...
},
timeout: const Timeout(Duration(days: 2)),
);

test('correctly inlines shared dependencies', () async {
final workspaceDir = await createTemporaryWorkspace(
configBuilder: (path) => MelosWorkspaceConfig.fromYaml(
createYamlMap(
{
'command': {
'bootstrap': {
'dependencies': {
'flame': '^1.21.0',
},
},
},
},
defaults: configMapDefaults,
),
path: path,
),
);

final pkgA = await createProject(
workspaceDir,
Pubspec(
'a',
dependencies: {
'flame': HostedDependency(version: VersionConstraint.any),
},
),
);

final logger = TestLogger();
final config = await MelosWorkspaceConfig.fromWorkspaceRoot(workspaceDir);
final melos = Melos(
logger: logger,
config: config,
);

await runMelosBootstrap(melos, logger);

final pubspecContent = _pubspecContent(pkgA);
expect(
(pubspecContent['dependencies']! as YamlMap)['flame'],
'^1.21.0',
);
});
});

group('melos bs --skip-linking', () {
Expand Down Expand Up @@ -1142,3 +1189,8 @@ Future<void> dependencyResolutionTest(

await Future.wait<void>(packages.keys.map(validatePackage));
}

YamlMap _pubspecContent(io.Directory directory) {
final source = readTextFile(pubspecPath(directory.path));
return loadYaml(source) as YamlMap;
}
4 changes: 4 additions & 0 deletions packages/melos/test/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ String packageConfigPath(String packageRoot) {
);
}

String pubspecPath(String directory) {
return p.join(directory, 'pubspec.yaml');
}

PackageConfig packageConfigForPackageAt(Directory dir) {
final source = readTextFile(packageConfigPath(dir.path));
return PackageConfig.fromJson(json.decode(source) as Map<String, Object?>);
Expand Down

0 comments on commit 83909a7

Please sign in to comment.