Repository: microsoft/terminal (language: cpp)

Issue:
Set environment variable `WT_FOLDER_PATH` to the folder path that contains `WindowsTerminal.exe`
<!-- 
🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨

I ACKNOWLEDGE THE FOLLOWING BEFORE PROCEEDING:
1. If I delete this entire template and go my own path, the core team may close my issue without further explanation or engagement.
2. If I list multiple bugs/concerns in this one issue, the core team may close my issue without further explanation or engagement.
3. If I write an issue that has many duplicates, the core team may close my issue without further explanation or engagement (and without necessarily spending time to find the exact duplicate ID number).
4. If I leave the title incomplete when filing the issue, the core team may close my issue without further explanation or engagement.
5. If I file something completely blank in the body, the core team may close my issue without further explanation or engagement.

All good? Then proceed!
-->

# Description of the new feature/enhancement

<!-- 
A clear and concise description of what the problem is that the new feature would solve.
Describe why and how a user would use this new functionality (if applicable).
-->

Set an environment variable `WT_FOLDER_PATH` to the folder path that contains `WindowsTerminal.exe`.  This variable should be set when Windows Terminal launches before the profile is launched.

This would be useful for users that run Windows Terminal in portable mode so Windows terminal profiles can be configured with paths relative to the folder wher WIndows Terminal is installed as shown below:

![image](https://github.com/microsoft/terminal/assets/7318053/f77f8e2c-7c8e-48d1-a4ce-81038c653468)

The normal user profile is configured as shown below and works perfectly because the `%CMDER_ROOT%` env variable was set dynamically before launching Windows Terminal.

![image](https://github.com/microsoft/terminal/assets/7318053/1bc3f646-e05d-4e69-8add-b8422a8cea90)

The above configuration does not work when launching as Administrator because the `%CMDER_ROOT%` env variable is not set globally on the system nor in the Administator's personal environment variables.  it produces the error below:

![image](https://github.com/microsoft/terminal/assets/7318053/ffd23d64-329c-4ae1-8e66-ebe36b563d4f)

For completeness sake the icon config being complained about in the error above is below:

![image](https://github.com/microsoft/terminal/assets/7318053/7b878169-edfc-4046-9ea6-453c91703b4a)

# Proposed technical implementation details (optional)

<!-- 
A clear and concise description of what you want to happen.
-->



Discussion / hints:
~This really falls under the same problem space as #13006 et al. We could add that, sure,~

Wait, no it doesn't. This is actually so simple. Set that variable _for terminal itself_. So that we can resolve things relative to ourself. brilliant. 

I think we were already tracking this in #15379 but I'll motion to use this as the tracking thread instead, since this has a real good idea. Thanks!

Fix patch:
diff --git a/src/cascadia/TerminalApp/AppLogic.cpp b/src/cascadia/TerminalApp/AppLogic.cpp
index 29c13fdb13f..0b3ada01efa 100644
--- a/src/cascadia/TerminalApp/AppLogic.cpp
+++ b/src/cascadia/TerminalApp/AppLogic.cpp
@@ -149,6 +149,11 @@ namespace winrt::TerminalApp::implementation
         _languageProfileNotifier = winrt::make_self<LanguageProfileNotifier>([this]() {
             _reloadSettings->Run();
         });
+
+        // Do this here, rather than at the top of main. This will prevent us from
+        // including this variable in the vars we serialize in the
+        // Remoting::CommandlineArgs up in HandleCommandlineArgs.
+        _setupFolderPathEnvVar();
     }
 
     // Method Description:
@@ -720,4 +725,14 @@ namespace winrt::TerminalApp::implementation
         return TerminalApp::ParseCommandlineResult{ winrt::to_hstring(_appArgs.GetExitMessage()), r };
     }
 
+    // Function Description
+    // * Adds a `WT_SETTINGS_DIR` env var to our own environment block, that
+    //   points at our settings directory. This allows portable installs to
+    //   refer to files in the portable install using %WT_SETTINGS_DIR%
+    void AppLogic::_setupFolderPathEnvVar()
+    {
+        std::wstring path{ CascadiaSettings::SettingsPath() };
+        auto folderPath = path.substr(0, path.find_last_of(L"\\"));
+        SetEnvironmentVariableW(L"WT_SETTINGS_DIR", folderPath.c_str());
+    }
 }
diff --git a/src/cascadia/TerminalApp/AppLogic.h b/src/cascadia/TerminalApp/AppLogic.h
index 4f735dc9b29..3c14a4bca41 100644
--- a/src/cascadia/TerminalApp/AppLogic.h
+++ b/src/cascadia/TerminalApp/AppLogic.h
@@ -112,6 +112,8 @@ namespace winrt::TerminalApp::implementation
         void _RegisterSettingsChange();
         fire_and_forget _DispatchReloadSettings();
 
+        void _setupFolderPathEnvVar();
+
 #ifdef UNIT_TESTING
         friend class TerminalAppLocalTests::CommandlineTest;
 #endif


Test patch: