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

cmd/cue: Allow "fmt" subcommand to indicate whether any formatting changes are necessary #363

Closed
cueckoo opened this issue Jul 3, 2021 · 4 comments
Labels
FeatureRequest New feature or request fmt Related to formatting functionality.

Comments

@cueckoo
Copy link
Collaborator

cueckoo commented Jul 3, 2021

Originally opened by @seh in cuelang/cue#363

At present, cue fmt unconditionally overwrites a target file, whether or not any changes in formatting were required. It doesn't offer any indication to a calling process whether the input required rewriting. In other words, repeated invocations of cue fmt reaches a fixed point, but a caller can only tell that by comparing the input and output afterward.

Describe the solution you'd like
Introduce three additions to cue fmt:

  • Exit with a distinguished code when no formatting change was necessary.
    Since the existing tool exits with code zero whether or not it had to change the file content, in the interest of backward compatibility, we could introduce a command-line flag like --detailed-code to enable this distinction. Exit with zero if the content changed, and something like two if the content didn't need to change.
  • Don't overwrite the target file if no formatting change was necessary.
    If we're already able to tell whether a change is necessary, we can skip overwriting the target file when there's no need to do so. cue fmt already claims that it operates in place, so there's no need to emit unchanged content to handle creating new files unconditionally. This is also convenient when integrating with tools like Make, which might be fooled by the file's mtime changing even if its content did not. (Bazel is more clever than that.)
  • Introduce a "test-only" mode.
    Allow using cue fmt to detect whether changes are necessary without actually writing those changes to the target file. This is useful in pre-commit hooks and CI jobs to enforce proper formatting of CUE files.

Describe alternatives you've considered
It's possible to wrap cue fmt in a helper tool that copies the original content, runs cue fmt, compares the original content to the resulting content, and exits with a distinguished code indicating its findings. In other words, the predicate is whether the original content matches the resulting content. Could we track whether we've performed any mutations in the call to cmd.fix, and handle this inside of cue fmt?

Additional context
The jsonnetfmt command is the basis of inspiration here.

@cueckoo cueckoo added FeatureRequest New feature or request fmt Related to formatting functionality. labels Jul 3, 2021
@cueckoo
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @seh in cuelang/cue#363 (comment)

I see that the aging #247 addresses some of these ideas.

@eonpatapon
Copy link
Contributor

I'd like to work on this issue as it would make my life simpler :)

Can we talk about how we should approach this ? Ie: what UX interface to implement ?

@myitcv myitcv added this to the fmt-redesign milestone Apr 27, 2023
@myitcv myitcv added the zGarden label Jun 15, 2023
@mvdan mvdan removed the zGarden label Feb 8, 2024
@myitcv
Copy link
Member

myitcv commented Mar 11, 2024

For modules we are just about to land cue mod tidy --check. It feels like the time is right to do something for cue fmt :)

Not least because Go is moving in that direction too: golang/go#46289 (comment)

We have missed the boat for v0.8.0, but would be happy to see something land in the first alpha of v0.9.0.

I will create a project for that release series now.

@myitcv
Copy link
Member

myitcv commented Mar 11, 2024

Done: https://github.com/orgs/cue-lang/projects/21/views/1

If anyone would like to work on this, please shout!

@mvdan mvdan removed this from the fmt-redesign milestone Mar 19, 2024
cueckoo pushed a commit that referenced this issue Mar 30, 2024
Adds a --check flag that will cause cue to fail with exit code 1
in case any files require formatting. A list of non formatted files
will be displayed, line by line, to stdout.

resolves #363.

Change-Id: I27c8e9b18bb01f981cd061a23ee3323bc403c9a9
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
cueckoo pushed a commit that referenced this issue Mar 30, 2024
Adds a --check flag that will cause cue to fail with exit code 1
in case any files require formatting. A list of non formatted files
will be displayed, line by line, to stdout.

Also, a typo is fixed in cue/ast/ast.go.

resolves #363.

Change-Id: I27c8e9b18bb01f981cd061a23ee3323bc403c9a9
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
cueckoo pushed a commit that referenced this issue Mar 30, 2024
Adds a --check flag that will cause cue to fail with exit code 1
in case any files require formatting. A list of non formatted files
will be displayed, line by line, to stdout.

Also, a typo is fixed in cue/ast/ast.go.

resolves #363.

Change-Id: I27c8e9b18bb01f981cd061a23ee3323bc403c9a9
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
cueckoo pushed a commit that referenced this issue Mar 30, 2024
Adds a --check flag that will cause cue to fail with exit code 1
in case any files require formatting. A list of non formatted files
will be displayed, line by line, to stdout.

Also, a typo is fixed in cue/ast/ast.go.

resolves #363.

Change-Id: I27c8e9b18bb01f981cd061a23ee3323bc403c9a9
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
cueckoo pushed a commit that referenced this issue Mar 30, 2024
Adds a --check flag that will cause cue to fail with exit code 1
in case any files require formatting. A list of non formatted files
will be displayed, line by line, to stdout.

Also, a typo is fixed in cue/ast/ast.go.

resolves #363.

Change-Id: I27c8e9b18bb01f981cd061a23ee3323bc403c9a9
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
cueckoo pushed a commit that referenced this issue Mar 30, 2024
Adds a --check flag that will cause cue to fail with exit code 1
in case any files require formatting. A list of non formatted files
will be displayed, line by line, to stdout.

Also, a typo is fixed in cue/ast/ast.go.

resolves #363.

Change-Id: I27c8e9b18bb01f981cd061a23ee3323bc403c9a9
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
cueckoo pushed a commit that referenced this issue Mar 30, 2024
Adds a --check flag that will cause cue to fail with exit code 1
in case any files require formatting. A list of non formatted files
will be displayed, line by line, to stdout.

Also, a typo is fixed in cue/ast/ast.go.

fixes #363.

Change-Id: I27c8e9b18bb01f981cd061a23ee3323bc403c9a9
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
cueckoo pushed a commit that referenced this issue Mar 30, 2024
Adds a --check flag that will cause cue to fail with exit code 1
in case any files require formatting. A list of non formatted files
will be displayed, line by line, to stdout.

Also, a typo is fixed in cue/ast/ast.go.

fixes #363.

Change-Id: I27c8e9b18bb01f981cd061a23ee3323bc403c9a9
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
cueckoo pushed a commit that referenced this issue Mar 30, 2024
Adds a --check flag that will cause cue to fail with exit code 1
in case any files require formatting. A list of non formatted files
will be displayed, line by line, to stdout.

Also, a typo is fixed in cue/ast/ast.go.

fixes #363.

Change-Id: I27c8e9b18bb01f981cd061a23ee3323bc403c9a9
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
cueckoo pushed a commit that referenced this issue Mar 31, 2024
Adds a --check flag that will cause cue to fail with exit code 1
in case any files require formatting. A list of non formatted files
will be displayed, line by line, to stdout.

Also, a typo is fixed in cue/ast/ast.go.

fixes #363.

Change-Id: I27c8e9b18bb01f981cd061a23ee3323bc403c9a9
Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
cueckoo pushed a commit that referenced this issue Apr 25, 2024
Currently, `cue fmt` will always write to cue files, regardless of
whether they are formatted or not. In the case of formatted files, there
contents will remain exactly the same, but their modification time will
change. This is problematic for tools that rely on the mod time.

Fixes #1731, #363

Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
Change-Id: Iea55c1eb9e99c1dcba301194857a92dad1faa9ad
cueckoo pushed a commit that referenced this issue Apr 25, 2024
Currently, `cue fmt` will always write to cue files, regardless of
whether they are formatted or not. In the case of formatted files, there
contents will remain exactly the same, but their modification time will
change. This is problematic for tools that rely on the mod time.

Fixes #1731, #363

Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
Change-Id: Iea55c1eb9e99c1dcba301194857a92dad1faa9ad
cueckoo pushed a commit that referenced this issue Apr 25, 2024
Currently, `cue fmt` will always write to cue files, regardless of
whether they are formatted or not. In the case of formatted files,
their contents will remain exactly the same, but their modification
time will change.
This is problematic for tools that rely on the mod time.

To fix this, we buffer the original/formatted bytes. If the result
is exactly the same, the file is not written to.

Fixes #1731, #363

Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
Change-Id: Iea55c1eb9e99c1dcba301194857a92dad1faa9ad
cueckoo pushed a commit that referenced this issue Apr 25, 2024
Currently, `cue fmt` will always write to cue files, regardless of
whether they are formatted or not. In the case of formatted files,
their contents will remain exactly the same, but their modification
time will change.
This is problematic for tools that rely on the mod time.

To fix this, we buffer the original/formatted bytes. If the result
is exactly the same, the file is not written to.

Fixes #1731, #363

Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
Change-Id: Iea55c1eb9e99c1dcba301194857a92dad1faa9ad
cueckoo pushed a commit that referenced this issue Apr 25, 2024
Currently, `cue fmt` will always write to cue files, regardless of
whether they are formatted or not. In the case of formatted files,
their contents will remain exactly the same, but their modification
time will change.
This is problematic for tools that rely on the mod time.

To fix this, we buffer the original/formatted bytes. If the result
is exactly the same, the file is not written to.

Fixes #1731, #363

Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
Change-Id: Iea55c1eb9e99c1dcba301194857a92dad1faa9ad
cueckoo pushed a commit that referenced this issue Apr 25, 2024
Currently, `cue fmt` will always write to cue files, regardless of
whether they are formatted or not. In the case of formatted files,
their contents will remain exactly the same, but their modification
time will change.
This is problematic for tools that rely on the mod time.

To fix this, we buffer the original/formatted bytes. If the result
is exactly the same, the file is not written to.

Fixes #1731, #363

Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
Change-Id: Iea55c1eb9e99c1dcba301194857a92dad1faa9ad
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest New feature or request fmt Related to formatting functionality.
Projects
Status: v0.9.0-alpha.2
Development

No branches or pull requests

4 participants