Skip to content

Conversation

@mbland
Copy link

@mbland mbland commented Dec 17, 2025

Adds ^ and $ anchors to the -Wconf:src regex to ensure it matches entire path segments by default. Mostly replicates the Scala 2 src filter anchoring logic except for the rootDir bit, which Scala 3 doesn't support:

Applies the new anchored function after parsing the -Wconf:src argument as a regex first. This guards against pathological cases of invalid patterns that may become valid after anchoring, such as \ becoming /\$. (One of the existing test cases covers this specific invalid regex case.)

Adds new test cases, including cases to validate the existing behavior of normalizing paths without resolving symlinks. This is to help ensure that the Scala 2 issue from scala/bug#13145 (which scala/scala#11192 resolves) doesn't ever appear.

Also extracts the diagnosticWarning, virtualFile, and plainFile helper methods to reduce duplication between new and existing test cases.

Fixes #24771. Review by @lrytz.

@lrytz
Copy link
Member

lrytz commented Dec 18, 2025

After this change, it produces the following, which still prepends the current working directory, but properly converts path separators to /:

original: Optional[C:\foo\bar\myfile.scala]
resolved: /Users/mbland/src/scala/scala3/C:/foo/bar/myfile.scala

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.txt

On 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.txt

So arguably the current implementation using toUri is also OK..?

@mbland
Copy link
Author

mbland commented Dec 18, 2025

So arguably the current implementation using toUri is also OK..?

Hmm, arguably, yes. So if you want, I could:

  • Close this pull request outright (and maybe open a new one for Scala 2 that uses toURI and removes the Windows path assertions, to ensure parity between the implementations?)
  • Pare the changes down to only add the new tests (minus the Windows path test), which may be good to have
  • Keep the pull request as it is, to make the Scala 3 implementation closer to the implementation from Don't resolve symlinks in -Wconf:src scala#11192

One other thing I noticed was that, unlike the Scala 2 implementation, the src filter doesn't automatically prepend / to patterns to ensure they match an entire path component. For example, this currently fails:

  @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 expected, actual ordering of the assertEquals parameters, so the assertion failure message reads backwards:

[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:

  • Repurpose this pull request so that src path prefixes only match entire directory path components.

@Gedochao Gedochao assigned lrytz and unassigned tgodzik Dec 19, 2025
@Gedochao Gedochao requested a review from lrytz December 19, 2025 09:52
@lrytz
Copy link
Member

lrytz commented Dec 19, 2025

Keeping the additional tests certainly sounds useful to me.

src filter doesn't automatically prepend / to patterns to ensure they match an entire path component

Not sure if that was a deliberate simplification, @povder (#18783)?

@mbland mbland force-pushed the issue/24771-wconf-src-fix-windows-path-conversion branch 2 times, most recently from 3b08f8a to d1c809b Compare January 6, 2026 20:50
@mbland
Copy link
Author

mbland commented Jan 6, 2026

@lrytz I've restored the toUri method, and took a stab at adding the path component anchors from Scala 2. Namely, it now adds / to the beginning and $ to the end of the -Wconf:src pattern if needed (rootDir doesn't appear to be present in Scala 3). If either you or @povder have a reason to revert this, I'm happy to do so.

Once everything looks good, I can update the PR title and description and squash the commits accordingly. (And let me know if I should go back and touch up the Scala 2 implementation as well.)

BTW, the one job failing at the moment appears to be due to a transient infrastructure failure.

@lrytz
Copy link
Member

lrytz commented Jan 8, 2026

LGTM, but let's also update the flag help:

| - Source location: src=regex
| The regex is evaluated against the full source path.

Here's the Scala 2 version: https://github.com/scala/scala/blob/8073bce6dd787d96b627a05438ca9199961358b4/src/compiler/scala/tools/nsc/settings/Warnings.scala#L58-L62

It might have to wait for 2.10, @Gedochao? The change is that -Wconf:src=bar/.*\\.scala will now only match files in a folder named bar, but not in a folder named foobar.

touch up the Scala 2 implementation

That's not needed IMO.

@Gedochao
Copy link
Contributor

Gedochao commented Jan 8, 2026

It might have to wait for 2.10, @Gedochao?

You mean 3.10.
Yeah, I think so. Will label/milestone accordingly.

@Gedochao Gedochao added the needs-minor-release This PR cannot be merged until the next minor release label Jan 8, 2026
@Gedochao Gedochao added this to the 3.10.0 milestone Jan 8, 2026
@mbland mbland force-pushed the issue/24771-wconf-src-fix-windows-path-conversion branch from d1c809b to 857941d Compare January 8, 2026 16:45
Adds `^` and `$` anchors to the `-Wconf:src` regex to ensure it matches
entire path segments by default. Mostly replicates the Scala 2 `src`
filter anchoring logic except for the `rootDir` bit, which Scala 3
doesn't support:

- https://github.com/mbland/scala/blob/v2.13.18/src/compiler/scala/tools/nsc/Reporting.scala#L862-L875
- https://docs.scala-lang.org/scala3/guides/migration/options-lookup.html

Applies the new `anchored` function after parsing the `-Wconf:src`
argument as a regex first. This guards against pathological cases of
invalid patterns that may become valid after anchoring, such as `\`
becoming `/\$`. (One of the existing test cases covers this specific
invalid regex case.)

Adds new test cases, including cases to validate the existing behavior
of normalizing paths without resolving symlinks. This is to help ensure
that the Scala 2 issue from scala/bug#13145 (which scala/scala#11192
resolves) doesn't ever appear.

Also extracts the `diagnosticWarning`, `virtualFile`, and `plainFile`
helper methods to reduce duplication between new and existing test
cases.
@mbland mbland force-pushed the issue/24771-wconf-src-fix-windows-path-conversion branch from 857941d to a3eaa64 Compare January 8, 2026 16:50
@mbland mbland changed the title Fix -Wconf:src Windows path conversion Update -Wconf:src to match Scala 2 behavior Jan 8, 2026
@mbland
Copy link
Author

mbland commented Jan 8, 2026

@lrytz I've updated the flag help (using most of the same text from Scala 2), squashed the commits with an updated message, updated the pull request title and description accordingly, and rebased the branch on the latest main. While I was at it, I extracted a couple more helper methods in the test class, virtualFile and plainFile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-minor-release This PR cannot be merged until the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

-Wconf:src source path conversion to URI breaks Windows paths

4 participants