Skip to content

Support -stdin_filename for input from stdin #299

@andrei-levitskii

Description

@andrei-levitskii

Hi!

Some formatters support an option to assume the filename when content is passed through stdin (clang-format's --assume-filename, ruff's --stdin-filename). Based on that filename they may act as if the user has passed an actual file.
I'm particularly interested in include/exclude config settings.

Currently, given this config

match_type: doublestar
exclude: ["**/skip.yaml"]

I get the following output when provide the file path

$ ./yamlfmt -v -debug all -conf .yamlfmt skip.yaml 
[DEBUG]: Using config path .yamlfmt from -conf flag
[DEBUG]: using doublestar path matching. include patterns: [skip.yaml]
[DEBUG]: trying pattern: skip.yaml
[DEBUG]: pattern skip.yaml matches: [skip.yaml]
[DEBUG]: calculating excludes for skip.yaml
[DEBUG]: pattern **/skip.yaml matched, excluding
[DEBUG]: paths to format: []
[DEBUG]: The following files were modified:

The following files were modified:

When passed to stdin it's being modified regardless which is expected

$ cat skip.yaml | ./yamlfmt -v -debug all -conf .yamlfmt -
[DEBUG]: Using config path .yamlfmt from -conf flag
[DEBUG]: using doublestar path matching. include patterns: [-]
[DEBUG]: trying pattern: -
[DEBUG]: pattern - matches: []
[DEBUG]: paths to format: []
a: 1

With -stdin_filename skip.yaml modification wouldn't have happened.

$ cat skip.yaml | ./yamlfmt -v -debug all -conf .yamlfmt -stdin_filename skip.yaml -
[DEBUG]: Using config path .yamlfmt from -conf flag
[DEBUG]: using doublestar path matching. include patterns: [skip.yaml]
[DEBUG]: trying pattern: skip.yaml
[DEBUG]: pattern skip.yaml matches: [skip.yaml]
[DEBUG]: calculating excludes for skip.yaml
[DEBUG]: pattern **/skip.yaml matched, excluding
[DEBUG]: paths to format: []
[DEBUG]: The following files were modified:

The following files were modified:

yamlfmt version

$ ./yamlfmt --version
yamlfmt 0.20.0 (42486ac)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions