EsLint issues Import fails with Expected BEGIN_ARRAY

I am trying to import EsLint Issues to my sonarCloud analysis. I wrote a custom formatter and when i execute locally sonar-scanner, everything works. If i run it on bitbucket server during pull request analysis, i got the error

ERROR: Error during SonarScanner execution
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
	at com.google.gson.Gson.fromJson(Gson.java:975)
	at com.google.gson.Gson.fromJson(Gson.java:901)
	at org.sonar.plugins.javascript.external.TslintReportSensor.importReport(TslintReportSensor.java:64)

My structure of the esLint Report is as defined in the documentation:

{issues: [{issues1}, {issue2},…]}

What can be the problem in this case, that i am getting this error?

Hi,

Can you share your formatted report?

 
Ann

my Report is:

{"issues":[{"engineId":"eslint","ruleId":"camelcase","primaryLocation":{"message":"Identifier 'v7_startTransition' is not in camel case.","filePath":"C:\\path\to\file\\App.tsx","textRange":{"startLine":21,"endLine":21,"endColumn":85}},"type":"BUG","severity":"MAJOR"},{"engineId":"eslint","ruleId":"@typescript-eslint/no-unused-vars","primaryLocation":{"message":"'id' is assigned a value but never used.","filePath":"C:\\Path\to\file.ts","textRange":{"startLine":86,"endLine":86,"endColumn":19}},"type":"BUG","severity":"MAJOR"}]}

i tried also with the json formatter, but then i get the error Message: Expected BEGIN OBJECT but was BEGIN ARRay

[{"engineId":"eslint","ruleId":"camelcase","primaryLocation":{"message":"Identifier 'v7_startTransition' is not in camel case.","filePath":"C:\\path\to\file\\App.tsx","textRange":{"startLine":21,"endLine":21,"endColumn":85}},"type":"BUG","severity":"MAJOR"},{"engineId":"eslint","ruleId":"@typescript-eslint/no-unused-vars","primaryLocation":{"message":"'id' is assigned a value but never used.","filePath":"C:\\path\to\file.ts","textRange":{"startLine":86,"endLine":86,"endColumn":19}},"type":"BUG","severity":"MAJOR"}]

So in the end both versions of the report don’t work

Hi,

I didn’t pay enough attention to your OP, for which I apologize.

You’re saying your report is in the Generic Issues format. With what parameter are you feeding it into analysis? Because if you’re feeding it in as the RHS of sonar.eslint.reportPaths, then it’s not surprising you get an error, since the report doesn’t match that expected format.

And on the topic of expected format, it doesn’t seem to match the format of the Generic Issue report either.

 
HTH,
Ann

I tried it based on this documentation with a custom formatter: How to import ESLint issues into SonarQube overview analysis in a frontend application | by José Queirós | Medium

and the second report was generated with “eslint --format=json -o eslint_report.json”, which was stated in the sonarCloud docs.

i tried it with both properties, but none of it worked
sonar.eslint.reportPaths = eslint_report.json
sonar.externalIssuesReportPaths = eslint_report.json

Hi,

Which report did you provide above? The one from the 3rd-party blog or the one from the command in our docs?

 
Thx,
Ann

First try was with the report from above from the 3rd party blog.

Second try was like describe here at the JS/TS section

: eslint -f json src -o eslint_report.json

  • sonar.eslint.reportPaths
    • Comma-delimited list of paths to JSON ESLint reports (use -f json ESLint option).

For me the section of Generic issue data sounded like any linters or static analysis tools which are not listed in the external analysis reports section.

If i am running the sonar scanner locally against sonarcloud(and use the report format from the third party blog), the eslint issues are shown in sonarcloud.
if i am using it the same way in my bitbucket pipeline i get the errors described as above.

If i really need to format my eslint report data like this: How do i get this rule information? How can i produce a report like this?

Hi,

Have you tried diff-ing the two reports?

 
Ann

i know whats the difference between the reports. The one from the 3rd party blog has the array of issues wrapped in an issues object and the other one only has the array of issues.
That is my problem with Sonarcloud. If i am using the report from the 3rd party blog where i have an Object that contains the array, i get the error message that Sonarcloud expects an Array, but it was an Object. If i am using the report generated with -f json, where i have the array, Sonarcloud complains that it expects an object at Line 1 Column 2, but it was an array.
So with those error messages I am running in circles.

Hi,

Can you provide the report generated with

?

Because what you posted here

doesn’t match what I see in the eslint docs

 
Thx,
Ann

looks like es lint can generate different reports with the json specifier depending if you use --format=json or -f json: But also for the report which is as in the eslint docs genereates the same error in sonar cloud… ( Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $)

[{"filePath":"path\\src\\App.tsx","messages":[{"ruleId":"camelcase","severity":2,"message":"Identifier 'v7_startTransition' is not in camel case.","line":21,"column":67,"nodeType":"Identifier","messageId":"notCamelCase","endLine":21,"endColumn":85}],"suppressedMessages":[],"errorCount":1,"fatalErrorCount":0,"warningCount":0,"fixableErrorCount":0,"fixableWarningCount":0,"source":"import { useLocalStorage } from 'usehooks-ts';\r\nimport { StrictMode } from 'react';\r\nimport { QueryClientProvider } from '@tanstack/react-query';\r\nimport { CssBaseline, ThemeProvider } from '@mui/material';\r\nimport { moxisDefaultTheme } from '@moxis/moxis-design';\r\nimport { RouterProvider } from 'react-router-dom';\r\nimport routes from '@/routes';\r\nimport { ReactQueryDevtools } from '@tanstack/react-query-devtools';\r\nimport { queryClient } from '@/lib/tanStack/config.ts';\r\nimport NotificationContainer from '@/components/notifications/NotificationContainer.tsx';\r\n// ReactQueryDevtools: https://tanstack.com/query/latest/docs/react/devtools\r\n\r\nconst App = () => {\r\n    const [isDarkMode] = useLocalStorage('darkMode', false, { initializeWithValue: true });\r\n    return (\r\n        <StrictMode>\r\n            <QueryClientProvider client={queryClient}>\r\n                <ThemeProvider theme={moxisDefaultTheme(isDarkMode)}>\r\n                    <NotificationContainer>\r\n                        <CssBaseline />\r\n                        <RouterProvider router={routes} future={{ v7_startTransition: true }} />\r\n                        <ReactQueryDevtools initialIsOpen={false} position='bottom-left' />\r\n                    </NotificationContainer>\r\n                </ThemeProvider>\r\n            </QueryClientProvider>\r\n        </StrictMode>\r\n        //Test\r\n    );\r\n};\r\n\r\nexport default App;\r\n","usedDeprecatedRules":[]},...]

Hi,

So you’ve generated a report in a different format. Is it imported correctly by analysis?

 
Ann

As written already above, that report causes the same problem as before:
Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

Hi,

Things work best when we’re patient with each other.

A lot was written ‘above’.

Our docs are pretty clear about which option you should use to generate the expected format for import.

So forgive me if I’d like explicit confirmation that when you used the correct option to generate the expected format, you still received an error.

Could we have the full analysis log, please?

The analysis / scanner log is what’s output from the analysis command. Hopefully, the log you provide - redacted as necessary - will include that command as well.

This guide will help you find them.

 
Thx,
Ann