linter icon indicating copy to clipboard operation
linter copied to clipboard

Avoid `depend_on_referenced_packages` false positive in colocated test directory

Open rorystephenson opened this issue 3 years ago • 4 comments

Description

When test files are colocated (in a test/ subdirectory of lib/src/) the depend_on_referenced_packages lint does not consider dev dependencies as part of the file's dependencies and thus gives false positives for all referenced dev dependencies.

rorystephenson avatar May 30 '22 17:05 rorystephenson

fyi @jakemac53

pq avatar May 31 '22 17:05 pq

All files under lib are considered public, anybody could import them with a package: import. So this is working as intended. I don't think we want to special case directories called test or anything like that.

jakemac53 avatar May 31 '22 17:05 jakemac53

I don't think we want to special case directories called test or anything like that.

The proposal is to use the existing detection for test directories (see the PR code), not a special case for directories called "test/".

All files under lib are considered public, anybody could import them with a package: import.

I opened an issue a while back to suggest a lint to avoid this issue: https://github.com/dart-lang/linter/issues/3289

rorystephenson avatar Jun 02 '22 13:06 rorystephenson

The proposal is to use the existing detection for test directories (see the PR code), not a special case for directories called "test/".

That detection looks very permissive and is probably designed for a different use case.

For instance, one strategy I have seen is to create a testing directory under lib with actually intentional shared helper code for writing tests (generated mocks, etc). In that case it is important for the dependencies of those libraries to be real dependencies.

Creating files under lib which import packages that are not regular dependencies will also break build_runner, because it expects to be able to compile all of lib.

In general, we expect published packages to use the package layout conventions, and for good reason imo.

I opened an issue a while back to suggest a lint to avoid this issue: #3289

That doesn't really avoid the issue, not everybody uses the linter (or would have that lint enabled), and it also doesn't allow for the valid use case of a testing directory under lib which contains test helper code. It also wouldn't fix things like build_runner that expect packages to have a certain layout.

jakemac53 avatar Jun 02 '22 14:06 jakemac53

I agree. (Sorry for the delay.) I think we can only support the package conventions laid out by convention. I'll close this for now.

srawlins avatar May 22 '23 21:05 srawlins