Skip to content
New issue

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

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

Already on GitHub? # to your account

CLI should lint only *.kt,*.kts files when only path are provided #917

Closed
nokite opened this issue Sep 18, 2020 · 3 comments · Fixed by #1537
Closed

CLI should lint only *.kt,*.kts files when only path are provided #917

nokite opened this issue Sep 18, 2020 · 3 comments · Fixed by #1537
Assignees
Labels
cli ktlint command line interface enhancement

Comments

@nokite
Copy link

nokite commented Sep 18, 2020

Expected Behavior

The same type of files (kotlin) should be checked regardless of whether a path has been passed or not - as long as no pattern has been specified in the path.
No errors of type "Not a valid Kotlin file" should be shown for files that are clearly not in Kotlin.

Observed Behavior

Seeing errors for Java files when running with a path parameter without a pattern, like
ktlint /Users/bla/project/somefolder/

Not a valid Kotlin file (51:68 name expected)
Not a valid Kotlin file (6:5 expecting member declaration)
Not a valid Kotlin file (55:30 expecting an element)  

Steps to Reproduce

It's just an Android project with some Java and Kotlin files. Java and Kotlin are found coexisting in the same packages and folders. Happens both on classes and interfaces.

Your Environment

  • Version of ktlint used: 0.39.0 (updated from 0.36.0 where the issue was not present)
  • Name and version (or code for custom task) of integration used: command line (both brew and curl) and custom Gradle task
  • Version of Gradle used (if applicable): gradle-6.5.1-all
  • Operating System and version: OS X 10.15.6
@Tapchicoma
Copy link
Collaborator

Documentation of CLI is not explicitly states it, but you need to pass file extensions that will be included into linting. It is located in example usages.

If you invoke ktlint without any paths (for example ,./ktlint) it will only check *.kt and *.kts files starting from current location.

I am not sure if it is better to check only kt and kts files when only path without any additional file patterns was passed.

@Tapchicoma Tapchicoma added the cli ktlint command line interface label Sep 18, 2020
@nokite
Copy link
Author

nokite commented Sep 21, 2020

@Tapchicoma My bad, thanks! I thought I was seeing the error by running ktlint without specifying a path, but checking my log now - I've used a path in those cases.

If it makes any difference - it would be better if the behavior is the same, in my opinion. Whether a path is specified or not, only Kotlin files should be checked when no pattern is specified.

This turns out to be a change request, so this issue can be closed or converted.

@Tapchicoma
Copy link
Collaborator

I think what you are proposing makes sense.

@Tapchicoma Tapchicoma changed the title Getting "Not a valid Kotlin file" for .java files CLI should lint only *.kt,*.kts files when only path are provided Sep 22, 2020
@Tapchicoma Tapchicoma added this to the 0.41.0 milestone Jan 15, 2021
@Tapchicoma Tapchicoma self-assigned this Jan 15, 2021
@Tapchicoma Tapchicoma modified the milestones: 0.41.0, 0.42.0 Mar 1, 2021
@paul-dingemans paul-dingemans removed this from the 0.42.0 milestone Jan 25, 2022
paul-dingemans added a commit to paul-dingemans/ktlint that referenced this issue Jul 3, 2022
… only process files with default kotlin extensions in that directory or its subdirectories. This is equivalent to execute ktlint in that directory without specifying any pattern.

Closes pinterest#917
paul-dingemans added a commit that referenced this issue Jul 24, 2022
…1537)

* Expand (existing) directory to globs for default kotlin extensions 

* When an (existing) directory (without glob pattern) is specified then only process files with default kotlin extensions in that directory or its subdirectories. This is equivalent to execute ktlint in that directory without specifying any pattern.

Closes #917

* When a glob does not contain an absolute path then do not prefix it with the root directory but instead ensure that it is prefixed it with the "**/" matcher. In this way files directly inside the workdir are matched as well as files in subdirectories of the workdir.

* Print an error message and return with non-zero exit code when no files are found that match with the globs

Closes #629

* Refactor by eliminating variable project2Files

* Refactor to improve readability and consistency

* Extract constants to private values. Align name of test class with production class.

* Remove characters which may cause a problem on Windows

* Normalize paths for Windows build

* Disable tests on Windows using absolute paths as those are not supported by jimfs
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
cli ktlint command line interface enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants