com-lihaoyi / os-lib

OS-Lib is a simple, flexible, high-performance Scala interface to common OS filesystem and subprocess APIs
Other
672 stars 63 forks source link

`os.root.baseName` throws an exception #276

Closed philwalk closed 2 months ago

philwalk commented 2 months ago

A new scala-cli bug is caused by os.root.baseName ~being an empty string~ throwing an exception.

https://github.com/VirtusLab/scala-cli/issues/2954 If a scala-cli script resides in the root directory, it crashes with an error message:

Error: os.PathError$LastOnEmptyPath: empty path has no last segment

Although this could be avoided in client code, it would require any os.Path object to be tested before referencing baseName.

One way to avoid the exception would be to redefine os.root.baseName as the empty String. However, the jvm defines the empty String as pwd, so if os.root.baseName is also the empty String, that would make it equal to pwd, which is a problem.

Shell environments (mostly) treat the root directory as its own parent, which is much easier to work with. For example, /usr/bin/basename takes this approach. The expression basename / resolves to /.

Defining os.root.baseName as s"$driveRoot/" resolves the scala-cli bug.

This is the approach taken by PR #277

philwalk commented 2 months ago

Clarification: the original symptom for the scala-cli bug was a LastOnEmptyPath error.