# File::getCurrentWorkingDirectory doesn't work when CWD is set from Xcode scheme options (OSX / posix)

**URL:** <https://forum.juce.com/t/file-getcurrentworkingdirectory-doesnt-work-when-cwd-is-set-from-xcode-scheme-options-osx-posix/49669>\
**Category:** General JUCE discussion\
**Created:** [January 14, 2022, 8:15am UTC](https://forum.juce.com/t/file-getcurrentworkingdirectory-doesnt-work-when-cwd-is-set-from-xcode-scheme-options-osx-posix/49669 "2022-01-14T08:15:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kunitoki](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kunitoki/32/15_2.png) [@kunitoki](https://forum.juce.com/u/kunitoki)\
**Post date:** [January 14, 2022, 8:15am UTC](https://forum.juce.com/t/file-getcurrentworkingdirectory-doesnt-work-when-cwd-is-set-from-xcode-scheme-options-osx-posix/49669/1 "2022-01-14T08:15:03Z")

</div>

Xcode allows to set the CWD from its scheme options, but it will just set the PWD environment variable, and if you set a custom directory there, and execute `File::getCurrentWorkingDirectory` it will return an empty `File()` because `getcwd` will return `nullptr`.

I’ve extended the function (for posix) to take into account the `PWD` env variable when `getcwd` fails:

```
File File::getCurrentWorkingDirectory()
{
    HeapBlock<char> heapBuffer;

    char localBuffer[1024];
    auto cwd = getcwd (localBuffer, sizeof (localBuffer) - 1);
    size_t bufferSize = 4096;

    while (cwd == nullptr && errno == ERANGE)
    {
        heapBuffer.malloc (bufferSize);
        cwd = getcwd (heapBuffer, bufferSize - 1);
        bufferSize += 1024;
    }

    if (cwd == nullptr)
        return File (CharPointer_UTF8 (getenv ("PWD")));

    return File (CharPointer_UTF8 (cwd));
}

```

Maybe this is something that could be fixed ?

---

<div class="post-metadata">

**Author:** ![t0m](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/t0m/32/16413_2.png) [@t0m](https://forum.juce.com/u/t0m)\
**Post date:** [January 20, 2022, 12:27pm UTC](https://forum.juce.com/t/file-getcurrentworkingdirectory-doesnt-work-when-cwd-is-set-from-xcode-scheme-options-osx-posix/49669/2 "2022-01-20T12:27:28Z")

</div>

There are cases where `getcwd` returning `nullptr` is the correct thing to do, like if the directory you are currently in has been deleted. Silently falling back to `PWD` after a long chain of `chdir`s would be very surprising.

Unfortunately I can’t think of an automatic way around this.

---

<div class="post-metadata">

**Author:** ![kunitoki](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kunitoki/32/15_2.png) [@kunitoki](https://forum.juce.com/u/kunitoki)\
**Post date:** [January 21, 2022, 5:55am UTC](https://forum.juce.com/t/file-getcurrentworkingdirectory-doesnt-work-when-cwd-is-set-from-xcode-scheme-options-osx-posix/49669/3 "2022-01-21T05:55:47Z")

</div>

Yeah indeed, i’m experiencing a weird behaviour in Xcode where you override the CWD in the options for the binary, then when the app starts, getpwd returns nullptr and chdir consistently fails, so it’s impossible to get or set the CWD, and that one is only available in the PWD env variable. By default when you launch debugging from xcode without setting the cwd, the apps is set to start from /, so i wanted to change that to be able to be executed from a writable directory at least. Not sure how to workaround it tho

---

<div class="post-metadata">

**Author:** ![t0m](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/t0m/32/16413_2.png) [@t0m](https://forum.juce.com/u/t0m)\
**Post date:** [January 21, 2022, 8:42am UTC](https://forum.juce.com/t/file-getcurrentworkingdirectory-doesnt-work-when-cwd-is-set-from-xcode-scheme-options-osx-posix/49669/4 "2022-01-21T08:42:44Z")

</div>

For this specific situation could you do `if (getCurrentWorkingDirectory() == {}) setCurrentWorkingDirectory($PWD);` on initialisation of your app?

---

<div class="post-metadata">

**Author:** ![kunitoki](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kunitoki/32/15_2.png) [@kunitoki](https://forum.juce.com/u/kunitoki)\
**Post date:** [January 21, 2022, 5:35pm UTC](https://forum.juce.com/t/file-getcurrentworkingdirectory-doesnt-work-when-cwd-is-set-from-xcode-scheme-options-osx-posix/49669/5 "2022-01-21T17:35:56Z")

</div>

It doesn’t work if i have PWD set from Xcode cwd option, as setCurrentWorkingDirectory uses chdir internally and that fails.

Maybe i can use a separate env var and not involve using the xcode option at all.

---

<div class="post-metadata">

**Author:** ![jbingie](https://avatars.discourse-cdn.com/v4/letter/j/85f322/32.png) [@jbingie](https://forum.juce.com/u/jbingie)\
**Post date:** [March 11, 2023, 4:55am UTC](https://forum.juce.com/t/file-getcurrentworkingdirectory-doesnt-work-when-cwd-is-set-from-xcode-scheme-options-osx-posix/49669/6 "2023-03-11T04:55:17Z")

</div>

I’m having this problem still. `getCurrentWorkingDirectory` doesn’t return the appropriate location. How can I use your snippet to remedy this?

---

<div class="post-metadata">

**Author:** ![kunitoki](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/kunitoki/32/15_2.png) [@kunitoki](https://forum.juce.com/u/kunitoki)\
**Post date:** [March 11, 2023, 8:48am UTC](https://forum.juce.com/t/file-getcurrentworkingdirectory-doesnt-work-when-cwd-is-set-from-xcode-scheme-options-osx-posix/49669/7 "2023-03-11T08:48:28Z")

</div>

I opted for a specific environment variable, so my app can test it and set the working directory to that variable instead of using Xcode current directory, because that is preventing `getCurrentWorkingDirectory` and `setAsCurrentWorkingDirectory` to work:

```
    juce::File currentWorkingDirectory = juce::File(juce::SystemStats::getEnvironmentVariable("MYAPP_PWD", {}).trim());

    if (currentWorkingDirectory.isDirectory())
        currentWorkingDirectory.setAsCurrentWorkingDirectory();

```

Hope it helps
