Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Roots (and directories) containing multi-file launcher sources should respond to ClassPath queries. #7733

Conversation

lahodaj
Copy link
Contributor

@lahodaj lahodaj commented Sep 5, 2024

The multi-file source launcher support has been tweaked under:
#7509

to only respond to files that are source launcher files. But, that means the queries no longer respond when asked about directories, especially the root. But, the Java indexing, and other features, need the ClassPath to work also on the root directories. As a consequence, the multi-file source launcher is now broken.

The proposal herein is to respond not only for source launcher files as before, but also for enclosing directories up to the package hierarchy root. This will happen only if the root has already been recognized, so it should not alter cases where source file launcher is not used.

@lahodaj lahodaj added Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) LSP [ci] enable Language Server Protocol tests VSCode Extension [ci] enable VSCode Extension tests labels Sep 5, 2024
@lahodaj lahodaj added this to the NB24 milestone Sep 5, 2024
@mbien
Copy link
Member

mbien commented Sep 5, 2024

I tested this with the following setup:

/tmp/sources/Main.java

public class Main {
    public static void main(String args[]) {
        System.out.println("hello");
        System.out.println(Runtime.version());
        dev.test.Util.foo();
    }
}

/tmp/sources/dev/test/Util.java

package dev.test;
public class Util {
    public static void foo() {
        System.out.println("foo called");
    }
}

and added /tmp to the favorites window.

this caused:

WARNING [org.netbeans.modules.java.source.tasklist.IncorrectErrorBadges]: Incorrect error badges detected, file=/tmp/sources/Main.java.
WARNING [org.netbeans.modules.java.source.tasklist.IncorrectErrorBadges]: Not PathRegistry controlled root: /tmp/sources@5c553aa0:17939bbf

although the error badges were sometimes not visible until I restarted NetBeans.

@matthiasblaesing
Copy link
Contributor

The proposal herein is to respond not only for source launcher files as before, but also for enclosing directories up to the package hierarchy root. This will happen only if the root has already been recognized, so it should not alter cases where source file launcher is not used.

I checked it and I think it does not work. Here is what I did:

  1. Build NetBeans from this branch
  2. Place breakpoints in MultiSourceRootProvider.java, lines 110 and 164
  3. Remove nbbuild/testuserdir
  4. Run "Debug" from the "Java File Launcher" project
  5. Enable "Java SE" in the Plugin manager
  6. Ensure there is a "test.java" file in your home directory
  7. Open "Favorites" tab
  8. Expand the "Home" node

The breakpoint in line 164 is hit with the home directory as root. The letting the IDE continue running the breakpoint in line 110 is hit, reporting true for my home directory.

I did not use the source file launcher, yet the file and the directory are now recognized.

What is more I see similar reports as @mbien. I opened java files from a JDK checkout and see:

INFO [org.netbeans.modules.java.hints.WrongPackageSuggestion]: source cp is either null or does not contain the compiled source cp=
INFO [org.netbeans.modules.java.hints.WrongPackageSuggestion]: source cp is either null or does not contain the compiled source cp=
WARNING [org.netbeans.modules.java.source.tasklist.IncorrectErrorBadges]: Incorrect error badges detected, file=/home/matthias/src/jdk/src/java.desktop/unix/classes/sun/font/X11GB18030_1.java.
WARNING [org.netbeans.modules.java.source.tasklist.IncorrectErrorBadges]: The file is not on its own source classpath, ignoring.

@lahodaj lahodaj force-pushed the multi-source-launcher-classpath-for-registered-roots branch from 3c0b8ba to ed150ca Compare September 6, 2024 18:47
@lahodaj
Copy link
Contributor Author

lahodaj commented Sep 6, 2024

I am not quite sure what exactly is (or better said was) the problem with the ClassPathProvider returning the ClassPath for the root or directory, except that debugger stopped at some line. The behavior before #7509 was I believe to return the ClassPath, and neither that PR nor the associated bug provide any clues what was the problem with that.

Anyway, I've updated this patch to only return the ClassPath when the branding is set to register the roots. I hereby also apologize I didn't quite realize the full impact of #7509, that was my mistake.

Regarding the:

WARNING [org.netbeans.modules.java.source.tasklist.IncorrectErrorBadges]: Incorrect error badges detected, file=/home/jlahoda/test-projects/multi-file-unnamed/test/TryGson.java.
WARNING [org.netbeans.modules.java.source.tasklist.IncorrectErrorBadges]: Not PathRegistry controlled root: /home/jlahoda/test-projects/multi-file-unnamed@e38edfdb:28505e6d

I am getting that even when running on master. Quite unsurprisingly: the file reports an error in the editor, but there are no errors in the index (since there's no index). IncorrectErrorBadges will try to force re-index, but finds out the root is not registered (and therefore not indexed), and prints a warning. So, the only options to solve this warning is either to register the root, or disable the warning. And since we are not going to do the former, feel free to do the later. I just don't think it is related to fixing this regression.

@matthiasblaesing
Copy link
Contributor

Whether or not my finding is really a problem I did not investigate, but the hits on the breakpoints invalidated the claim that it is save because it is only relevant if multi-file launcher is used.

Wouldn't it make sense to use the same approach as CPPLite? I.e. ask the user to create a "virtual" project? That way we could get rid of the hacks and use the "normal" NetBeans architecture.

I'll be away from keyboard for the next week, I won't stay in the way of this, but it would be nice if new features (the problem exists since the introduction of support for source file launchers) would not break the IDE.

@mbien
Copy link
Member

mbien commented Sep 6, 2024

Wouldn't it make sense to use the same approach as CPPLite? I.e. ask the user to create a "virtual" project? That way we could get rid of the hacks and use the "normal" NetBeans architecture.

could the main class not simply have a check box in the file properties to opt into a project-less compilation/execution model?

I think at some point there must be some way to specify the classpath anyway, otherwise "java scripts" like https://github.com/apache/netbeans/blob/master/.github/scripts/BinariesListUpdates.java will never work properly in NB (since the jar dependencies are not necessarily in the same folder).

The file properties already have JVM/program args and a JDK selector #7605

@lahodaj
Copy link
Contributor Author

lahodaj commented Sep 6, 2024

Whether or not my finding is really a problem I did not investigate, but the hits on the breakpoints invalidated the claim that it is save because it is only relevant if multi-file launcher is used.

What I meant was that the root must first be discovered through a file. But I admit I wrote that ambiguously.

Wouldn't it make sense to use the same approach as CPPLite? I.e. ask the user to create a "virtual" project? That way we could get rid of the hacks and use the "normal" NetBeans architecture.

I don't recall any positive feedback on CPPLite (but plenty, plenty of negative). So no, forcing the users to create manually a project does not sound like a good idea, unless there are piles of positive feedback on CPPLite hiding somewhere.

When I was doing this, I tried to create a project automatically, but a) it was extremely complicated; b) it wouldn't solve anything if the project was created automatically anyway.

I'll be away from keyboard for the next week, I won't stay in the way of this, but it would be nice if new features (the problem exists since the introduction of support for source file launchers) would not break the IDE.

While I am trying to not break things, sometimes I do, I am afraid. The only way how one can not introduce (new) breakages is to do nothing (and live with the existing breakages).

@matthiasblaesing
Copy link
Contributor

Wouldn't it make sense to use the same approach as CPPLite? I.e. ask the user to create a "virtual" project? That way we could get rid of the hacks and use the "normal" NetBeans architecture.

I don't recall any positive feedback on CPPLite (but plenty, plenty of negative). So no, forcing the users to create manually a project does not sound like a good idea, unless there are piles of positive feedback on CPPLite hiding somewhere.

From my POV CPPLite suffers from the problem, that you need to configure much more, than just the root of the project. You'll need the metadata file, that described compilation. I tried once to get small step improvements by allowing to use the language server without additional configuration, which was rejected.

The same does not apply to this kind of java project.

@mbien mbien added the ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) label Sep 7, 2024
@lahodaj
Copy link
Contributor Author

lahodaj commented Sep 9, 2024

could the main class not simply have a check box in the file properties to opt into a project-less compilation/execution model?

Hm, that should be doable, thanks!

@lahodaj
Copy link
Contributor Author

lahodaj commented Sep 10, 2024

Here:
ef4a2e5
I tried to add a checkbox to enable/disable the registration of the root.

Copy link
Member

@mbien mbien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

works really well!

error badges went away right after I checked the box (same test from #7733 (comment)), the property will also only show on source files and only outside of projects.

There are probably some UI tweaks we could do later, e.g grayed out checkbox to indicate that a file is already in a source root - but I think this has lower priority. (maybe even a visual indicator on the icon?)

The opt-in action is also an excellent place to add context sensitive warnings for situations when the user is about to index his/her entire home folder ;)

Copy link
Contributor

@matthiasblaesing matthiasblaesing left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general this is looks like a good to me.

I don't think the following is in scope for this change, so I'll describe it, maybe one of the readers has an idea:

  • I ran ant tryme with a clean testuserdir
  • in my tmp folder I placed a file scratch.java
  • I selected a JDK in the properties of the file

After that I observe indexing starting. I traced it into the APTUtils, which are queried by the JavacParser which is queried by the JavaNode. As a side effect indexing is invoked on attribute changes.

@lahodaj lahodaj force-pushed the multi-source-launcher-classpath-for-registered-roots branch from ef4a2e5 to 38cf35a Compare October 1, 2024 05:02
@lahodaj
Copy link
Contributor Author

lahodaj commented Oct 1, 2024

Unless there are objections, I'll integrate in the next day or so.

@lahodaj lahodaj merged commit 6cc84c8 into apache:master Oct 3, 2024
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) LSP [ci] enable Language Server Protocol tests VSCode Extension [ci] enable VSCode Extension tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants