Skip to content

Commit

Permalink
[linter] Error on using dev dependency in hooks
Browse files Browse the repository at this point in the history
`hook/` is a reserved directory.
#54334

And the two hooks currently in existence require to be invoked with
the same dependencies as the root package. Hence the dependencies of
the hooks should be normal dependencies and not dev dependencies.

Bug: dart-lang/native#160
Change-Id: I7cf48659bd240c2e46b5bdede2a336763301b8c5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/383301
Reviewed-by: Samuel Rawlins <[email protected]>
Commit-Queue: Daco Harkes <[email protected]>
Reviewed-by: Hossein Yousefi <[email protected]>
  • Loading branch information
dcharkes authored and Commit Queue committed Sep 3, 2024
1 parent e099a00 commit bb49ef2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pkg/linter/lib/src/ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ bool isInLibDir(CompilationUnit node, WorkspacePackage? package) {

/// Return `true` if this compilation unit [node] is declared within a public
/// directory in the given [package]'s directory tree. Public dirs are the
/// `lib` and `bin` dirs.
/// `lib` and `bin` dirs and the build and link hook file.
//
// TODO(jakemac): move into WorkspacePackage
bool isInPublicDir(CompilationUnit node, WorkspacePackage? package) {
Expand All @@ -166,7 +166,13 @@ bool isInPublicDir(CompilationUnit node, WorkspacePackage? package) {
if (cuPath == null) return false;
var libDir = path.join(package.root, 'lib');
var binDir = path.join(package.root, 'bin');
return path.isWithin(libDir, cuPath) || path.isWithin(binDir, cuPath);
// Hook directory: https://github.com/dart-lang/sdk/issues/54334,
var buildHookFile = path.join(package.root, 'hook', 'build.dart');
var linkHookFile = path.join(package.root, 'hook', 'link.dart');
return path.isWithin(libDir, cuPath) ||
path.isWithin(binDir, cuPath) ||
cuPath == buildHookFile ||
cuPath == linkHookFile;
}

/// Returns `true` if the keyword associated with the given [token] matches
Expand Down
59 changes: 59 additions & 0 deletions pkg/linter/test/rules/depend_on_referenced_packages_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,49 @@ dev_dependencies:
]);
}

void test_referencedInBuildHook_listedInDeps() async {
newFile(testPackagePubspecPath, r'''
name: fancy
version: 1.1.1
dependencies:
meta: any
''');
var hookFile =
newFile('$testPackageRootPath/hook/build.dart', sourceReferencingMeta);
result = await resolveFile(hookFile.path);
await assertNoDiagnosticsIn(result.errors);
}

void test_referencedInBuildHook_listedInDevDeps() async {
newFile(testPackagePubspecPath, r'''
name: fancy
version: 1.1.1
dev_dependencies:
meta: any
''');
var hookFile =
newFile('$testPackageRootPath/hook/build.dart', sourceReferencingMeta);
result = await resolveFile(hookFile.path);
await assertDiagnosticsIn(result.errors, [
lint(7, 24),
]);
}

void test_referencedInBuildHook_missingFromPubspec() async {
newFile(testPackagePubspecPath, r'''
name: fancy
version: 1.1.1
''');
var hookFile =
newFile('$testPackageRootPath/hook/build.dart', sourceReferencingMeta);
result = await resolveFile(hookFile.path);
await assertDiagnosticsIn(result.errors, [
lint(7, 24),
]);
}

test_referencedInLib_flutterGen() async {
var packageConfigBuilder = PackageConfigFileBuilder();
packageConfigBuilder.add(
Expand Down Expand Up @@ -109,6 +152,22 @@ var y = x;
''');
}

void test_referencedInLinkHook_listedInDevDeps() async {
newFile(testPackagePubspecPath, r'''
name: fancy
version: 1.1.1
dev_dependencies:
meta: any
''');
var hookFile =
newFile('$testPackageRootPath/hook/link.dart', sourceReferencingMeta);
result = await resolveFile(hookFile.path);
await assertDiagnosticsIn(result.errors, [
lint(7, 24),
]);
}

test_referencedInTest_listedInDevDeps() async {
newFile(testPackagePubspecPath, r'''
name: fancy
Expand Down

0 comments on commit bb49ef2

Please sign in to comment.