Debug Kotlin Symbol Processing (KSP) - kotlin

How to debug KSP using Idea and Gradle?
Running build task from IDE in debug mode does not attach IDE to the process.
Is there any command-line way to make the processor wait until IDE attach to thte process?
(I'm using KSP 1.5.31-1.0.0)

./gradlew :sample:build --no-daemon -Dorg.gradle.debug=true -Dkotlin.compiler.execution.strategy=in-process
If debugging without daemon is too slow (it has to start jvm everytime), ./gradlew -Dkotlin.daemon.jvm.options="-Xdebug,-Xrunjdwp:transport=dt_socket\,address=8765\,server=y\,suspend=n" ... and attach to the KotlinCompileDaemon process in the debugger.
More info here: https://github.com/google/ksp/issues/31

Related

Start IntelliJ run-configuration from command line

I work on a Kotlin-based cloud microservice. From time to time I need to run it in the IntelliJ debugger, but we often accidentally break the "local debug flow". There is no pipeline that checks if the IntelliJ run-configuration and the somewhat complex set of cloud-simulating proxies and mock-servers are in sync with the code base.
I would like to write a script that helps me check if the local debug flow works. The script should do the following:
build the project
start up the "mock cloud"
start the service locally using an IntelliJ run-configuration
run a Postman-collection against the locally running service
I need help with step 3 (and possibly step 1): How can I start the IntelliJ run-configuration via command line? I am using IntelliJ IDEA 2021.2 and MacOS 12.3.1.
There is no built-in way to start a Run Configuration from the command line. Instead, you should execute java/kotlin binary with classpath etc.
For example, for "Application" configuration it is needed to run the command like that:

What magic happens when IntelliJ compiles my Kotlin project?

I currently know that kotlinc hello.kt -include-runtime -d hello.jar compiles my hello world program written in kotlin. Furthermore I am able to execute my program via java -jar hello.jar.
Setting up a Kotlin "Hello world!" console application example in IntelliJ I may choose between different Build Systems (Maven, Gradle, IntelliJ).
I would like to know what happens in the background using the IntelliJ Build System for example?
It seems that there is an ant script running considering the Build Output:
Executing pre-compile tasks…
Loading Ant configuration...
Running Ant tasks...
Running 'before' tasks
Checking sources
Kotlin: connecting to daemon
Kotlin: compiling [firstKotlinApp]
Kotlin: kotlinc-jvm 1.6.10-release-923 (JRE 17.0.1+12-39)
Kotlin: performing incremental compilation analysis
Checking dependencies… [firstKotlinApp]
Dependency analysis found 0 affected files
Updating dependency information… [firstKotlinApp]
Running 'after' tasks
Finished, saving caches…
Executing post-compile tasks…
Loading Ant configuration...
Running Ant tasks...
Synchronizing output directories…
04.01.22, 07:59 - Build completed successfully in 6 sec, 686 ms
Can someone help me out and direct me to the right place to look?
I already tried to find information with several search engines.

Upgrading android gradle plugin from 4.1.3 to 7.0.4 causes gradle process crashes when running lintDebug

I am trying to upgrade my Android Gradle plugin from 4.1.3 to 7.0.4 also lint version to 30.0.4
I am using Gradle version 7.3.3
My Gradle daemon crashes now while running lint target on CI process with error
Gradle build daemon disappeared unexpectedly (it may have been killed or may have crashed)
Here is my gradel.properties file.
I tried various options android.experimental.runLintInProcess, android.experimental.lint.heapSize
discussed in this issue, without success.
https://issuetracker.google.com/issues/178631052
Has anyone seen this issue
org.gradle.jvmargs=-Xmx8g -Xms512M -XX:MaxPermSize=1024m -Dkotlin.daemon.jvm.options="-Xmx8g" -XX:+HeapDumpOnOutOfMemoryError -XX:+UseParallelGC -XX:MaxMetaspaceSize=1g -XX:+UnlockExperimentalVMOptions -Dfile.encoding=UTF-8
# When set to true, Gradle will try to reuse outputs from previous builds
org.gradle.caching=true
# Enables new incubating mode that makes Gradle selective when configuring projects
org.gradle.configureondemand=true
# When configured, Gradle will run in incubating parallel mode
org.gradle.parallel=true
# When configured, Gradle will use a maximum of the given number of workers
# 4 is good for a max for CI and running junit tests in parallel
org.gradle.workers.max=4
# Allowing to avoid Kotlin recompilation when the changes in Java do not affect Kotlin code
kotlin.incremental.usePreciseJavaTracking=true
android.useAndroidX=true
android.enableJetifier=true
android.uniquePackageNames=true
kapt.use.worker.api=true
org.gradle.unsafe.watch-fs=true
#https://issuetracker.google.com/issues/178631052
#android.experimental.runLintInProcess=false
#android.experimental.lint.heapSize=1G
It's OOM issue with Gradle Daemon. Setting these 6 bits in application.properties should work fine.
org.gradle.jvmargs=-Xmx8g
kotlin.daemon.jvmargs=-Xmx8g
org.gradle.caching=true
org.gradle.parallel=true
org.gradle.daemon=true
android.enableJetifier=true
The AGP 7.0.x reqire Gradle version 7.0.2
see https://developer.android.com/studio/releases/gradle-plugin#compatibility

Debugging SBT project with Play in IntelliJ IDEA

I have a SBT project
in this project i have a sub play project and other projects
example from my build file :
lazy val subProj1 = Project(id = "sub-proj-1", base = file("sub1"))
.settings(...)...
lazy val subProjPlay = play.Project("play-proj", 1.0 , path = file("web"))
need to debug the play server from IntelliJ IDEA.
To run the project I use sbt run on the command line.
How can I debug the project in IDEA?
I found this to be the easiest solution : (using IntelliJ IDEA )
in IntelliJ :
Go to "edit run configurations"
Create a new remote configuration (port 9999, all other details leave with default values)
Go back to IntelliJ and run the new debug configuration (don't forget to put a break point)
From command line run :
sbt -jvm-debug 9999 run
The easiest solution.
Edit Configurations... -> add SBT Task (not Remote task).
Specify SBT Task: ~run.
Run created SBT Task using - Debug button
Provided you've Play distribution installed locally, use play debug run on the command line and connect to localhost on the port 9999 in IDEA.
From Debugging section in Using the Play console in the official Play 2.2.x documentation:
You can ask Play to start a JPDA debug port when starting the console.
You can then connect using Java debugger. Use the play debug command
to do that
If however you don't have it (and for a reason don't want to install it), add Remote Run configuration in IDEA that will give you a hint for the command line arguments you should be using when launching SBT, e.g.
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005
When you launch SBT that may or may not be as simple as launching SBT jar, just use the above to configure JVM to run in debug mode.
IntelliJ IDEA 2016.1.1 && Play Framework 2.5.3
For me, no matter how I set(create new Run/Debug Configuration for Play 2 App or SBT Task, specify the debug port, execute in Run or Debug mode) in the IntelliJ IDEA 2016.1.1 Enterprise Edtion, the IDEA can not open the debug port(default 9999), so the debug is impossible.
After disable the sbt-fork-run-plugin(comment it in /project/paly-fork-run.sbt), it works!!!
I am newer to Play framework,and have found many bugs...Compare to RoR, it's so hard to learn, to run, to use, to debug...
Below is my steps:
disable the sbt-fork-run-plugin(comment it in /project/paly-fork-run.sbt)
execute activator -jvm-debug 9999 "run 11111" (I use port 9999 to debug, port 11111 to run my Play project)
In IDEA, add an new Run/Debug configuration, Choose, set debug port to 9999
debug the new created configutation

groovy (java): exec() do not detach from process (Intellij IDEA vs Maven+TestNG)

I have Groovy Maven2 test project with TestNG and Surefire plugin enabled.
I want to launch external process (*.cmd file which start some *.exe file) in last test method, finish my tests and left process running after tests.
I tried the following codes to do it:
1 attempt
def builder = new ProcessBuilder('cmd','/c <name>.cmd')
builder.directory( ( new File( <path_to_working_directory> ) ) )
builder.start()
2 attempt (with and without start cmd option)
Runtime.getRuntime().exec( "cmd /c start <name>.cmd", null , ( new File( <path_to_working_directory> ) ) )
3 attempt
( new AntBuilder() ).exec(
dir: "<path_to_working_directory>",
executable: "<name>.cmd"
)
Where .cmd is:
set path=<path_to_execFile>;%path%
start <execFileName>.exe
When I launch each of these codes from Intellij IDEA via 'Run' functionality (Alt+Shift+F10) codes execute successfully, process started and run after test finishes.
When I launch each of these codes both from Intellij IDEA Maven task, clean Maven installation (and even Maven task from Jenkins) process started successfully but test remains running. I need to kill it manually. When I kill test process (Maven process) manually my launched external process continue to work as I expect.
This hung test process is my headache for the moment.
I looked through a lot of materials but didn't find any root cause, fix and even workaround for this issue. I see that all my attempts (perhaps, except of AntBuilder()) create deattached processes. I suppose that this can be connected with JVM settings. But I coudnl't find to which one.
Also, I tried
"full command to run my cmd".execute()
but it didn't help me too.
Could you please help me resolve the issue?
Thanks In Advance!
So, I do not see any answers for my issue here. But I have some updates.
I found that I can use PsExec tool instead of direct cmd calling:
def builder = new ProcessBuilder( 'psexec', 'cmd', '/c', '<name>.cmd' )
builder.directory( ( new File( <path_to_working_directory> ) ) )
builder.start()
And this code works fine when I launch it from clean Maven only (not from Jenkins): process started, Maven task completes successfully, the process continues to run.
But during execute this code as part of some Maven2 Jenkins task I faced to issue again: psexec started but Jenkins task is running and my process does not started before I terminate Jenkins task manually.
To avoid this issue I created simple additional Groovy service script I launch in listen mode (and Writing a TCP Server) on target machine manually during initial machine preparation. This script is running on machine always.
I send to this listener name of command file to execute from my test I launch from Jenkins and it executes all cmds successfully: processes start, Jenkins task completes successfully, processes continue to run. I use processbuilder inside this listener.
For name sending I use simple socket (Writing a TCP Client)
Also, I found how to detach child from process tree on win32?. But for me system my way looks more Groovy I think.