Link a file with no extension to an existing language

Hi everyone,

I have a weird question with no answer for the moment, and hopefully some of you may have great ideas !

Let’s say I have a project that contains a file with no extension (like .java, .c, or else).
I am developing a plugin to do some analysis on this file, define metrics, highlightings, so it will be uploaded on the SonarQube server.
Then, on the web interface I can see it is linked to an “Unknown language”.

My goal is to have it linked to an existing and defined language, but as it has no file extension and the scanner seems to only rely on file extensions, I cannot link it automatically.
Renaming files before analysis, or adding a fake extension are not options I would like to consider… if possible, of course.

Is there a way to manually link this file a to “known” language during scanner analysis ?
Or any trick with language suffixes to match a filename and not only an extension ?

Thank you very much for your thoughts on this ! :slightly_smiling_face:


I guess you have implemented the extension point org.sonar.api.resources.AbstractLanguage to declare your language. This extension point only allow to declare file suffixes (=extension) to recognize that a file belong to this language.
An option is to return an empty list. In this case the javadoc says all source files should be associated to this language (I have not verified this is still the case). I’m not really promoting this way, since it will prevent to analyze files of different languages.

Your best option is probably to declare a fake file suffix (like .mycustomlanguage) that will in fact be useless (just to avoid returning an empty list), and then during the analysis, use the scanner property sonar.lang.patterns.<your language key> to provide a pattern to match files of your language only (assuming there is a pattern to identify them).
sonar.lang.patterns.<your language key> is more powerful because you can pass a full path pattern (including folders). For example if your language key is javaplusplus and you know all your language source files are in subfolder xyz then simply run analysis with:
sonar-scanner -Dsonar.lang.patterns.javaplusplus=**/xyz/**/*

If there is no path patterns to differentiate your language files from other files… then situation is more complex :frowning: You would have to pre-process your project to move files in separate folders, or add file suffixes.


Thank you very much for your detailed answer !
Your guess is right, I’m using the AbstractLanguage extension point.
I already tried with an empty list of suffixes but… well, like you said, not really a good idea.

I did not know about this scanner property, it seems interesting, I will give it a try as finding a pattern should be easy.
I guess this one cannot be set inside my plugin implementation, right ?
It is probably used before my sensor execution…

The property is indeed read very early in the scanner lifecycle. You can give a try to set it in a ProjectBuilder which is the first extension point that is processed. AFAIR it is deprecated but since it has no replacement for now, I don’t think it will be removed anytime soon (but no promises :slight_smile: ).

Ok, I’ll think about it, but the scanner property seems promising.
Thanks again ! :slightly_smiling_face:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.