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

Fix Location macro to be hermetic #823

Merged
merged 5 commits into from
Sep 11, 2024
Merged

Conversation

eed3si9n
Copy link
Contributor

@eed3si9n eed3si9n commented Sep 10, 2024

Fixes #382

Problem

The Location macro currently captures the absolute path of the source file, which is not hermetic, and won't cache correctly on remote cached build tools such as Bazel, Gradle, and (in development) sbt 2.x.

$ jardiff target/out/jvm/scala-3.4.2/inctest/test-backend/ ../inctest2/target/out/jvm/scala-3.4.2/inctest/test-backend/
diff --git a/example/CowTest.class.asm b/example/CowTest.class.asm
index 1b3911e..91fd219 100644
--- a/example/CowTest.class.asm
+++ b/example/CowTest.class.asm
@@ -56,7 +56,7 @@
     ]
     NEW munit/Location
     DUP
-    LDC "/Users/xxx/work/inctest/src/test/scala/example/CowTest.scala"
+    LDC "/Users/xxx/work/inctest2/src/test/scala/example/CowTest.scala"
     BIPUSH 6
     INVOKESPECIAL munit/Location.<init> (Ljava/lang/String;I)V
     INVOKEVIRTUAL example/CowTest.assert (Lscala/Function0;Lscala/Function0;Lmunit/Location;)V

Solution

This captures the relative path from the working directory instead.

**Problem**
The Locaiton macro currently captures the absolute path of the source file,
which is not hermetic, and won't cache correctly on remote cached build tools
such as Bazel, Gradle, and (in development) sbt 2.x.

**Solution**
This captures the relative path from the working directory instead.
val p = Paths.get(cwd).resolve(path)
if (Files.exists(p)) p
else if (max < 1) sys.error(s"$path was not found")
else if (cwd.contains("\\")) findPath(cwd.split("\\").dropRight(1).mkString("\\"), path, max - 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like some of the tests are failing on Windows, would you be able to take a look?

Copy link
Contributor

Choose a reason for hiding this comment

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

Scalafmt and Scalafix are having an issue, too

Copy link
Contributor Author

@eed3si9n eed3si9n Sep 10, 2024

Choose a reason for hiding this comment

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

I guess it's failing because the expected value contains / and on Windows it's returning \.

if (Files.exists(p)) p
else if (max < 1) sys.error(s"$path was not found")
else if (cwd.contains("\\"))
findPath(cwd.split("\\").dropRight(1).mkString("\\"), path, max - 1)
else findPath(cwd.split("/").dropRight(1).mkString("/"), path, max - 1)
findPath(getParentPath(cwd, "\\"), path, max - 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about using https://docs.oracle.com/javase/8/docs/api/java/io/File.html#separator instead of looking at the current path ? Or would this only work on the JVM ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yea, for native and JS it might be simpler to just look for \.

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.

LGTM!

@tgodzik tgodzik merged commit 262024d into scalameta:main Sep 11, 2024
9 checks passed
@tgodzik
Copy link
Contributor

tgodzik commented Sep 11, 2024

Let me know if you need a release right away.

@eed3si9n eed3si9n deleted the wip/location branch September 11, 2024 12:42
@eed3si9n
Copy link
Contributor Author

It would be great if could get a release this week or next. Thanks!

@tgodzik
Copy link
Contributor

tgodzik commented Sep 13, 2024

It's running! https://github.com/scalameta/munit/actions/runs/10847736559

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.

munit.Location incompatible with Gradle build caching and Distributed Testing
3 participants