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

Refactor validateWorkspace to handle missing custom scalafmtConfigPath gracefully and log warning #7080

Merged
merged 3 commits into from
Jan 3, 2025

Conversation

Austinito
Copy link

@Austinito Austinito commented Dec 31, 2024

PR Overview

This PR refactors the validateWorkspace function to gracefully handle missing scalafmtConfigPath files. Previously, this scenario would result in uncaught exceptions.

Example Logs

2024.12.31 11:48:07 ERROR Failed to connect with build server, no functionality will work.
java.nio.file.NoSuchFileException: {dir}/akka-quickstart-scala/.my_scalafmt.conf
	at sun.nio.fs.UnixException.translateToIOException(UnixException.java:92)
	at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:106)
	at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
	at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:218)
	at java.nio.file.Files.newByteChannel(Files.java:380)
	at java.nio.file.Files.newByteChannel(Files.java:432)
	at java.nio.file.Files.readAllBytes(Files.java:3288)
	at scala.meta.internal.io.PlatformFileIO$.slurp(PlatformFileIO.scala:42)
	at scala.meta.internal.io.FileIO$.slurp(FileIO.scala:18)
	at scala.meta.internal.mtags.ScalametaCommonEnrichments$XtensionAbsolutePath.toInput(ScalametaCommonEnrichments.scala:445)
	at scala.meta.internal.metals.MetalsEnrichments$XtensionAbsolutePathBuffers.toInputFromBuffers(MetalsEnrichments.scala:621)
	at scala.meta.internal.metals.FormattingProvider.validateWorkspace(FormattingProvider.scala:408)

The refactor:

  • Wraps .toInputFromBuffers in a Try to catch errors.
  • Logs a warning when custom scalafmtConfigPath is missing.
  • Returns None instead of throwing exceptions, improving stability and providing clear feedback.

Key Changes

  • Added handling for missing scalafmtConfigPath.
  • Refactored validateWorkspace to use a safer text extraction approach.

Rationale

Improves robustness and user experience by ensuring the absence of scalafmtConfigPath is handled gracefully with logging instead of crashes.

Stemmed from discussion in nvim-metals (logs included):

@Austinito Austinito changed the title Refactor validateWorkspace to handle missing .scalafmt.conf gracefully and log warning Refactor validateWorkspace to handle missing custom scalafmt path gracefully and log warning Jan 2, 2025
@Austinito Austinito changed the title Refactor validateWorkspace to handle missing custom scalafmt path gracefully and log warning Refactor validateWorkspace to handle missing custom scalafmtConfigPath gracefully and log warning Jan 2, 2025
Copy link
Contributor

@tgodzik tgodzik left a comment

Choose a reason for hiding this comment

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

Thanks! LGTM

I pushed one last commit to run scalafix

@tgodzik tgodzik merged commit 521cc9d into scalameta:main Jan 3, 2025
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants