Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for checkstyle plugin java. #50

Open
231tr0n opened this issue Dec 8, 2024 · 15 comments
Open

Support for checkstyle plugin java. #50

231tr0n opened this issue Dec 8, 2024 · 15 comments

Comments

@231tr0n
Copy link
Contributor

231tr0n commented Dec 8, 2024

Hi! Is it possible to add checkstyle linter support in guard-collection.

@xiaoshihou514
Copy link
Collaborator

Just took a quick look, it doesn't seem that checkstyle has a widely used cli interface.

@231tr0n
Copy link
Contributor Author

231tr0n commented Dec 8, 2024

Just took a quick look, it doesn't seem that checkstyle has a widely used cli interface.

You can actually install checkstyle in ubuntu using sudo apt install checkstyle.

@231tr0n
Copy link
Contributor Author

231tr0n commented Dec 8, 2024

Other way to run it would probably be mvn checkstyle:check ig.

@xiaoshihou514
Copy link
Collaborator

Sure then I guess pr is welcome.

Feel free to ask any questions :)

@231tr0n
Copy link
Contributor Author

231tr0n commented Dec 8, 2024

Sure then I guess pr is welcome.

Feel free to ask any questions :)

I was going to implement this but my only doubt is about how to parse messages from the output. Checkstyle support xml and plain text and does not support json. So I cannot use vim.json to parse it.

@231tr0n
Copy link
Contributor Author

231tr0n commented Dec 8, 2024

[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< com.fibonacci:fibonacci >-----------------------
[INFO] Building fibonacci 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-checkstyle-plugin:3.6.0:check (default-cli) @ fibonacci ---
[INFO] There are 15 errors reported by Checkstyle 9.3 with sun_checks.xml ruleset.
[ERROR] src/main/java/com/fibonacci/Worker.java:[1] (javadoc) JavadocPackage: Missing package-info.java file.
[ERROR] src/main/java/com/fibonacci/Worker.java:[4,3] (javadoc) JavadocVariable: Missing a Javadoc comment.
[ERROR] src/main/java/com/fibonacci/Worker.java:[5,3] (javadoc) JavadocVariable: Missing a Javadoc comment.
[ERROR] src/main/java/com/fibonacci/Worker.java:[7,3] (modifier) RedundantModifier: Redundant 'public' modifier.
[ERROR] src/main/java/com/fibonacci/Worker.java:[7,17] (misc) FinalParameters: Parameter question should be final.
[ERROR] src/main/java/com/fibonacci/Worker.java:[7,21] (coding) HiddenField: 'question' hides a field.
[ERROR] src/main/java/com/fibonacci/Worker.java:[15,32] (misc) FinalParameters: Parameter n should be final.
[ERROR] src/main/java/com/fibonacci/App.java:[12,1] (design) HideUtilityClassConstructor: Utility classes should not have a public or default constructor.
[ERROR] src/main/java/com/fibonacci/App.java:[13,30] (misc) FinalParameters: Parameter n should be final.
[ERROR] src/main/java/com/fibonacci/App.java:[35,27] (misc) FinalParameters: Parameter args should be final.
[ERROR] src/main/java/com/fibonacci/App.java:[36,65] (coding) MagicNumber: '8081' is a magic number.
[ERROR] src/main/java/com/fibonacci/App.java:[40] (sizes) LineLength: Line is longer than 80 characters (found 89).
[ERROR] src/main/java/com/fibonacci/App.java:[46] (sizes) LineLength: Line is longer than 80 characters (found 100).
[ERROR] src/main/java/com/fibonacci/App.java:[47,42] (coding) MagicNumber: '400' is a magic number.
[ERROR] src/main/java/com/fibonacci/App.java:[51,42] (coding) MagicNumber: '400' is a magic number.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.549 s
[INFO] Finished at: 2024-12-08T21:20:07Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:3.6.0:check (default-cli) on project fibonacci: You have 15 Checkstyle violations. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

The output looks something of this sort. And I have to parse the ones in between.

@xiaoshihou514
Copy link
Collaborator

It's possible to use regex

local lint = require('guard.lint')
return {
cmd = 'clang-tidy',
args = { '--quiet' },
parse = lint.from_regex({
source = 'clang-tidy',
regex = ':(%d+):(%d+):%s+(%w+):%s+(.-)%s+%[(.-)%]',
groups = { 'lnum', 'col', 'severity', 'message', 'code' },
severities = {
information = lint.severities.info,
hint = lint.severities.info,
note = lint.severities.style,
},
}),
}

Maybe it's possible to use some flags to silent some of the non-diagnostic output

@231tr0n
Copy link
Contributor Author

231tr0n commented Dec 8, 2024

Ok. I reduced it to less output using mvn -q checkstyle:check.

[ERROR] src/main/java/com/fibonacci/Worker.java:[1] (javadoc) JavadocPackage: Missing package-info.java file.
[ERROR] src/main/java/com/fibonacci/Worker.java:[4,3] (javadoc) JavadocVariable: Missing a Javadoc comment.
[ERROR] src/main/java/com/fibonacci/Worker.java:[5,3] (javadoc) JavadocVariable: Missing a Javadoc comment.
[ERROR] src/main/java/com/fibonacci/Worker.java:[7,3] (modifier) RedundantModifier: Redundant 'public' modifier.
[ERROR] src/main/java/com/fibonacci/Worker.java:[7,17] (misc) FinalParameters: Parameter question should be final.
[ERROR] src/main/java/com/fibonacci/Worker.java:[7,21] (coding) HiddenField: 'question' hides a field.
[ERROR] src/main/java/com/fibonacci/Worker.java:[15,32] (misc) FinalParameters: Parameter n should be final.
[ERROR] src/main/java/com/fibonacci/App.java:[12,1] (design) HideUtilityClassConstructor: Utility classes should not have a public or default constructor.
[ERROR] src/main/java/com/fibonacci/App.java:[13,30] (misc) FinalParameters: Parameter n should be final.
[ERROR] src/main/java/com/fibonacci/App.java:[35,27] (misc) FinalParameters: Parameter args should be final.
[ERROR] src/main/java/com/fibonacci/App.java:[36,65] (coding) MagicNumber: '8081' is a magic number.
[ERROR] src/main/java/com/fibonacci/App.java:[40] (sizes) LineLength: Line is longer than 80 characters (found 89).
[ERROR] src/main/java/com/fibonacci/App.java:[46] (sizes) LineLength: Line is longer than 80 characters (found 100).
[ERROR] src/main/java/com/fibonacci/App.java:[47,42] (coding) MagicNumber: '400' is a magic number.
[ERROR] src/main/java/com/fibonacci/App.java:[51,42] (coding) MagicNumber: '400' is a magic number.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:3.6.0:check (default-cli) on project fibonacci: You have 15 Checkstyle violations. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

The log looks something like this. But it still has few redundant lines at the end. Do you have any idea on how can I get around these. Like is the regex going to ignore lines if they dont match the regex exp?

@xiaoshihou514
Copy link
Collaborator

xiaoshihou514 commented Dec 8, 2024

Maybe you can try to match the src/main/... and check if it's a file. Use a parse function in this case

local lint = require('guard.lint')
return {
cmd = 'codespell',
args = {
'-',
},
stdin = true,
parse = function(result, bufnr)
local diags = {}
local t = vim.split(result, '\n')
for i, e in ipairs(t) do
local lnum = e:match('^%d+')
if lnum then
diags[#diags + 1] =
lint.diag_fmt(bufnr, tonumber(lnum) - 1, 0, t[i + 1]:gsub('\t', ''), 2, 'codespell')
end
end
return diags
end,
}

@231tr0n
Copy link
Contributor Author

231tr0n commented Dec 10, 2024

@xiaoshihou514 sorry for this ping but I wanted you to take a look at this configuration for checkstyle and let me know if everything is right since even if I call :lua require("guard.lint").do_lint(), it does not show any diagnostics.

	ft("java"):lint({
		cmd = "checkstyle",
		args = {
			"-c",
			"sun_checks.xml",
		},
		fname = true,
		parse = lint.from_regex({
			source = "checkstyle",
			regex = "%[(%w+)%]%s+.-:(%d+):?(%d*):%s+(.-)%s+%[(.-)%]",
			groups = { "severity", "lnum", "col", "message", "code" },
			severities = {
				ERROR = lint.severities.error,
				WARN = lint.severities.warning,
				INFO = lint.severities.info,
				DEBUG = lint.severities.style,
			},
		}),
	})

I verified the working of the regex and it works well. So, regex is not the issue afaik.

I also had one more doubt, like in few linter logs, there is no column number, so the above regex returns empty column number.

For reference this is the log of checkstyle

Starting audit...
[ERROR] /home/zeltron/git/concurrency/java-platform/src/main/java/com/fibonacci/App.java:1: Missing package-info.java file. [JavadocPackage]
[ERROR] /home/zeltron/git/concurrency/java-platform/src/main/java/com/fibonacci/App.java:12:1: Utility classes should not have a public or default constructor. [HideUtilityClassConstructor]
[ERROR] /home/zeltron/git/concurrency/java-platform/src/main/java/com/fibonacci/App.java:13:30: Parameter n should be final. [FinalParameters]
[ERROR] /home/zeltron/git/concurrency/java-platform/src/main/java/com/fibonacci/App.java:35:27: Parameter args should be final. [FinalParameters]
[ERROR] /home/zeltron/git/concurrency/java-platform/src/main/java/com/fibonacci/App.java:36:65: '8081' is a magic number. [MagicNumber]
[ERROR] /home/zeltron/git/concurrency/java-platform/src/main/java/com/fibonacci/App.java:40: Line is longer than 80 characters (found 89). [LineLength]
[ERROR] /home/zeltron/git/concurrency/java-platform/src/main/java/com/fibonacci/App.java:45: Line is longer than 80 characters (found 114). [LineLength]
[ERROR] /home/zeltron/git/concurrency/java-platform/src/main/java/com/fibonacci/App.java:46:42: '400' is a magic number. [MagicNumber]
[ERROR] /home/zeltron/git/concurrency/java-platform/src/main/java/com/fibonacci/App.java:50:42: '400' is a magic number. [MagicNumber]
Audit done.

@xiaoshihou514
Copy link
Collaborator

xiaoshihou514 commented Dec 11, 2024

I will just put print statements inside guard _ツ_/

Not ideal, but then again most formatters and linters aren't that complicated...

I think a nil column number is fine?

As for none matching lines I need to check the source, I think they are skipped.

@231tr0n
Copy link
Contributor Author

231tr0n commented Dec 11, 2024

I will just put print statements inside guard /

Not ideal, but then again most formatters and linters aren't that complicated...

I think a nil column number is fine?

As for none matching lines I need to check the source, I think they are skipped.

Ok. I think I found out what the issue is. The thing is that #data is returning 0 even though this is the content of data in this line
https://github.com/nvimdev/guard.nvim/blob/d5b4a492ed77107eef43261c2b76124583c27aea/lua/guard/lint.lua#L54

{
  code = 9,
  signal = 0,
  stderr = "Checkstyle ends with 9 errors.\n",
  stdout = "Starting audit...\n[ERROR] /home/zeltron/git/concurrency/java-platf
orm/src/main/java/com/fibonacci/App.java:1: Missing package-info.java file. [Ja
vadocPackage]\n[ERROR] /home/zeltron/git/concurrency/java-platform/src/main/jav
a/com/fibonacci/App.java:12:1: Utility classes should not have a public or defa
ult constructor. [HideUtilityClassConstructor]\n[ERROR] /home/zeltron/git/concu
rrency/java-platform/src/main/java/com/fibonacci/App.java:13:30: Parameter n sh
ould be final. [FinalParameters]\n[ERROR] /home/zeltron/git/concurrency/java-pl
atform/src/main/java/com/fibonacci/App.java:35:27: Parameter args should be fin
al. [FinalParameters]\n[ERROR] /home/zeltron/git/concurrency/java-platform/src/
main/java/com/fibonacci/App.java:36:65: '8081' is a magic number. [MagicNumber]
\n[ERROR] /home/zeltron/git/concurrency/java-platform/src/main/java/com/fibonac
ci/App.java:40: Line is longer than 80 characters (found 89). [LineLength]\n[ER
ROR] /home/zeltron/git/concurrency/java-platform/src/main/java/com/fibonacci/Ap
p.java:46: Line is longer than 80 characters (found 100). [LineLength]\n[ERROR]
 /home/zeltron/git/concurrency/java-platform/src/main/java/com/fibonacci/App.ja
va:47:42: '400' is a magic number. [MagicNumber]\n[ERROR] /home/zeltron/git/con
currency/java-platform/src/main/java/com/fibonacci/App.java:51:42: '400' is a m
agic number. [MagicNumber]\nAudit done.\n"
}

@231tr0n
Copy link
Contributor Author

231tr0n commented Dec 11, 2024

I will just put print statements inside guard /
Not ideal, but then again most formatters and linters aren't that complicated...
I think a nil column number is fine?
As for none matching lines I need to check the source, I think they are skipped.

Ok. I think I found out what the issue is. The thing is that #data is returning 0 even though this is the data coming as part of #data

{
  code = 9,
  signal = 0,
  stderr = "Checkstyle ends with 9 errors.\n",
  stdout = "Starting audit...\n[ERROR] /home/zeltron/git/concurrency/java-platf
orm/src/main/java/com/fibonacci/App.java:1: Missing package-info.java file. [Ja
vadocPackage]\n[ERROR] /home/zeltron/git/concurrency/java-platform/src/main/jav
a/com/fibonacci/App.java:12:1: Utility classes should not have a public or defa
ult constructor. [HideUtilityClassConstructor]\n[ERROR] /home/zeltron/git/concu
rrency/java-platform/src/main/java/com/fibonacci/App.java:13:30: Parameter n sh
ould be final. [FinalParameters]\n[ERROR] /home/zeltron/git/concurrency/java-pl
atform/src/main/java/com/fibonacci/App.java:35:27: Parameter args should be fin
al. [FinalParameters]\n[ERROR] /home/zeltron/git/concurrency/java-platform/src/
main/java/com/fibonacci/App.java:36:65: '8081' is a magic number. [MagicNumber]
\n[ERROR] /home/zeltron/git/concurrency/java-platform/src/main/java/com/fibonac
ci/App.java:40: Line is longer than 80 characters (found 89). [LineLength]\n[ER
ROR] /home/zeltron/git/concurrency/java-platform/src/main/java/com/fibonacci/Ap
p.java:46: Line is longer than 80 characters (found 100). [LineLength]\n[ERROR]
 /home/zeltron/git/concurrency/java-platform/src/main/java/com/fibonacci/App.ja
va:47:42: '400' is a magic number. [MagicNumber]\n[ERROR] /home/zeltron/git/con
currency/java-platform/src/main/java/com/fibonacci/App.java:51:42: '400' is a m
agic number. [MagicNumber]\nAudit done.\n"
}

https://github.com/nvimdev/guard.nvim/blob/d5b4a492ed77107eef43261c2b76124583c27aea/lua/guard/lint.lua#L54

@xiaoshihou514, I think this part of the code is the issue
https://github.com/nvimdev/guard.nvim/blob/d5b4a492ed77107eef43261c2b76124583c27aea/lua/guard/lint.lua#L54
It is a table instead of an array and so returns length 0.

@xiaoshihou514
Copy link
Collaborator

Looks fishy... Will take a look tomorrow.

@xiaoshihou514
Copy link
Collaborator

xiaoshihou514 commented Dec 12, 2024

Ahh I see what's happening, checkstyle gave non zero exit code and non empty stderr, so guard thinks it errored out.

You will have to override the default spawn function: https://github.com/nvimdev/guard.nvim/blob/main/ADVANCED.md#custom-logic-with-linters

Seems like error reporting is not there for linters yet, will add it with event pr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants