[BUG] check-unused-code unexpected behavior when used with injectable lib
ivan-semkin opened this issue · 11 comments
Environment and configuration
DCM version: 5.5.1
Dart SDK version: 2.19.2
What did you do?
I have a project that consists of the main lib/ folder, and several sub-packages in common/ and stuff/ folders.
When running flutter pub run dart_code_metrics:metrics check-unused-code with both --monorepo and without it, the linter seemed to only analyze the contents of the main package (i.e. the lib/ folder).
So I figured that I might use this command to achieve my desired result of analyzing all the subpackages by listing them manually:
flutter pub run dart_code_metrics:metrics check-unused-code lib common/ stuff/
And it did the trick. For some time at least.
What did you expect to happen?
I expected the tool to analyze my entire project.
What actually happened?
When used together with the injectable library, check-unused-code marks the -Impl classes in stuff/ subpackage as unused, even though it's being referenced in the main package as folows:
lib/di/injectors.config.dart:
gh.factory<_i31.ExampleDataHolder>(() =>
_i941.ExampleDataHolderImpl(gh<_i939.PreferencesEditor>()));
stuff/example_data_holder.dart:
@Injectable(as: ExampleDataHolder)
class ExampleDataHolderImpl extends BaseExampleDataHolder
implements ExampleDataHolder {
const ExampleDataHolderImpl(super.preferencesEditor);
}
injectors.config.dart is auto-generated, but does not follow the pattern specified in the docs: {/**.g.dart,/**.freezed.dart}
Participation
- I am willing to submit a pull request for this issue.
Additional comments
I'd also like to know whether my approach to analyzing all sub-packages is valid and correct.
Let me know if you need a more comprehensive sample, I will create a repo with a reproduction.
Thank you for submitting the bug, looks like _i941.ExampleDataHolderImpl reference is not picked up correctly. Could you please create a repro so I can run my tests on it?
I'd also like to know whether my approach to analyzing all sub-packages is valid and correct.
You need to list the folders manually, there is no such thing as "autodiscoverability" for any folder, except lib.
@ivan-semkin-gismart any chance you can create a repro?
@incendial I will. Sorry for the delay, been busy at work
No problem and thank you! Waiting for the repro then.
(I am the same person that reported this issue initially)
I finally figured this out. This only happens when the analyzed file is a part of of some other file
https://github.com/vanyasem/dart-code-metrics-issue-repro
I made an incredibly small reproduction there ^
There is no meaningful code there, you don't even need to launch it. It's just a default flutter counter app. It's specifically made to showcase an issue in check-unused-code cli command with part of files.
Please do not run generation manually, the precompiled files are already included in the repo.
You'd just need to pub get in the root project, and run pub get in the stuff subproject.
Thanks for the repro! Good news that the Teams version works fine on this example.
I'll check whether this difference is portable to this version.
Fixed, thanks again for the report + repro. Will become available in the next release.
thank you, much appreciated. I will close the issue for now, will reopen if I still manage to reproduce it later
can confirm, the issue was fixed in 5.7.0. thank you once again
Thank you, great to hear!