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
4 changes: 2 additions & 2 deletions docs/environment-variables.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ then its parent is the 'parent package'. For example, the package
`melos exec` script runs in that `example` package, the parent package is
`firebase_auth`.

Note, this 'parent package' functionality only works when the 'child
package' name ends with `example`.
Note, this 'parent package' functionality only works when the directory of the
'child package' ends with `example`.

## User defined

Expand Down
32 changes: 14 additions & 18 deletions packages/melos/lib/src/commands/exec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,20 @@ mixin _ExecMixin on _Melos {
EnvironmentVariableKey.path: workspace.childProcessPath!,
};

if (package.isExample) {
final exampleParentPackagePath = p.normalize('${package.path}/..');
final exampleParentPubspecPath = p.normalize(
'$exampleParentPackagePath/pubspec.yaml',
);

if (fileExists(exampleParentPubspecPath)) {
final exampleParentPackage = Pubspec.parse(
await readTextFileAsync(exampleParentPubspecPath),
);

environment[EnvironmentVariableKey.melosParentPackageName] =
exampleParentPackage.name;
environment[EnvironmentVariableKey.melosParentPackageVersion] =
(exampleParentPackage.version ?? Version.none).toString();
environment[EnvironmentVariableKey.melosParentPackagePath] =
exampleParentPackagePath;
}
// The parent must be a workspace package, so that, for example, a
// top-level `example` package doesn't get the workspace root as parent.
final exampleParentPackage = package.isExample
? workspace.allPackages.values.firstWhereOrNull(
(other) => p.equals(other.path, p.dirname(package.path)),
)
: null;
if (exampleParentPackage != null) {
environment[EnvironmentVariableKey.melosParentPackageName] =
exampleParentPackage.name;
environment[EnvironmentVariableKey.melosParentPackageVersion] =
exampleParentPackage.version.toString();
environment[EnvironmentVariableKey.melosParentPackagePath] =
exampleParentPackage.path;
}
if (environment.containsKey(EnvironmentVariableKey.melosTest)) {
EnvironmentVariableKey.allMelosKeys().forEach(environment.remove);
Expand Down
5 changes: 3 additions & 2 deletions packages/melos/lib/src/package.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1351,8 +1351,9 @@ class Package {
);
}

/// Whether this package is an example package.
bool get isExample => p.dirname(path).endsWith('example');
/// Whether this package is an example package, that is, whether its
/// directory name ends with `example`, like `my_package/example`.
bool get isExample => p.basename(path).endsWith('example');

/// Returns whether this package is a Flutter app.
///
Expand Down
85 changes: 85 additions & 0 deletions packages/melos/test/commands/exec_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ 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:platform/platform.dart' show FakePlatform, LocalPlatform;
import 'package:pub_semver/pub_semver.dart';
import 'package:pubspec_parse/pubspec_parse.dart';
import 'package:test/test.dart';

import '../matchers.dart';
import '../mock_env.dart';
import '../utils.dart';

void main() {
Expand Down Expand Up @@ -80,6 +82,89 @@ ${'-' * terminalWidth}
);
});

test(
'sets the parent package variables for an example package',
withMockPlatform(
() async {
final workspaceDir = await createTemporaryWorkspace(
workspacePackages: [
'packages/a',
'packages/a/example',
'packages/a/example/app',
'example',
],
prependPackages: false,
);
final aDir = await createProject(
workspaceDir,
Pubspec('a', version: Version(1, 2, 3)),
);
await createProject(
workspaceDir,
Pubspec('a_example'),
path: p.join('packages', 'a', 'example'),
);
await createProject(
workspaceDir,
Pubspec('a_example_app'),
path: p.join('packages', 'a', 'example', 'app'),
);
// Its parent directory is the workspace root, which is not a
// package.
await createProject(
workspaceDir,
Pubspec('top_example'),
path: 'example',
);

// A Dart script prints the variables, because `%VAR%` inside the
// command is not expanded by `cmd.exe /C %MELOS_SCRIPT%` on Windows.
final scriptPath = p.join(
createTestTempDir().path,
'print_parent.dart',
);
writeTextFile(scriptPath, r'''
import 'dart:io';

void main() {
final env = Platform.environment;
print(
'parent: ${env['MELOS_PARENT_PACKAGE_NAME']} '
'${env['MELOS_PARENT_PACKAGE_VERSION']} '
'${env['MELOS_PARENT_PACKAGE_PATH']}',
);
}
''');

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

await melos.exec(['dart', scriptPath], concurrency: 1);

final output = logger.output.normalizeLines();
expect(
output,
// Package paths are canonicalized, which lowercases them on
// Windows.
contains(
'a_example:\nparent: a 1.2.3 ${p.canonicalize(aDir.path)}\n',
),
);
expect(output, contains('a:\nparent: null null null\n'));
expect(output, contains('a_example_app:\nparent: null null null\n'));
expect(output, contains('top_example:\nparent: null null null\n'));
},
// `melos exec` drops the Melos variables when MELOS_TEST is set, so
// remove it in case the tests run with it, as in
// `MELOS_TEST=true dart test`.
platform: FakePlatform.fromPlatform(const LocalPlatform())
..environment.remove('MELOS_TEST'),
),
);

group('concurrent processes', () {
/// Use this file instead of running "exit 1" so the failure
/// order is more predictable
Expand Down
Loading