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

Fix searching from inside a hidden directory #2377

Merged
merged 2 commits into from
Nov 26, 2023
Merged

Fix searching from inside a hidden directory #2377

merged 2 commits into from
Nov 26, 2023

Conversation

kitterion
Copy link
Contributor

Description

When searching files like this:

mkdir .hidden_folder
cd .hidden_folder
touch File.kt
ktlint '*.kt'

ktlint reported that it found no files.
The fact that the current directory is hidden should have no effect.

Checklist

Before submitting the PR, please check following (checks which are not relevant may be ignored):

  • Commit message are well written. In addition to a short title, the commit message also explain why a change is made.
  • At least one commit message contains a reference Closes #<xxx> or Fixes #<xxx> (replace<xxx> with issue number)
  • Tests are added
  • KtLint format has been applied on source code itself and violations are fixed
  • PR title is short and clear (it is used as description in the release changelog)
  • PR description added (background information)

Documentation is updated. See difference between snapshot and release documentation

  • Snapshot documentation in case documentation is to be released together with a code change
  • Release documentation in case documentation is related to a released version of ktlint and has to be published as soon as the change is merged to master

When searching files like this:
```
mkdir .hidden_folder
cd .hidden_folder
touch File.kt
ktlint '*.kt'
```
ktlint reported that it found no files.
The fact that the current directory is hidden should have no effect.
@paul-dingemans
Copy link
Collaborator

Tnx for your contribution. Unfortunately, I have to reject it in its current form.

What are your reasons to use hidden folders and what is your need to scan them?

Hidden directories are excluded on purpose. I think, the original reason was to exclude those directories as they are used by version control systems like git. Scanning files in .git folders is for example unwanted for two reasons: 1) this folder can be massively large (e.g. deeply nested); 2) files in this directory should not be manipulated manually.

Of course the current exclusion of all hidden folders, might lead to exclusion of folders for which you do want to enable scanning. The default behavior of excluding hidden folders may not be changed.

Have you tried to specify the path to your hidden folder as glob? Something like below, does also scan the hidden folders for me:

ktlint **/*.kt **/*.kts src/main/kotlin/.hidden_folder/**

@kitterion
Copy link
Contributor Author

Hidden directories are excluded on purpose. I think, the original reason was to exclude those directories as they are used by version control systems like git. Scanning files in .git folders is for example unwanted for two reasons: 1) this folder can be massively large (e.g. deeply nested); 2) files in this directory should not be manipulated manually.

Understandable, nothing changes in those cases. The hidden folders under the root folder continue to be skipped. All the existing tests pass as well, including those that test that ktlint does not enter hidden folders. There is a side-effect of the current logic, however: if the root folder itself is hidden from the point of view of its parent then no searching will be done at all.
In other words, entering the hidden folder (cd .hidden_folder in the example above) and searching within that folder should work but doesn't. I feel like if I enter a hidden folder then I have expressed intent that I want to search within that folder.

What are your reasons to use hidden folders and what is your need to scan them?

I have a tool that launches subcommands from within a hidden folder, and unfortunately I have little control over it. One of the subcommands is ktlint, and ktlint refuses to search any files, no matter how I lay them out on the filesystem.

Something like below, does also scan the hidden folders for me:

 ktlint **/*.kt **/*.kts src/main/kotlin/.hidden_folder/**

In this example as written, the shell expands **, not ktlint. In this case ktlint ignores the fact that files might be in a hidden directory, it just uses the full filepaths, because that's the only thing it sees. It doesn't traverse the filesystem because it doesn't need to.
If I change that example slightly by adding quotes

ktlint '**/*.kt' '**/*.kts' 'src/main/kotlin/.hidden_folder/**'

then ktlint stops searching files under 'src/main/kotlin/.hidden_folder'.
What I'm fixing is (note the quotes here as well)

cd src/main/kotlin/.hidden_folder
ktlint '**'

… Make unit test less confusing by renaming ".git" to ".hidden"
@paul-dingemans
Copy link
Collaborator

Tnx for detailed explanation! Now I understand what the change is about. I have added a commit to add a log message and made the unit less confusing.

@kitterion
Copy link
Contributor Author

No worries, thanks!

@paul-dingemans paul-dingemans merged commit c2697f6 into pinterest:master Nov 26, 2023
11 checks passed
@kitterion kitterion deleted the fix-searching-from-inside-a-hidden-directory branch November 26, 2023 19:42
@paul-dingemans paul-dingemans added this to the 1.1 milestone Nov 28, 2023
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.

None yet

2 participants