Skip to content

Error converting relative paths to absolute paths in Windows if relative path has leading slash or backslash #201

@philwalk

Description

@philwalk

Version(s)

os-lib release 0.9.1

Describe the bug
The following idiom is intended to convert a relative path to an absolute path:

val abspath = Path(relativeDir, pwd)

On Windows, this idiom fails, due to there being two types of relative path, unlike on other platforms.

Windows paths are described here: windows-file-path-formats. A reference to what might be called rootRelative is here.

To Reproduce

The following code illustrates the problem with rootRelative paths in Windows.

Example code:

    val p1 = java.nio.file.Paths.get("/omg")
    printf("isAbsolute: %s\n", p1.isAbsolute)
    printf("%s\n", p1)
    printf("%s\n", os.Path("/omg"))

Output on Linux or OSX:

isAbsolute: true
/omg
/omg

On Windows:

isAbsolute: false
\omg
java.lang.IllegalArgumentException: requirement failed: \omg is not an absolute path
        at os.Path.<init>(Path.scala:474)
        at os.Path$.apply(Path.scala:426)
        at oslibChek$package$.main(oslibChek.sc:11)
        at oslibChek$package.main(oslibChek.sc)

Expected behaviour
A rootRelative path is supported by java.nio.file.Paths.get() but not by os.Path().

In Windows, calls to java.io.File#isAbsolute are insufficient to determine whether a path is relative to pwd.
A rootRelative path in Windows is relative to the current working drive (an immutable value captured at jvm startup).

There are 3 use-cases when creating a java.nio.file.Path object:

  • path is relative - interpret path as immediately below pwd
  • path is absolute - java.nio.file.Paths.get(path)
  • path is rootRelative - interpret path as relative to the current working drive.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions