Since version 26.0.1, Command Line Tools has integrated codescanner to detect resource leaks in the entire project. You can manually fix code defects based on the warning prompts in the check results to guarantee code quality during development.
The codescanner command format is as follows:
codescanner [options] [projectPath]
options: (optional) command-line configuration parameters. For details, see Table 1.
projectPath: (optional) root directory of the project to be checked. Both absolute and relative paths are supported. The default value is the current directory.
Parameter | Description |
|---|---|
-h, --help | Flag to display the help information for codescanner. |
-v, --version | Flag to display the version of codescanner. |
-s, --sdkPath | (Optional) HarmonyOS SDK path specified during ArkTS code check. The HarmonyOS SDK loading priority is as follows: SDK path specified in the command > Path of the SDK bundled with Command Line Tools > Paths from environment variables DEVECO_SDK_HOME and DEVECO_PATH/sdk/default. By default, the path of the HarmonyOS SDK bundled with Command Line Tools is used. |
-o, --output | (Optional) Output location of the check report. By default, the report is output to the output folder in the current directory. |
-f, --format | (Optional) Output format of the check report, which can be sarif (default) and html. |
-l, --logPath | (Optional) Output location of runtime logs. By default, runtime logs are output to the scanner folder in the current directory. |
-c, --config | (Optional) Location of the codescanner check rule configuration file. The tool performs checks based on the configuration file. By default, all ArkTS and C++ code check rules are applied. |
--compileCommandsDir | (Mandatory for C++ code check) Directory containing compile_commands.json (compilation database file).
|
--no-cpp | (Optional) Flag to disable C++ code check rules. |
--no-arkts | (Optional) Flag to disable ArkTS code check rules. |
codescanner check rule configuration file
The rule configuration file is a JSON file manually created by developers. The configuration fields and descriptions are as follows:
ruleId: rule name.
Example:
// Rule configuration file: xxxxxx.json
{
"rules": { // Rule structure
"cpp": { // C++ code check rules
"ResourceLeak": [ // Rule set
{
"ruleId": "FileHandleNotClosed", // Rule name
"severity": "warning/error/fatal", // Alarm severity of this rule
"skip": false // Whether to skip the check against this rule
},
{
"ruleId": "MemoryNotFreed", // Rule name
"severity": "warning/error/fatal", // Alarm severity of this rule
"skip": false // Whether to skip the check against this rule
}
]
},
"arkts": { // ArkTS code check rules
"ResourceLeak": [ // Rule set
{
"ruleId": "MediaResourceNotReleased", // Rule name
"severity": "warning/error/fatal", // Alarm severity of this rule
"skip": false // Whether to skip the check against this rule
},
{
"ruleId": "FileDescriptorNotClosed", // Rule name
"severity": "warning/error/fatal", // Alarm severity of this rule
"skip": false // Whether to skip the check against this rule
}
]
}
}
} codescanner -h Usage: codescanner [options] [projectPath] Arguments: projectPath Path to the project root directory Options: -v, --version output the version number -s, --sdkPath <dir> Path to the SDK directory -o, --output <dir> Directory for output (default: "output") -f, --format <format> Output format: sarif or html (default: "sarif") -l, --logPath <dir> Directory for log files (default: "scanner/logs") -c, --config <path> Path to the rule config file --compileCommandsDir <dir> Directory containing compile_commands.json (C++ rules) --no-cpp Disable C++ rule checking --no-arkts Disable ArkTS rule checking -h, --help display help for command
codescanner -v
Check the current directory against C++ rules. The check results are saved in the output folder under the current directory, and runtime logs are saved in the scanner folder under the current directory.
codescanner --compileCommandsDir D:\MyApplication\.idea\.deveco\cxx
codescanner D:\MyApplication --compileCommandsDir D:\MyApplication\.idea\.deveco\cxx
codescanner D:\MyApplication --compileCommandsDir D:\MyApplication\.idea\.deveco\cxx -s D:\HarmonyOS\sdk
codescanner D:\MyApplication --compileCommandsDir D:\MyApplication\.idea\.deveco\cxx -f html
codescanner D:\MyApplication --compileCommandsDir D:\MyApplication\.idea\.deveco\cxx -f html -o D:\MyApplication\codescanner\reports
codescanner D:\MyApplication --compileCommandsDir D:\MyApplication\.idea\.deveco\cxx -f html -l D:\MyApplication\codescanner\logs
codescanner D:\MyApplication --compileCommandsDir D:\MyApplication\.idea\.deveco\cxx -f html -c D:\MyApplication\codescanner\rule_config.json
codescanner D:\MyApplication -f html --no-cpp
codescanner D:\MyApplication --compileCommandsDir D:\MyApplication\.idea\.deveco\cxx -f html --no-arkts