Skip to content

feat: Util for opening a file in the IDE running the application - #15605

Merged
Artur- merged 28 commits into
masterfrom
ide-open
Jan 12, 2023
Merged

Artur- merged 28 commits into
masterfrom
ide-open

Conversation

@Artur-

@Artur- Artur- commented Jan 2, 2023 •

Copy link
Copy Markdown
Member

Supports Eclipse, IntelliJ and VS Code

@github-actions

github-actions Bot commented Jan 2, 2023 •

Copy link
Copy Markdown
Contributor

Test Results

   940 files  +  1     940 suites  +1   58m 36s ⏱️ -29s
5 882 tests +11  5 847 ✔️ +11  35 💤 ±0  0 ❌ ±0 
6 133 runs  +11  6 091 ✔️ +11  42 💤 ±0  0 ❌ ±0 

Results for commit 1b14bc8. ± Comparison against base commit d12939d.

♻️ This comment has been updated with latest results.

@Artur-
Artur- marked this pull request as ready for review January 4, 2023 17:03
@Artur-

Artur- commented Jan 4, 2023

Copy link
Copy Markdown
Member Author

Can't think of how this could be auto tested

@vaadin-bot vaadin-bot added +0.0.1 and removed +1.0.0 labels Jan 4, 2023
@czp13

czp13 commented Jan 5, 2023

Copy link
Copy Markdown
Contributor

Added some minor comments. 🙇 Thank you.

@czp13

czp13 commented Jan 5, 2023 •

Copy link
Copy Markdown
Contributor

Can't think of how this could be auto tested
Maybe with:

  • mocked unit tests, to see if the log formatting
  • and if/else are always running properly in every case, and we are in a good execution branch,
  • and so make sure to not break with any further changes,

and do not have many test cases probably with mc/dc coverage testing, or at least some smoke tests...

p.s: thanks for considering other comments
p.s.2: the only problem is that we would need to moke static methods e.g. OSUtils.isMac, but Mockito 3.4 is capable of doing that:
https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#48

To moke directly java API like ProcessHandle not sure how it is possible, maybe need to extract to a separate class and mock that class rather... :/

And in that case, it is just maybe too much hassle for unit tests, it's a decision.

@Artur-

Artur- commented Jan 6, 2023

Copy link
Copy Markdown
Member Author

I think the interesting parts to tests would be:

  1. Is the correct IDE detected. This is dependent on the platform and the IDE and requires running a Java snippet from inside an IDE on a given platform. Many combinations and hard to automate so I would not do that
  2. Assuming the correct IDE is detected, is the file opened properly at the correct line. This is 98% IDE functionality that we just need to assume that works and auto testing the rest is as hard as case 1.

-> Let's not spend time on auto testing this but let's react when/if somebody reports a problem

if (javaAgent.contains(":")) {
String binFolder = javaAgent.split(":")[1];
Optional<File> bin = Stream.of("idea", "idea.sh", "idea.bat")
.map(binName -> new File(binFolder, binName))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

7% of developers fix this issue

PATH_TRAVERSAL_IN: This API (java/io/File.(Ljava/lang/String;Ljava/lang/String;)V) reads a file whose location might be specified by user input


ℹ️ Learn about @sonatype-lift commands

You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.

Command Usage
@sonatype-lift ignore Leave out the above finding from this PR
@sonatype-lift ignoreall Leave out all the existing findings from this PR
@sonatype-lift exclude <file|issue|path|tool> Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file

Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.


Was this a good recommendation?
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]

@sonatype-lift

sonatype-lift Bot commented Jan 11, 2023

Copy link
Copy Markdown

🛠 Lift Auto-fix

Some of the Lift findings in this PR can be automatically fixed. You can download and apply these changes in your local project directory of your branch to review the suggestions before committing.1

# Download the patch
curl https://lift.sonatype.com/api/patch/github.com/vaadin/flow/15605.diff -o lift-autofixes.diff

# Apply the patch with git
git apply lift-autofixes.diff

# Review the changes
git diff

Want it all in a single command? Open a terminal in your project's directory and copy and paste the following command:

curl https://lift.sonatype.com/api/patch/github.com/vaadin/flow/15605.diff | git apply

Once you're satisfied commit and push your changes in your project.

Footnotes

  1. You can preview the patch by opening the patch URL in the browser. ↩

@Artur-

Artur- commented Jan 11, 2023

Copy link
Copy Markdown
Member Author

Now there is at least one test for each Eclipse, IDEA, VS Code+ Linux, Mac, Windows combination. I am pretty sure there will be some edge cases though but I don't think we can find them upfront

@Artur-

Artur- commented Jan 12, 2023

Copy link
Copy Markdown
Member Author

Any more considerations @mcollovati ?

+ lineNumber);
} else {
try {
run(cmd, "--line", lineNumber + "", absolutePath);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This does actually not work on my environment (IDEA on Linux)
It starts a jvm, but not IDEA

/home/marco/local/tools/idea/2021.3/jbr/bin/java --line 12 /home/marco/tmp/my-app-v24/src/main/java/com/example/application/views/helloworld/HelloWorldView.java

Using processInfo.commandLine() may work, but only if the command is short enough not to be truncated by the native call that gets process info

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Coding is hard yo.. Maybe now that it actually uses the binary it detects?

Comment on lines +201 to +202
int agentPos = commandAndArguments.toLowerCase(Locale.ENGLISH)
.indexOf("-javaagent:");

@mcollovati mcollovati Jan 12, 2023 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

-javaagent seems to be present only on the command line that runs the application, not in the one that launches the IDE

/home/marco/local/tools/idea/2021.3/jbr/bin/java -classpath /home/marco/local/tools/idea/2021.3/lib/util.jar:/home/marco/local/tools/idea/2021.3/lib/app.jar:/home/marco/local/tools/idea/2021.3/lib/3rd-party-rt.jar:/home/marco/local/tools/idea/2021.3/lib/platform-statistics-devkit.jar:/home/marco/local/tools/idea/2021.3/lib/jps-model.jar:/home/marco/local/tools/idea/2021.3/lib/stats.jar:/home/marco/local/tools/idea/2021.3/lib/protobuf.jar:/home/marco/local/tools/idea/2021.3/lib/external-system-rt.jar:/home/marco/local/tools/idea/2021.3/lib/forms_rt.jar:/home/marco/local/tools/idea/2021.3/lib/intellij-test-discovery.jar:/home/marco/local/tools/idea/2021.3/lib/annotations.jar:/home/marco/local/tools/idea/2021.3/lib/groovy.jar:/home/marco/local/tools/idea/2021.3/lib/3rd-party-native.jar:/home/marco/local/tools/idea/2021.3/lib/annotations-java5.jar:/home/marco/local/tools/idea/2021.3/lib/byte-buddy-agent.jar:/home/marco/local/tools/idea/2021.3/lib/error-prone-annotations.jar:/home/marco/local/tools/idea/2021.3/lib/externalProcess-rt.jar:/home/marco/local/tools/idea/2021.3/lib/idea_rt.jar:/home/marco/local/tools/idea/2021.3/lib/intellij-coverage-agent-1.0.682.jar:/home/marco/local/tools/idea/2021.3/lib/jsch-agent.jar:/home/marco/local/tools/idea/2021.3/lib/jsp-base.jar:/home/marco/local/tools/idea/2021.3/lib/junit.jar:/home/marco/local/tools/idea/2021.3/lib/junit4.jar:/home/marco/local/tools/idea/2021.3/lib/kotlin-script-runtime.jar:/home/marco/local/tools/idea/2021.3/lib/platform-duplicates-analysis.jar:/home/marco/local/tools/idea/2021.3/lib/platform-objectSerializer-annotations.jar:/home/marco/local/tools/idea/2021.3/lib/platform-structuralSearch.jar:/home/marco/local/tools/idea/2021.3/lib/rd.jar:/home/marco/local/tools/idea/2021.3/lib/tools-testsBootstrap.jar:/home/marco/local/tools/idea/2021.3/lib/util_rt.jar:/home/marco/local/tools/idea/2021.3/lib/xml-dom-impl.jar:/home/marco/local/tools/idea/2021.3/lib/xml-dom.jar:/home/marco/local/tools/idea/2021.3/lib/ant/lib/ant.jar -Xms128m -Xmx750m -XX:ReservedCodeCacheSize=512m -XX:+UseG1GC -XX:SoftRefLRUPolicyMSPerMB=50 -XX:CICompilerCount=2 -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -XX:+IgnoreUnrecognizedVMOptions -XX:CompileCommand=exclude,com/intellij/openapi/vfs/impl/FilePartNodeRoot,trieDescend -ea -Dsun.io.useCanonCaches=false -Dsun.java2d.metal=true -Djbr.catch.SIGABRT=true -Djdk.http.auth.tunneling.disabledSchemes="" -Djdk.attach.allowAttachSelf=true -Djdk.module.illegalAccess.silent=true -Dkotlinx.coroutines.debug=off -Dsun.tools.attach.tmp.only=true -Xmx1985m -XX:ErrorFile=/home/marco/java_error_in_idea_%p.log -XX:HeapDumpPath=/home/marco/java_error_in_idea_.hprof -Djb.vmOptionsFile=/home/marco/.config/JetBrains/IdeaIC2022.3/idea64.vmoptions -Djava.system.class.loader=com.intellij.util.lang.PathClassLoader -Didea.vendor.name=JetBrains -Didea.paths.selector=IdeaIC2022.3 -Djna.boot.library.path=/home/marco/local/tools/idea/2021.3/lib/jna/amd64 -Dpty4j.preferred.native.folder=/home/marco/local/tools/idea/2021.3/lib/pty4j -Djna.nosys=true -Djna.nounpack=true -Didea.platform.prefix=Idea -Dsplash=true --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.ref=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.nio=ALL-UNNAMED --add-opens=java.base/java.nio.charset=ALL-UNNAMED --add-opens=java.base/java.text=ALL-UNNAMED --add-opens=java.base/java.time=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED --add-opens=java.base/jdk.internal.vm=ALL-UNNAMED --add-opens=java.base/sun.nio.ch=ALL-UNNAMED --add-opens=java.base/sun.nio.fs=ALL-UNNAMED --add-opens=java.base/sun.security.ssl=ALL-UNNAMED --add-opens=java.base/sun.security.util=ALL-UNNAMED --add-opens=java.desktop/com.sun.java.swing.plaf.gtk=ALL-UNNAMED --add-opens=java.desktop/java.awt=ALL-UNNAMED --add-opens=java.desktop/java.awt.dnd.peer=ALL

What about checking for -Didea.vendor.name?

@Artur- Artur- Jan 12, 2023 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes but that should not matter if we can get the idea location from the last part of the javaagent parameter. Does it still not work?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What about checking for lib/idea_rt.jar and then search the idea script in ../bin?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yes but that should not matter if we can get the idea location from the last part of the javaagent parameter. Does it still not work?

On the inspected command line, javaagent is not present at all

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Are you saying there is something wrong with the ideaOnLinuxDetected test then because that one should be based exactly on your environment...

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I would go for idea_rt.jar
For example running on another project with no evident differences in Run configuration I get this for the javaagent param

-javaagent:/home/marco/local/tools/idea/2021.3/plugins/java/lib/rt/debugger-agent.jar=file:/tmp/capture.props

and the detection fails because of the file:/tmp/... part

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Changed once again. Does it work now? :)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can confirm it now works on my laptop! 🥳

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

OMG. I am sure you will figure out how to break it tomorrow though

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I can perhaps try to install Eclipse... 😆

@sonarqubecloud

Copy link
Copy Markdown

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

"bin");
Optional<File> bin = Stream
.of("idea", "idea.sh", "idea.bat")
.map(binName -> new File(binFolder, binName))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

7% of developers fix this issue

PATH_TRAVERSAL_IN: This API (java/io/File.(Ljava/io/File;Ljava/lang/String;)V) reads a file whose location might be specified by user input


ℹ️ Learn about @sonatype-lift commands

You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.

Command Usage
@sonatype-lift ignore Leave out the above finding from this PR
@sonatype-lift ignoreall Leave out all the existing findings from this PR
@sonatype-lift exclude <file|issue|path|tool> Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file

Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.


Was this a good recommendation?
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]

.replaceFirst(".*[:;]([^:;]*)(idea_rt.jar).*", "$1$2");
if (!replaced.equals(commandAndArguments)) {
System.out.println(replaced);
File binFolder = new File(

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

7% of developers fix this issue

PATH_TRAVERSAL_IN: This API (java/io/File.(Ljava/lang/String;)V) reads a file whose location might be specified by user input


ℹ️ Learn about @sonatype-lift commands

You can reply with the following commands. For example, reply with @sonatype-lift ignoreall to leave out all findings.

Command Usage
@sonatype-lift ignore Leave out the above finding from this PR
@sonatype-lift ignoreall Leave out all the existing findings from this PR
@sonatype-lift exclude <file|issue|path|tool> Exclude specified file|issue|path|tool from Lift findings by updating your config.toml file

Note: When talking to LiftBot, you need to refresh the page to see its response.
Click here to add LiftBot to another repo.


Was this a good recommendation?
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]

@vaadin-bot

Copy link
Copy Markdown
Collaborator

This ticket/PR has been released with Vaadin 24.0.0.alpha9 and is also targeting the upcoming stable 24.0.0 version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants