DanielHWe / sonar-fxcop

FxCop plugin for C# or VB.NET projects
13 stars 6 forks source link

trimWorkdir in fxcopSensor.java messing up path #23

Closed dafunkjoker closed 5 years ago

dafunkjoker commented 5 years ago

Current working dir during analysis is: D:\Builds\1\5\Sources\TFSValidation.HelloWorld

But instead of a proper trim, the first ":" just gets removed which leads to a relative path and thus a wrong and very long one. Sometimes too long for windows.

Please change line 221 e.g. from: workDirPath.replace(projectKey.replace(":", ""), projectKey.substring(projectKey.indexOf(':'))).replace(":", "");

To: workDirPath.replace(projectKey.replace(":", ""), projectKey.substring(projectKey.indexOf(':'))).replace(":[^\]", "");

Here some further debug info: INFO: Project key: TFSValidation-TFSValidation.HelloWorld:TFSValidation-TFSValidation.HelloWorld:E697BAEF-147A-400A-87BB-EB20E93AA3D4

INFO: Work Dir: D\Builds\1\5\Sources\TFSValidation.HelloWorld.sonarqube\out.sonar\TFSValidation-TFSValidation.HelloWorld_TFSValidation-TFSValidation.HelloWorld_E697BAEF-147A-400A-87BB-EB20E93AA3D4

INFO: FxCop start create FxCop configuration for 'D:\Builds\1\5\Sources\TFSValidation.HelloWorld\TFSValidation.HelloWorld.sln'

BR Sebastian

DanielHWe commented 5 years ago

Added feature to 1.4.2 Snapshot 1, please test.

dafunkjoker commented 5 years ago

Hi Daniel,

unfortunately the bux is not fixed. In my test I got a path which was too long again. I also noticed that you added another replace() instead of modifying the second replace() in FxCopSensor.java -> the current code looks like this: workDirPath = workDirPath.replace(projectKey.replace(":", ""), projectKey.substring(projectKey.indexOf(':'))).replace(":[^\]", "").replace(":", ""); instead of workDirPath = workDirPath.replace(projectKey.replace(":", ""), projectKey.substring(projectKey.indexOf(':'))).replace(":[^\]", "");

Was it by accident? I can reproduce/fix the error via compilejava I attached copy-paste-code for reproducing as txt attachments

BR Sebastian

DanielHWe commented 5 years ago

No this was done to hold the unit tests working. because on some projects I know the projeckt key simply end with a ':' what sould be cur of, what is not done by .replace(":[^]", ""); also replace(":[^]", "").replace(":", ""); is not cutting less from path. than only replace(":[^]", "");

dafunkjoker commented 5 years ago

Hm, no attachment --> Example code for the bugfix

//*******************************************************************
// Welcome to CompileJava!
//*******************************************************************

// one class needs to have a main() method
public class HelloWorld
{
  // arguments are passed using the text field below this editor
  public static void main(String[] args)
  {
    // OtherClass myObject = new OtherClass("Hello World!");
    // System.out.print(myObject);
    trimWorkdir();
  }

  public static String trimWorkdir() {
            String projectKey  = "SITA.Spy-Xpta.Sita.Spy.Plugins:SITA.Spy-Xpta.Sita.Spy.Plugins:E36495D1-5808-4758-AA0D-374E6925770E";
            String workDirPath = "D:\\Builds\\16\\70\\Sources\\Xpta.Sita.Spy.Plugins\\.sonarqube\\out\\.sonar\\SITA.Spy-Xpta.Sita.Spy.Plugins_SITA.Spy-Xpta.Sita.Spy.Plugins_E36495D1-5808-4758-AA0D-374E6925770E";
            if (projectKey!=null && !projectKey.isEmpty() && projectKey.indexOf(':') >= 0/*&& projectKey.matches("[a-zA-Z0-9_-]:[a-zA-Z0-9_-]:[a-zA-Z0-9_-]")*/){

                        workDirPath = workDirPath.replace(projectKey.replace(":", ""), projectKey.substring(projectKey.indexOf(':'))).replace(":[^\\]", "");

                        System.out.print("\nProject key: " + projectKey);
                        System.out.print("\nWork Dir: " + workDirPath);
            }
            return workDirPath;
  }
}
dafunkjoker commented 5 years ago

Hi Daniel, Sorry I did not fully understand your response but as far as I understood, there is a problem if projectKey ends with ":" --> so maybe some if/else might support both (ofc this would be the quick an dirty fix)? BR Sebastian

DanielHWe commented 5 years ago

Ok, I see replace(":", "") also removed the : in path, so I changed to replaceAll("^:", "") to only remove leading :. See Snapshot 2

dafunkjoker commented 5 years ago

perfect, now it is working --> Thanks for the quick help!