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

Allow passing a file path when using --stdin #1123

Closed
benkay opened this issue Mar 26, 2021 · 2 comments · Fixed by #2836
Closed

Allow passing a file path when using --stdin #1123

benkay opened this issue Mar 26, 2021 · 2 comments · Fixed by #2836

Comments

@benkay
Copy link

benkay commented Mar 26, 2021

When formatting using --stdin, you lose filename/path in any error messages (obviously). Additionally there's no way to use things like specific-directory editorconfig overrides, which depend on knowing the file location.

This affects some tools like git-format-staged, which work by passing files through stdin. To get around this, some other formatters allow passing a path to treat as the current file path e.g. --stdin-filepath in Prettier, or --stdinpath in SwiftFormat.

It'd be great if ktlint supported this too!

@paul-dingemans
Copy link
Collaborator

Additionally there's no way to use things like specific-directory editorconfig overrides, which depend on knowing the file location.

This additional reason is not true. When the editorconfig parameter is specified it overrides any .editorconfig found on the path to the file.

$ cat .editorconfig 
[*.{kt,kts,gradle}]
indent_size = 4

$ ktlint-0.46.1 --stdin
class Foo {
fun foo() {}
}
<stdin>:2:1: Unexpected indentation (0) (should be 4) (indent)

$ cat ../xx/.editorconfig 
[*.{kt,kts,gradle}]
indent_size = 8

$ ktlint-0.46.1 --editorconfig=../xx --stdin
class Foo {
fun foo() {}
}
<stdin>:2:1: Unexpected indentation (0) (should be 8) (indent)

$ cat ../yy/.editorconfig 
[*.{kt,kts,gradle}]
indent_size = 2

$ ktlint-0.46.1 --editorconfig=../yy --stdin
class Foo {
fun foo() {}
}
<stdin>:2:1: Unexpected indentation (0) (should be 2) (indent)

Given above, it does not seem to have an added value to specify the file path explicitly.

@paul-dingemans paul-dingemans closed this as not planned Won't fix, can't repro, duplicate, stale Mar 27, 2023
@adecker89
Copy link
Contributor

adecker89 commented Oct 15, 2024

A problem that was not mentioned before is that by not having the file path available some rules like FilenameRule that depend on it will silently break when used with --stdin.

Providing the file location could fix this behavior.

adecker89 pushed a commit to adecker89/ktlint that referenced this issue Oct 16, 2024
adecker89 pushed a commit to adecker89/ktlint that referenced this issue Oct 16, 2024
paul-dingemans added a commit that referenced this issue Oct 19, 2024
Fixes #1123

---------

Co-authored-by: Alex Decker <[email protected]>
Co-authored-by: Paul Dingemans <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants