InputFile.relativePath() obsoleted in 6.6

Since sonar 6.6, InputFile.relativePath() has been obsoleted.
To obtain the same result, I now have to call toString() which feels wrong.
What is the proper way to get the path relative the the projects root.

Hi Troosan,

We have deprecated all path relative methods to encourage analyzer developers to avoid building analyzers coupled with the filesystem layout. This is especially important for SonarSource analyzers that should run in SonarLint, because some IDEs have a virtual filesystem, where you can have a remote (SSH, FTP, mainframe) file open in the IDE, with no existence on disk.

Even in scanner world, relative path is something tricky, because it has to be relative to something (project or module basedir) that is not always well defined (for example in MSBuild projects).

Long story short:

  • try as best as you can to avoid using path methods. Use uri() to get a unique identifier of the file, or filename() if you need to write a rule based on the file name.
  • if you really need a physical access to a file, you can check if the uri() starts with file:// and then convert it to an absolute path

Anyway we are aware of the troubles of those APIs, so we are considering undeprecating those APIs. So another option if you can live with calling deprecated code is to continue using them (in particular absolute path, that we have no plan to remove on scanner side).

Thanks Julien for the very complete answer.
I’ll see if I can use uri(), otherwise I’ll keep the call to relativePath(), as that’s actually what I need. That path has to be passed to a command line (scm) afterwards.