diff --git a/docs/environment-variables.mdx b/docs/environment-variables.mdx index 5494806c..d2d57fc7 100644 --- a/docs/environment-variables.mdx +++ b/docs/environment-variables.mdx @@ -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 diff --git a/packages/melos/lib/src/commands/exec.dart b/packages/melos/lib/src/commands/exec.dart index 797827dc..f6133153 100644 --- a/packages/melos/lib/src/commands/exec.dart +++ b/packages/melos/lib/src/commands/exec.dart @@ -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); diff --git a/packages/melos/lib/src/package.dart b/packages/melos/lib/src/package.dart index b103deb0..5b7db1f7 100644 --- a/packages/melos/lib/src/package.dart +++ b/packages/melos/lib/src/package.dart @@ -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. /// diff --git a/packages/melos/test/commands/exec_test.dart b/packages/melos/test/commands/exec_test.dart index 7bb4c155..9a94c800 100644 --- a/packages/melos/test/commands/exec_test.dart +++ b/packages/melos/test/commands/exec_test.dart @@ -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() { @@ -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