Node.js axios upload large file using FormData, read all data to the memory, cause out of memory issue
  • 08-May-2023
Lightrun Team
Author Lightrun Team
Share
Node.js axios upload large file using FormData, read all data to the memory, cause out of memory issue

java.lang.reflect.InaccessibleObjectException: Unable to make field private transient java.lang.Object java.lang.Throwable.backtrace accessible: module java.base does not “opens java.lang” to unnamed module @76628072

Lightrun Team
Lightrun Team
08-May-2023

Explanation of the problem

It seems that the task ktlintFormat has failed due to an ExceptionInInitializerError caused by a java.lang.NullPointerException.

The error message suggests that the issue occurred while trying to remove children from a composite element, specifically in the ImportOrderingRule.visit function. The error may be related to a problem with the syntax tree, potentially caused by an incorrect implementation of the rule.

Troubleshooting with the Lightrun Developer Observability Platform

Getting a sense of what’s actually happening inside a live application is a frustrating experience, one that relies mostly on querying and observing whatever logs were written during development.
Lightrun is a Developer Observability Platform, allowing developers to add telemetry to live applications in real-time, on-demand, and right from the IDE.

  • Instantly add logs to, set metrics in, and take snapshots of live applications
  • Insights delivered straight to your IDE or CLI
  • Works where you do: dev, QA, staging, CI/CD, and production

Start for free today

Problem solution for java.lang.reflect.InaccessibleObjectException: Unable to make field private transient java.lang.Object java.lang.Throwable.backtrace accessible: module java.base does not “opens java.lang” to unnamed module @76628072

The issue at hand is caused by newer versions of Java restricting access to certain libraries, which in turn affects KtLint. This means that running the KtLint command may result in an error message that says “Illegal reflective access operation” and gives the name of the library that is being restricted. The solution to this problem is to provide the –add-opens=java.base/java.lang=ALL-UNNAMED flag when running the KtLint command. This flag is used to open all packages in the java.lang module to unnamed modules, which allows for the reflective access operation to occur without triggering the error.

To implement this solution, we need to change the definition of the ktlintFormat task in our Gradle build file. The task should be defined as a JavaExec task and we can use the tasks.creating() method to create the task. The classpath for the task should be set to the ktlint library and the jvmArgs should be set to listOf(“–add-opens=java.base/java.lang=ALL-UNNAMED”). Finally, we need to set the args for the task to the arguments that we want to pass to the KtLint command, such as the “-F” flag to fix code style deviations and the path to the Kotlin files that we want to format.

In summary, the issue with KtLint and newer versions of Java can be resolved by providing the –add-opens=java.base/java.lang=ALL-UNNAMED flag when running the KtLint command. To do this in a Gradle build file, we need to define the ktlintFormat task as a JavaExec task and set the classpath and jvmArgs appropriately. Once this is done, we can run the ktlintFormat task to format our Kotlin code without encountering any reflective access operation errors. This solution is a temporary fix until the libraries that we use are updated to work with newer versions of Java and should be used until a more permanent solution is available.

Other popular problems with ktlint

Problem: KtLint not formatting code correctly

This issue usually occurs when KtLint is not configured to use the desired formatting rules or when the formatting rules themselves are incorrect.

Solution:

One solution is to create a ktlintrc file at the root of the project to define the formatting rules. Here is an example of a ktlintrc file that defines the maximum line length as 100 and the indentation as 4 spaces:

max_line_length=100
indent_size=4

Problem: KtLint not finding certain rules or plugins

This issue can occur when KtLint is not able to find certain rules or plugins that are required for the project.

Solution:

One solution is to add the missing rules or plugins to the build.gradle file:

dependencies {
    ktlint "com.pinterest:ktlint:<version>"
    ktlintRuleset "com.github.<username>:<repository>:<tag>"
    ktlintPlugin "com.github.<username>:<repository>:<tag>"
}

Here, the ktlintRuleset and ktlintPlugin dependencies are added to provide the missing rules or plugins. Replace <username>, <repository>, and <tag> with the appropriate values for the missing dependency.

 

Problem: KtLint not working with newer versions of Java

This issue can occur when KtLint is not able to access certain libraries due to newer versions of Java restricting access to those libraries.

Solution:

One solution is to add the --add-opens JVM argument to the KtLint task to allow access to the required libraries. Here is an example:

task ktlint(type: JavaExec) {
    classpath = configurations.ktlint
    main = "com.pinterest.ktlint.Main"
    jvmArgs = ["--add-opens=java.base/java.lang=ALL-UNNAMED"]
    args = ["src/**/*.kt"]
}

A brief introduction to KtLint

Ktlint is a Kotlin linter that checks the style and formatting of Kotlin source code files. It is built to be a flexible and extensible tool that enforces a set of predefined code style rules, while also allowing for the customization and addition of new rules. The tool can be used from the command line, as a Gradle or Maven plugin, or as an IDE plugin for IntelliJ IDEA, Android Studio, and Visual Studio Code. Ktlint comes with a set of default style rules that are based on the Kotlin coding conventions, which are designed to improve code readability and maintainability. Some of these rules include enforcing the use of single quotes for string literals, preferring a single line for simple functions and properties, and requiring the use of the this keyword for member properties and functions.

One of the key benefits of using ktlint is that it can help maintain a consistent coding style across a project, which is important for team collaboration and code review. By enforcing a set of coding conventions, ktlint can help ensure that code is easier to read and maintain, reducing the likelihood of errors and improving overall code quality. Additionally, ktlint is highly configurable, allowing developers to tailor the style rules to their specific project needs. This flexibility, combined with the tool’s ease of use and integration with popular development environments, makes ktlint a valuable tool for Kotlin developers.

Overall, ktlint is a powerful and easy-to-use tool that can help Kotlin developers maintain a consistent coding style and improve code quality. Whether used from the command line, as a Gradle or Maven plugin, or as an IDE plugin, ktlint provides a flexible and customizable way to enforce coding conventions and improve code readability and maintainability. With its default set of style rules based on the Kotlin coding conventions, and the ability to customize and add new rules, ktlint is a valuable tool for teams and individuals looking to write high-quality Kotlin code.

 

Most popular use cases for KtLint

Ktlint is a static code analysis tool for Kotlin that is designed to enforce a consistent coding style and identify potential errors and bugs. It can be used to ensure code quality and maintainability, as well as promote collaboration and understanding among developers. One of the primary benefits of ktlint is that it automates the process of checking code for common style and syntax issues, which can save time and reduce the likelihood of human error.

One way ktlint can be used is to ensure that code adheres to a specific set of style guidelines. This can help to make code more readable and easier to understand, as well as prevent errors that might arise from inconsistencies in formatting and naming conventions. For example, the following code block shows how ktlint can be used to check a file for style violations based on a specific configuration:

./gradlew ktlintCheck -P ktlint.config="path/to/ktlint.yml" "src/**/*.kt"

Another use case for ktlint is to identify potential bugs and errors in code. By analyzing code at compile time, ktlint can help to catch issues early on in the development process, when they are typically less expensive and time-consuming to fix. For example, ktlint can identify issues such as unused imports, null checks, and missing semicolons, among others.

Finally, ktlint can be used as part of a continuous integration (CI) process to automatically check code for errors and enforce style guidelines. By incorporating ktlint into a CI pipeline, developers can ensure that all code is automatically checked before it is merged into a shared codebase. This can help to prevent code quality issues from arising and ensure that all code is consistent and adheres to a specific set of guidelines.

Share

It’s Really not that Complicated.

You can actually understand what’s going on inside your live applications.

Try Lightrun’s Playground

Lets Talk!

Looking for more information about Lightrun and debugging?
We’d love to hear from you!
Drop us a line and we’ll get back to you shortly.

By submitting this form, I agree to Lightrun’s Privacy Policy and Terms of Use.