-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix -Wconf:src Windows path conversion #24772
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
base: main
Are you sure you want to change the base?
Fix -Wconf:src Windows path conversion #24772
Conversation
Updates the -Wconf:src filter to avoid using `java.nio.file.Path.toURI` in order to fix Windows source path conversions. `Path.toURI` prepends the current working directory to Windows-like paths unconditionally, and converts backslashes in such paths to `%5C` escape sequences This can cause `-Wconf:src` filters that work on non-Windows platforms to fail on Windows. For example, before this change, the `Path.toURI` conversion in the `SourcePattern` case from `MessageFilter.matches()` produced: ```txt original: Optional[C:\foo\bar\myfile.scala] resolved: /Users/mbland/src/scala/scala3/C:%5Cfoo%5Cbar%5Cmyfile.scala ``` After this change, it produces the following, which still prepends the current working directory, but properly converts path separators to `/`: ```txt original: Optional[C:\foo\bar\myfile.scala] resolved: /Users/mbland/src/scala/scala3/C:/foo/bar/myfile.scala ``` This change is based on scala/scala#11192, and also adapts some test cases from that pull request to validate symlink and normalized path handling. This change also extracts the `diagnosticWarning` helper method to reduce duplication between new and existing test cases.
After blowing the dust off my Windows VM, I can confirm this is platform dependent. On Windows: scala> java.io.File("C:\\foo\\..\\bar\\f.txt").toPath.toAbsolutePath.normalize
val res0: java.nio.file.Path = C:\bar\f.txt
scala> java.io.File("C:\\foo\\..\\bar\\f.txt").toPath.toAbsolutePath.toUri.normalize.getRawPath
val res1: String = /C:/bar/f.txtOn macOS: scala> java.io.File("C:\\foo\\..\\bar\\f.txt").toPath.toAbsolutePath.normalize
val res0: java.nio.file.Path = /Users/luc/code/scala/scala13/sandbox/C:\foo\..\bar\f.txt
scala> java.io.File("C:\\foo\\..\\bar\\f.txt").toPath.toAbsolutePath.toUri.normalize.getRawPath
val res1: String = /Users/luc/code/scala/scala13/sandbox/C:%5Cfoo%5C..%5Cbar%5Cf.txtSo arguably the current implementation using |
Hmm, arguably, yes. So if you want, I could:
One other thing I noticed was that, unlike the Scala 2 implementation, the @Test def `Wconf src filter only matches entire directory path components`: Unit =
val path = Path("foobar/File.scala")
val result = wconfSrcFilterTest(
argsStr = "-Wconf:src=bar/.*\\.scala:s",
warning = diagnosticWarning(util.SourceFile(new PlainFile(path), "UTF-8"))
)
assertEquals(result, Right(reporting.Action.Warning))Note that the current tests reverse the [error] Test dotty.tools.dotc.config.ScalaSettingsTests.Wconf src filter only matches entire directory path components failed:
java.lang.AssertionError: expected:<Right(Silent)> but was:<Right(Warning)>, took 0.001 sec
[error] at dotty.tools.dotc.config.ScalaSettingsTests.Wconf src filter only matches entire directory path components(ScalaSettingsTests.scala:308)So the fourth option would be:
|
Updates the -Wconf:src filter to avoid using
java.nio.file.Path.toURIin order to fix Windows source path conversions.Path.toURIprepends the current working directory to Windows-like paths unconditionally, and converts backslashes in such paths to%5Cescape sequences This can cause-Wconf:srcfilters that work on non-Windows platforms to fail on Windows.For example, before this change, the
Path.toURIconversion in theSourcePatterncase fromMessageFilter.matches()produced:After this change, it produces the following, which still prepends the current working directory, but properly converts path separators to
/:This change is based on scala/scala#11192, and also adapts some test cases from that pull request to validate symlink and normalized path handling. This change also extracts the
diagnosticWarninghelper method to reduce duplication between new and existing test cases.Fixes #24771. Review by @lrytz.