Skip to content

Conversation

@philwalk
Copy link
Contributor

The following changes are made when calling the GraalVM compiler:

  • modified availableDriveLetter() to exclude existing SUBSTed drive letters
  • replaced the try/finally block for doing cleanup with a java shutdown hook
  • cleanup now includes restoring the original code page as well as removing the SUBSTed drive letter
  • the code for calling Windows command lines inside the shutdown hook doesn't use os.proc()

The shutdown hook runs on JVM shutdown, including Ctrl-C and is more reliable than a finally block for cleanup.
Because os.proc itself creates a shutdown hook, it isn't used inside the cleanup method.

For readability, (2 Windows command lines are called inside the cleanup method) a new method was created:

// execute a fire-and-forget windows command without using os.proc during shutdown.
private def exec(cmd: Seq[String], logger: Logger, desc: String): Unit

Although the code page is restored by the run-native-image.bat script (see #3981), it is vulnerable to interruption by Ctrl-C.

Copy link
Contributor

@Gedochao Gedochao left a comment

Choose a reason for hiding this comment

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

any chance for us to have an integration test for this?
While the solution looks good to me, I think it may be fragile without a test.

@philwalk
Copy link
Contributor Author

any chance for us to have an integration test for this?

Working on it.

A unit test to verify that NativeImage.availableDriveLetter doesn't return an already aliased drive would be fast and simple,
but it isn't obvious where to refactor the availableDriveLetter method so it's visible to both modules scala.cli.packaging and scala.cli.integration. This would me a much faster alternative to the first integration test below.

Integration tests to verify nextAvailableDrive letter avoids already aliased letters.

  • alias the nextAvailableDriveLetter (sufficient to cause a crash in earlier versions of code)
  • launch a native-image compile
  • verify it didn't crash

Integration test to verify the native-image interrupted cleanup:

  • set code page to 65001
  • launch a native-image compile
  • interrupt the compile (after the code page has changed to 437)
  • verify it didn't leave behind aliased drive letters
  • verify the code page was restored to 65001

Copy link
Contributor

@Gedochao Gedochao left a comment

Choose a reason for hiding this comment

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

LGTM

@philwalk philwalk marked this pull request as draft December 16, 2025 20:50
@philwalk
Copy link
Contributor Author

There's a problem with the shutdown hook that shows up in manual testing so I'm converting it to a draft while I work on it.

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.

native-image compile fails due to a residual SUBST pointing to Coursier‑managed GraalVM ZIP

2 participants