Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: usage of potentially wrong melos version during script execution #783

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
45 changes: 44 additions & 1 deletion packages/melos/lib/src/scripts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import 'package:collection/collection.dart';
import 'package:meta/meta.dart';

import 'common/platform.dart';
import 'common/utils.dart';
import 'common/validation.dart';
import 'package.dart';
Expand Down Expand Up @@ -318,7 +319,8 @@
if (exec == null) {
return scriptCommand;
} else {
final execCommand = ['melos', 'exec'];
/// `dart melos.dart exec` or `melos exec` with absolute paths
final execCommand = [..._determineMelosExecutablePaths(), 'exec'];

if (exec.concurrency != null) {
execCommand.addAll(['--concurrency', '${exec.concurrency}']);
Expand All @@ -338,6 +340,47 @@
}
}

/// Identifies path of currently running melos and reuses it for nested script execution.

Check notice on line 343 in packages/melos/lib/src/scripts.dart

View workflow job for this annotation

GitHub Actions / analyze

The line length exceeds the 80-character limit.

Try breaking the line across multiple lines. See https://dart.dev/lints/lines_longer_than_80_chars to learn more about this problem.
///
/// Starting melos by just calling `melos` via shell
/// would use the first found binary found in `$PATH`,
/// i.e. usually the globally activated version. If there is one.
///
/// This can cause issues when melos is started with `dart run melos`,
/// which will prefer the melos version specified via (dev_)dependencies.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Melos should always run scripts etc with the version that is defined in dev_dependencies, even if it is started directly from the version on path.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, thanks for the quick review.

Is this a future enhancement request or already implemented? 🙂

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The overarching goal of this PR is to eliminate ambiguity and surprises in the build process of Dart/Flutter applications. In line with that, I believe it would be ideal for Melos to behave in a consistent and predictable way.

Here’s what I think could be considered "unexpected" behavior:

  • If Melos uses different versions of itself for script execution (i.e. for subprocesses of itself)
    • This will be addressed with this PR!? :)
  • If Melos silently switches to a different version at startup (i.e., in the main process)
    • This could be especially challenging when intentionally testing a specific version.
    • Perhaps an optional strict mode (e.g., for CI environments) could help, where Melos would verify that its version matches the one specified in pubspec.lock.
      • The version in dev_dependencies might only define a minimum version, which wouldn’t fully address this, as you likely know as well. :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a future enhancement request or already implemented? 🙂

It's already implemented, try pinning you melos version to an older version an see what happens. :)

The version in dev_dependencies might only define a minimum version, which wouldn’t fully address this, as you likely know as well. :)

If one wants a specific version of melos, why not just pin the version in pubspec? I don't see the need for any separate feature for this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@spydon Am I correct in understanding that you're unhappy with the comment in the code? If so, could you suggest a better or more accurate phrasing? :)

Additionally, if the only remaining blocker for merging is the failing tests, I’d be happy to look into the breaking cod – given it's likely the changes will be merged. :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unsure if this is really needed/wanted, if I haven't misunderstood what this PR is trying to achieve.
If you run dart run melos --version you'll get the version specified in pubspec.yaml, which is the intentional behavior, no matter what melos binary that started it, the scripts should run with the melos version specified in the pubspec by the constraints.

/// So the initially started melos binary/script and the one used for melos script execution
/// could differ.
///
/// Besides ensuring starting the same melos as currently running,
/// this furthermore enables use of melos in environments
/// where `.pub-cache/bin` should/must not be put in `$PATH`.
List<String> _determineMelosExecutablePaths() {
final currentExecutablePathString = currentPlatform.resolvedExecutable;
final currentExecutablePathUri = Uri.file(currentExecutablePathString);
if (!currentExecutablePathUri.isAbsolute) {
throw 'Got invalid, unsupported or relative path to running executable. Expected absolute path.';

Check notice on line 361 in packages/melos/lib/src/scripts.dart

View workflow job for this annotation

GitHub Actions / analyze

Don't throw instances of classes that don't extend either 'Exception' or 'Error'.

Try throwing a different class of object. See https://dart.dev/lints/only_throw_errors to learn more about this problem.

Check notice on line 361 in packages/melos/lib/src/scripts.dart

View workflow job for this annotation

GitHub Actions / analyze

The line length exceeds the 80-character limit.

Try breaking the line across multiple lines. See https://dart.dev/lints/lines_longer_than_80_chars to learn more about this problem.
}

final currentScriptPathUri = currentPlatform.script;
final currentScriptPathString = currentScriptPathUri.toFilePath();
if (!currentScriptPathString.isEmpty && !currentScriptPathUri.isAbsolute) {

Check notice on line 366 in packages/melos/lib/src/scripts.dart

View workflow job for this annotation

GitHub Actions / analyze

Use 'isNotEmpty' rather than negating the result of 'isEmpty'.

Try rewriting the expression to use 'isNotEmpty'. See https://dart.dev/diagnostics/prefer_is_not_empty to learn more about this problem.
throw 'Got invalid, unsupported or relative path to melos. Expected absolute path.';

Check notice on line 367 in packages/melos/lib/src/scripts.dart

View workflow job for this annotation

GitHub Actions / analyze

Don't throw instances of classes that don't extend either 'Exception' or 'Error'.

Try throwing a different class of object. See https://dart.dev/lints/only_throw_errors to learn more about this problem.

Check notice on line 367 in packages/melos/lib/src/scripts.dart

View workflow job for this annotation

GitHub Actions / analyze

The line length exceeds the 80-character limit.

Try breaking the line across multiple lines. See https://dart.dev/lints/lines_longer_than_80_chars to learn more about this problem.
}

// Determine if melos is running as script or compiled binary
final isScriptPathAvailable = !currentScriptPathString.isEmpty;

Check notice on line 371 in packages/melos/lib/src/scripts.dart

View workflow job for this annotation

GitHub Actions / analyze

Use 'isNotEmpty' rather than negating the result of 'isEmpty'.

Try rewriting the expression to use 'isNotEmpty'. See https://dart.dev/diagnostics/prefer_is_not_empty to learn more about this problem.
final isCompiledMelosRunning =
currentExecutablePathUri == currentScriptPathUri;

if (!isScriptPathAvailable || isCompiledMelosRunning) {
return [currentExecutablePathString];
}

// currentExecutablePathString => path to dart binary
// currentScriptPathUri => path to melos.dart
return [currentExecutablePathString, currentScriptPathString];
}

/// Validates the script. Throws a [MelosConfigException] if the script is
/// invalid.
void validate() {
Expand Down
Loading