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 running dart_dev in mixed-version packages opted into null safety #373

Merged
merged 6 commits into from
May 24, 2022
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: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [3.8.5](https://github.com/Workiva/dart_dev/compare/3.8.4...3.8.5)

- Fix errors when running in mixed-version packages that have opted into null safety via pubspec.yaml.

## [3.8.4](https://github.com/Workiva/dart_dev/compare/3.8.3...3.8.4)
- Replace deprecated CLI command.

Expand Down
16 changes: 16 additions & 0 deletions lib/src/executable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:args/command_runner.dart';
import 'package:dart_dev/dart_dev.dart';
import 'package:dart_dev/src/dart_dev_tool.dart';
import 'package:dart_dev/src/utils/format_tool_builder.dart';
import 'package:dart_dev/src/utils/get_dart_version_comment.dart';
import 'package:dart_dev/src/utils/parse_flag_from_args.dart';
import 'package:io/ansi.dart';
import 'package:io/io.dart' show ExitCode;
Expand Down Expand Up @@ -114,9 +115,24 @@ bool get shouldWriteRunScript =>
!_runScript.existsSync() ||
_runScript.readAsStringSync() != buildDartDevRunScriptContents();

/// Whether dart_dev itself has opted into null-safety.
const _isDartDevNullSafe = false;

String buildDartDevRunScriptContents() {
final hasCustomToolDevDart = File(_configPath).existsSync();
// If the config has a dart version comment (e.g., if it opts out of null safety),
// copy it over to the entrypoint so the program is run in that language version.
var dartVersionComment = hasCustomToolDevDart
? getDartVersionComment(File(_configPath).readAsStringSync())
: null;
// If dart_dev itself is not null-safe, opt the entrypoint out of null-safety
// so the entrypoint doesn't fail to run in packages that have opted into null-safety.
if (!_isDartDevNullSafe && dartVersionComment == null) {
dartVersionComment = '// @dart=2.9';
}

return '''
${dartVersionComment ?? ''}
import 'dart:io';

import 'package:dart_dev/src/core_config.dart';
Expand Down
6 changes: 6 additions & 0 deletions lib/src/utils/get_dart_version_comment.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// Returns the Dart version comment contained in a given [source] file,
/// or `null` if one does not exist.
///
/// Uses regex over the analyzer for performance.
String getDartVersionComment(String source) =>
RegExp(r'^//\s*@dart\s*=.+$', multiLine: true).firstMatch(source)?.group(0);
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: dart_dev_test_functional_null_safety_opted_in_custom_config_version_comment
version: 0.0.0
environment:
sdk: ">=2.12.0"
dev_dependencies:
dart_dev:
path: ../../../../..

workiva:
disable_core_checks: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:dart_dev/dart_dev.dart';

final config = {
...coreConfig,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: dart_dev_test_functional_null_safety_opted_in_custom_config
version: 0.0.0
environment:
sdk: ">=2.12.0"
dev_dependencies:
dart_dev:
path: ../../../../..

workiva:
disable_core_checks: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @dart=2.9
import 'package:dart_dev/dart_dev.dart';

final config = {
...coreConfig,
};
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: dart_dev_test_functional_null_safety_opted_in_no_config
version: 0.0.0
environment:
sdk: ">=2.12.0"
dev_dependencies:
dart_dev:
path: ../../../../..

workiva:
disable_core_checks: true
27 changes: 27 additions & 0 deletions test/functional/null_safety_functional_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@TestOn('vm')
@Timeout(Duration(seconds: 20))
import 'package:test/test.dart';

import '../functional.dart';

void main() {
group('runs properly in a project that has opted into null safety', () {
test('without any custom config', () async {
final process = await runDevToolFunctionalTest(
'analyze', 'test/functional/fixtures/null_safety/opted_in_no_config');
await process.shouldExit(0);
});

test('with a custom config', () async {
final process = await runDevToolFunctionalTest('analyze',
'test/functional/fixtures/null_safety/opted_in_custom_config');
await process.shouldExit(0);
});

test('with a custom config that has a language version comment', () async {
final process = await runDevToolFunctionalTest('analyze',
'test/functional/fixtures/null_safety/opted_in_custom_config_version_comment');
await process.shouldExit(0);
});
});
}
42 changes: 42 additions & 0 deletions test/utils/get_dart_version_comment_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
@TestOn('vm')
import 'package:dart_dev/src/utils/get_dart_version_comment.dart';
import 'package:test/test.dart';

void main() {
group('getDartVersionComment returns the version comment in a Dart file', () {
test('', () {
expect(
getDartVersionComment([
'//@dart=2.9',
'',
'main() {}',
].join('\n')),
'//@dart=2.9');
});

test('allowing for whitespace', () {
expect(getDartVersionComment('//@dart=2.9'), '//@dart=2.9');
expect(
getDartVersionComment('// @dart = 2.9 '), '// @dart = 2.9 ');
});

test('regardless of which line it appears on', () {
expect(getDartVersionComment('\n\n//@dart=2.9\n\n'), '//@dart=2.9');
});

test(
'ignores version comments that don\'t start at the beginning of the line',
() {
const wellFormedVersionComment = '//@dart=2.9';
expect(getDartVersionComment(wellFormedVersionComment), isNotNull,
reason: 'test setup check');

expect(getDartVersionComment(' $wellFormedVersionComment'), isNull);
expect(getDartVersionComment('"$wellFormedVersionComment"'), isNull);
});

test('ignores incomplete version comments', () {
expect(getDartVersionComment('//@dart='), isNull);
});
});
}