Skip to content

Modify PrettyPrinter to output diagnostics using shortened relative paths #934

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

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/SwiftFormat/PrettyPrint/PrettyPrint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ public class PrettyPrinter {
context.findingEmitter.emit(
message,
category: category,
location: Finding.Location(file: context.fileURL.path, line: outputBuffer.lineNumber, column: column)
location: Finding.Location(file: context.fileURL.relativePath, line: outputBuffer.lineNumber, column: column)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m a little skeptical of this. When forming a URL, we provide a baseURL and a path relative to it. Generally, I wouldn’t expect it to matter what’s part of the base URL and what’s part of the relative path. For example, I would have expected url = URL(string: url.absoluteString) to not have any effect on the output but with this change it does.

What I’d prefer instead is some way of stripping off a known base path from context.fileURL here (though I don’t have concrete suggestions of how to do so at the moment).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, this change would bring the pretty printer to be consistent with how we're using the relativePath property elsewhere.

When we create the SourceLocationConverter that the other non-pretty-printer rules use in Context.swift, we use the relativePath property.

Then, in FileIterator.swift, we have a bunch of logic that explicitly constructs the URLs this way. (In fact, I thought we were using FileManager.DirectoryEnumerationOptions.producesRelativePathURLs here, but that doesn't seem to be the case. That might have been historical, or there might have been a platform-specific issue that prevented it.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @allevato mentioned, other parts of the codebase already use relativePath except for PrettyPrinter, so I thought it would be fine to change it to use the same property for consistency.

Would it be better to implement a separate logic to derive relative path, similar to what's done in FileIterator, and apply it across all rule diagnostics, including PrettyPrinter?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, OK. I didn’t realize that we already used relativePath elsewhere. While I have the same objection still in principle, I don’t object to adding another use for it if there’s already precedence.

)
}
}
Expand Down