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

Gradle kts files are rejected over standard in #1832

Closed
dpassen opened this issue Feb 21, 2023 · 1 comment · Fixed by #1865
Closed

Gradle kts files are rejected over standard in #1832

dpassen opened this issue Feb 21, 2023 · 1 comment · Fixed by #1865
Labels
bug cli ktlint command line interface
Milestone

Comments

@dpassen
Copy link

dpassen commented Feb 21, 2023

Expected Behavior

I expect ktlint to handle build.gradle.kts files over standard in.

$ <build.gradle.kts ktlint --stdin

Observed Behavior

$ <build.gradle.kts ktlint --log-level=info --stdin
10:37:16.772 [main] INFO com.pinterest.ktlint.internal.KtlintCommandLine - Enable default patterns [**/*.kt, **/*.kts]
<stdin>:1:1: Not a valid Kotlin file (1:1 expecting a top level declaration) (cannot be auto-corrected) ()

Summary error count (descending) by rule:
  Not a valid Kotlin file: 1

Steps to Reproduce

Pass a build.gradle.kts file over standard input.

Your Environment

  • Version of ktlint used: 0.48.2
  • Relevant parts of the .editorconfig settings: n/a
  • Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task): n/a
  • Version of Gradle used (if applicable): 8.0.1
  • Operating System and version: macOS Ventura 13.2.1
@paul-dingemans
Copy link
Collaborator

Given the command you specified, ktlint can not determine whether the code snippet is to be treated as kotlin (.kt) or as kotlin script (.kts).

The first log line is misleading in this case. It actually tells you which patterns will be recognized when scanning files. However when reading from stdin no files are scanned, only stdin is read.

10:37:16.772 [main] INFO com.pinterest.ktlint.internal.KtlintCommandLine - Enable default patterns [**/*.kt, **/*.kts]

Ktlint needs to run the embedded kotlin compiler with a file with either .kt or a .kts extension and it chooses .kt by default. As a result the pre-compiler throws the error:

<stdin>:1:1: Not a valid Kotlin file (1:1 expecting a top level declaration) (cannot be auto-corrected) ()

Possible solutions to be investigated:

  • For extension to be specified explicitly when reading from stdin. Either use the current patterns parameter of add a new parameter which indicates that stdin has to be treated as kotlin script
  • In case reading from stdin then retry compilation automatically as .kts file in case initial compilation with .kt fails.
  • When reading from stdin send the same snippet both as .kt and .kts file to the processing pipeline. Exactly one of the files is expected to raise an compilation error and could be ignored.

@paul-dingemans paul-dingemans added bug cli ktlint command line interface labels Feb 21, 2023
@paul-dingemans paul-dingemans added this to the 0.49.0 milestone Mar 14, 2023
paul-dingemans added a commit that referenced this issue Mar 16, 2023
paul-dingemans added a commit that referenced this issue Mar 18, 2023
…#1865)

* Recognize Kotlin Script when linting and formatting code from `stdin` with KtLint CLI

Closes #1832
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug cli ktlint command line interface
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants