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

Unlock _CFGetCurrentDirectory #557

Merged
merged 1 commit into from
Jan 19, 2023

Conversation

sidepelican
Copy link

Motivation

The URL generated using relative path in URL(initWithFilePath:) will have a baseURL of nil.

print(URL(fileURLWithPath: ".").baseURL) // nil
print(URL(fileURLWithPath: ".").absoluteURL) // .

On platforms such as macOS and Linux, CWD is set to baseURL if relativeTo is empty .
It can be seen from the test that baseURL should be exist.
https://github.com/apple/swift-corelibs-foundation/blob/9bcec0584a704e7be374bd5a1efc94adc3ebde7c/Tests/Foundation/Tests/TestURL.swift#L158

I would like to fix this behavior so that baseURL does not become nil.

Cause

I think this is a historically reason. wasi-libc did not support getcwd first era. so the functions that using getcwd are disabled.
because of this, the partial feature of URL(fileURLWithPath: ".") is also disabled.

#if !TARGET_OS_WASI
CF_PRIVATE Boolean _CFGetCurrentDirectory(char *path, int maxlen) {
return getcwd(path, maxlen) != NULL;
}
#endif

Solve

Current wasi-libc has getcwd.
WebAssembly/wasi-libc#214

simply, unlock some #if and it works well.

print(URL(fileURLWithPath: ".").baseURL) // Optional(file:///)
print(URL(fileURLWithPath: ".").absoluteURL) // file:///

Copy link
Member

@kateinoigakukun kateinoigakukun left a comment

Choose a reason for hiding this comment

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

Seems legit to me 👍

@kateinoigakukun
Copy link
Member

I think other filesystem related APIs can be also unblocked on WASI 😉

@kateinoigakukun kateinoigakukun merged commit c0defdb into swiftwasm:swiftwasm Jan 19, 2023
@sidepelican sidepelican deleted the unlock_getcwd branch January 20, 2023 01:01
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.

2 participants