Skip to main content

Check Unused Files

Checks unused *.dart files.

To execute the command, run:

$ dcm check-unused-files lib # or dcm uf lib

Full command description:

Usage: dcm check-unused-files [arguments...] <directories>
-h, --help Print this usage information.


-r, --reporter=<console> Analysis output format.
[console(Free+) (default), json(Teams+), codeclimate(Teams+), gitlab(Teams+), checkstyle(Teams+), sonar(Teams+)]
-a, --absolute-path Show absolute paths in console reporter output.
--json-path=<path/to/file.json> Path to the JSON file with the analysis output.


-c, --print-config Print resolved config.


--root-folder=<./> Root folder.
(defaults to the current directory)
-s, --sdk-path=<directory-path> Dart SDK directory path.
If the project has a `.fvm/flutter_sdk` symlink, it will be used if the SDK is not found.
-e, --exclude=<{**/*.g.dart,**/*.freezed.dart}> File paths in Glob syntax to be exclude.
(defaults to "{**/*.g.dart,**/*.freezed.dart}")


--[no-]congratulate Don't show output even when there are no issues.


--verbose Show verbose logs.


--ci-key The license key to run on CI server. Can be provided via DCM_CI_KEY env variable.
--email The email used to purchase the license. Can be provided via DCM_EMAIL env variable.


--no-analytics Disable sending anonymous usage statistics.


-m, --monorepo Include publicly exported files into the check.


--[no-]fatal-unused Treat find unused file as fatal.
(defaults to on)

Suppressing the Command

In order to suppress the command add ignore_for_file: unused-files to the beginning of a file.

Cyclic Usage Detection

If you have several files that reference each other, but are not referenced by other files and technically are unused, the command will only detect 1 level of cyclic usage.

Monorepo Support

warning

By default, this command treats all files that are exported from the package as used. It uses check-exports-completeness results and won't report even transitive public entities that are not exported directly.

This command also follows this convention of locating implementation files in the lib/src/ folder. If you don't follow this convention, you will need to pass the --monorepo flag for this command to work.

To disable this behavior use --monorepo flag. This might be useful when all the packages in your repository are only used within the repository and are not published to the pub.

Overriding the Monorepo Mode for a Particular Package

To override the monorepo mode for a particular package, set the monorepo entry in the analysis_options.yaml.

For example, if publicly exported files of a particular package are not expected to be changed, setting the monorepo entry to false and passing the --monorepo option will enable the monorepo mode for all other packages, but not the one with monorepo: false.

Output Example

Console (default)

Use --reporter=console to get output in console format.

Console

JSON

Use --reporter=json to get output as a single JSON object containing metadata and the list of unused files.

Format specification

The root object fields are

  • formatVersion - an integer representing the format version (will be incremented each time the serialization format changes)
  • timestamp - a creation time of the report in YYYY-MM-DD HH:MM:SS format
  • unusedFiles - an array of unused files
{
"formatVersion": 2,
"timestamp": "2021-04-11 14:44:42",
"unusedFiles": [
{
...
},
{
...
},
{
...
}
]
}

The unusedFiles object fields are

  • path - a relative path of the unused file
  • usedOnlyInTests a boolean value that indicates files used only by test files
{
"path": "lib/src/some/file.dart",
"usedOnlyInTests": false
}

GitLab

Use --reporter=gitlab to get output in a GitLab-compatible format. To learn how to integrate DCM with GitLab, refer to this guide.

Code Climate

Use --reporter=codeclimate to get output in Code Climate format.

Output example
{"type":"issue","check_name":"unused-file-issue","description":"Unused file","categories":["Bug Risk"],"location":{"path":"lib/src/unused_widget.dart","positions":{"begin":{"column":1,"line":1},"end":{"column":1,"line":1}}},"severity":"major","fingerprint":"527a5c75d3a9cd48e159621891ac24e7"}

Checkstyle

Use --reporter=checkstyle to get output in Checkstyle format.

Output example
<?xml version="1.0"?>
<checkstyle version="10.0">
<file name="example.dart">
<error line="0" message="Unused file" severity="warning" source="unused-file-issue"/>
</file>
</checkstyle>
note

Checkstyle format is supported by Bitbucket. To learn how to integrate DCM with Bitbucket, refer to this guide.

Sonar

Use --reporter=sonar to get output in SonarQube's generic format for external issues.

Output example
{
"rules": [
{
"cleanCodeAttribute": "CLEAR",
"description": "To learn more, visit the documentation https://dcm.dev/docs/cli/code-quality-checks/unused-files/",
"engineId": "dcm",
"id": "unused-file-issue",
"impacts": [
{
"severity": "MEDIUM",
"softwareQuality": "MAINTAINABILITY"
}
],
"name": "unused-file-issue"
}
],
"issues": [
{
"primaryLocation": {
"filePath": "lib/src/unused_widget.dart",
"message": "Unused file"
},
"ruleId": "unused-file-issue"
}
]
}