community-rust icon indicating copy to clipboard operation
community-rust copied to clipboard

Cannot resolve path to source files when project is in a subdirectory

Open schaffenrath opened this issue 1 year ago • 5 comments

Problem

When the Rust project is located in a subdirectory, the sonar-scanner is unable to resolve the path, resulting in the warning: WARN Failed to resolve 1 file path(s) in Clippy report. No issues imported related to file(s): src/main.rs

Related

The PR 66 introduced the FileAdjustor, which takes the path from the message:spans:file_name property of the generated clippy report. The file name seems to be relative to the project root.

Possible solution

One could consider the target:src_path property from the report as well, to find the source files.

An example report that shows the properties:
{
    "reason": "compiler-message",
    "package_id": "path+file:///builds/user/ci-test/rust/quality_test#0.1.0",
    "manifest_path": "/builds/user/ci-test/rust/quality_test/Cargo.toml",
    "target": {
        "kind": [
            "bin"
        ],
        "crate_types": [
            "bin"
        ],
        "name": "quality_test",
        "src_path": "/builds/user/ci-test/rust/quality_test/src/main.rs",
        "edition": "2021",
        "doc": true,
        "doctest": false,
        "test": true
    },
    "message": {
        "rendered": "warning: unneeded `return` statement\n --> src/main.rs:2:5\n  |\n2 |     return &val[12..50];\n  |     ^^^^^^^^^^^^^^^^^^^\n  |\n  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return\n  = note: `#[warn(clippy::needless_return)]` on by default\nhelp: remove `return`\n  |\n2 -     return &val[12..50];\n2 +     &val[12..50]\n  |\n\n",
        "$message_type": "diagnostic",
        "children": [
            {
                "children": [],
                "code": null,
                "level": "help",
                "message": "for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return",
                "rendered": null,
                "spans": []
            },
            {
                "children": [],
                "code": null,
                "level": "note",
                "message": "`#[warn(clippy::needless_return)]` on by default",
                "rendered": null,
                "spans": []
            },
            {
                "children": [],
                "code": null,
                "level": "help",
                "message": "remove `return`",
                "rendered": null,
                "spans": [
                    {
                        "byte_end": 53,
                        "byte_start": 34,
                        "column_end": 24,
                        "column_start": 5,
                        "expansion": null,
                        "file_name": "src/main.rs",
                        "is_primary": true,
                        "label": null,
                        "line_end": 2,
                        "line_start": 2,
                        "suggested_replacement": "&val[12..50]",
                        "suggestion_applicability": "MachineApplicable",
                        "text": [
                            {
                                "highlight_end": 24,
                                "highlight_start": 5,
                                "text": "    return &val[12..50];"
                            }
                        ]
                    },
                    {
                        "byte_end": 54,
                        "byte_start": 53,
                        "column_end": 25,
                        "column_start": 24,
                        "expansion": null,
                        "file_name": "src/main.rs",
                        "is_primary": true,
                        "label": null,
                        "line_end": 2,
                        "line_start": 2,
                        "suggested_replacement": "",
                        "suggestion_applicability": "MachineApplicable",
                        "text": [
                            {
                                "highlight_end": 25,
                                "highlight_start": 24,
                                "text": "    return &val[12..50];"
                            }
                        ]
                    }
                ]
            }
        ],
        "code": {
            "code": "clippy::needless_return",
            "explanation": null
        },
        "level": "warning",
        "message": "unneeded `return` statement",
        "spans": [
            {
                "byte_end": 53,
                "byte_start": 34,
                "column_end": 24,
                "column_start": 5,
                "expansion": null,
                "file_name": "src/main.rs",
                "is_primary": true,
                "label": null,
                "line_end": 2,
                "line_start": 2,
                "suggested_replacement": null,
                "suggestion_applicability": null,
                "text": [
                    {
                        "highlight_end": 24,
                        "highlight_start": 5,
                        "text": "    return &val[12..50];"
                    }
                ]
            }
        ]
    }
}

schaffenrath avatar Oct 01 '24 09:10 schaffenrath

Hi! Right, I think target:src_path will allow to drop the FileAdjustor as it is using the absolute path

I prepared the new version with fix, I am still testing it but you can check if it fixes your issue https://github.com/C4tWithShell/community-rust/releases/tag/v0.2.5

C4tWithShell avatar Oct 19 '24 01:10 C4tWithShell

Hi. Thanks for the quick fix! I would've loved to try it myself. Unfortunately, I wasn't able to start SonarQube:10.7-community with the plugin, since the Sonar Plugin API version requirement seems to be not fulfilled, according to the error message:

2024.10.21 07:23:54 WARN  web[][o.s.c.a.AnnotationConfigApplicationContext] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdk.internal.loader.ClassLoaders$AppClassLoader@71529963-org.sonar.server.plugins.ServerPluginManager': Initialization of bean failed; nested exception is Plugin Rust language analyzer [communityrust] requires at least Sonar Plugin API version 10.12.0.2522 (current: 10.11.0.2468)
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdk.internal.loader.ClassLoaders$AppClassLoader@71529963-org.sonar.server.plugins.ServerPluginManager': Initialization of bean failed; nested exception is Plugin Rust language analyzer [communityrust] requires at least Sonar Plugin API version 10.12.0.2522 (current: 10.11.0.2468)

Which version of SonarQube are you using and for which version is the 0.2.5 release intended?

schaffenrath avatar Oct 21 '24 07:10 schaffenrath

Sorry, my bad I uploaded wrong file, 10.12.0.2522 is the newest api version, it is not supported yet I replaced the file with the correct version 10.11.0.2468

I will work on workflow for plugin to avoid this problem in future

C4tWithShell avatar Oct 21 '24 15:10 C4tWithShell

Thanks, the version incompatibility seems to be fixed. A new error arose during the start of SonarQube though. I now get the issue that a rule name seems to be too long:

2024.10.22 08:03:37 WARN  web[][o.s.c.a.AnnotationConfigApplicationContext] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdk.internal.loader.ClassLoaders$AppClassLoader@71529963-org.sonar.server.rule.registration.RulesRegistrant': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Rule name is too long: Checks for casts between numeric types that may truncate large values. This is expected behavior, so the cast is `Allow` by default. It suggests user either explicitly ignore the lint, or use `try_from()` and handle the truncation, default, or panic explicitly.
2024.10.22 08:03:37 ERROR web[][o.s.s.p.Platform] Background initialization failed. Stopping SonarQube
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdk.internal.loader.ClassLoaders$AppClassLoader@71529963-org.sonar.server.rule.registration.RulesRegistrant': Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: Rule name is too long: Checks for casts between numeric types that may truncate large values. This is expected behavior, so the cast is `Allow` by default. It suggests user either explicitly ignore the lint, or use `try_from()` and handle the truncation, default, or panic explicitly.

Is there a way to extend this length limitation?

schaffenrath avatar Oct 22 '24 08:10 schaffenrath

I see, I was able to reproduce on clean deploy. I will make adjustments

I will test it and will reach you after that

UPD: Done, there should not be any errors @schaffenrath, can you check if it solved issue for you?

C4tWithShell avatar Oct 22 '24 14:10 C4tWithShell

Just tested it and works great. Clippy report for a Rust project in a sub-folder is now also processed correctly. Thanks!

schaffenrath avatar Oct 28 '24 14:10 schaffenrath

I believe this now reports on the wrong file in certain cases, causing the import to fail with an invalid location:

java.lang.IllegalArgumentException: 68 is not a valid line offset for pointer. File src/lib.rs has 36 character(s) at line 7

For example, the edited line below fails because it now incorrectly reports on lib.rs whereas the lint comes from ffi.rs.

{
  "reason": "compiler-message",
  "target": {
    "src_path": "/Users/bas/bzhoek/id3rs/src/lib.rs"
  },
  "message": {
    "rendered": "warning: unsafe function's docs are missing a `# Safety` section\n  --> src/ffi.rs:42:1\n   |\n42 | pub unsafe extern \"C\" fn id3_free(ptr: *mut ID3rs) {\n   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n   |\n   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_safety_doc\n\n",
    "$message_type": "diagnostic",
    "level": "warning",
    "message": "unsafe function's docs are missing a `# Safety` section",
    "spans": [
      {
        "byte_end": 1292,
        "byte_start": 1242,
        "column_end": 51,
        "column_start": 1,
        "file_name": "src/ffi.rs",
        "text": [
          {
            "highlight_end": 51,
            "highlight_start": 1,
            "text": "pub unsafe extern \"C\" fn id3_free(ptr: *mut ID3rs) {"
          }
        ]
      }
    ]
  }
}

bzhoek avatar Nov 25 '24 15:11 bzhoek