Repository: microsoft/terminal (language: cpp)

Issue:
startingDirectory and splitMode ignored in settings file
### Windows Terminal version

1.14.1962.0

### Windows build number

10.0.19042.0

### Other Software

_No response_

### Steps to reproduce

```
{
    "$help": "https://aka.ms/terminal-documentation",
    "$schema": "https://aka.ms/terminal-profiles-schema",
    "actions": 
    [
        {
            "command": 
            {
                "action": "copy",
                "singleLine": false
            },
            "keys": "ctrl+c"
        },
        {
            "command": "paste",
            "keys": "ctrl+v"
        },
        {
            "command": "find",
            "keys": "ctrl+shift+f"
        },
        {
            "command": 
            {
                "action": "splitPane",
                "split": "down",
                "splitMode": "duplicate"
            },
            "keys": "alt+shift+minus"
        }
    ],
    "copyFormatting": "none",
    "copyOnSelect": false,
    "defaultProfile": "{dd06fdc4-e3c7-404a-ba9c-ead0245d608b}",
    "profiles": 
    {
        "defaults": {
			
		},
        "list": 
        [
            {
                "commandline": "%SystemRoot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
                "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
                "hidden": false,
                "name": "Windows PowerShell"
            },
            {
                "commandline": "%SystemRoot%\\System32\\cmd.exe",
                "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
                "hidden": false,
                "name": "Command Prompt"
            },
            {
                "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}",
                "hidden": false,
                "name": "PowerShell",
                "source": "Windows.Terminal.PowershellCore"
            },
            {
                "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
                "hidden": false,
                "name": "Azure Cloud Shell",
                "source": "Windows.Terminal.Azure"
            },
            {
                "commandline": "pwsh.exe -ExecutionPolicy ByPass -NoExit -Command \"& C:\\Users\\amine.aboufirass\\miniconda3\\shell\\condabin\\conda-hook.ps1 ; conda activate C:\\Users\\amine.aboufirass\\miniconda3\"",
                "guid": "{db6044f2-9daa-458b-afd6-20cbd1db506d}",
                "hidden": false,
                "name": "Anaconda Powershell Prompt"
            },
            {
                "commandline": "\"C:\\Program Files\\PowerShell\\7\\pwsh.exe\" -noe -c \"&{Import-Module \"\"\"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Professional\\Common7\\Tools\\Microsoft.VisualStudio.DevShell.dll\"\"\"; Enter-VsDevShell f47eb35f}\"",
                "guid": "{dd06fdc4-e3c7-404a-ba9c-ead0245d608b}",
                "hidden": false,
                "name": "Developer Command Prompt VS2019",
				"startDirectory": "D:/Dev"
            }
        ]
    },
    "schemes":  [...]
}
```

- The opened default prompt (Developer Command Prompt VS2019) Does not open in the directory listed under `startDirectory`
- When attempting to split a pane horizontally using shift+alt+- the new pane does not replicate the same directory, unless it is the home directory

### Expected Behavior

I expect `splitMode="duplicate` to start any new panes in the directory of the parent pane.
I expect `startingDirectory="D:/Dev"` to open the profile in `D:/Dev`

### Actual Behavior

`splitMode="duplicate` to starts any new panes in home directory regardless of which directory the parent pane is in
`startingDirectory="D:/Dev"` does not affect the default directory when opening the terminal


Discussion / hints:
`startingDirectory` or `startDirectory` in YOUR settings.json?
@237dmitry  I tried both. Neither worked.
Firstly, yea it's [`startingDirectory`](https://docs.microsoft.com/en-us/windows/terminal/customize-settings/profile-general#starting-directory).

Second, did you [configure your shell](https://docs.microsoft.com/en-us/windows/terminal/tutorials/new-tab-same-directory) to actually tell the Terminal about it's CWD/?
@zadjii-msft I added the following to my Powershell profile based on the link you provided:

```
function prompt {
  $loc = $($executionContext.SessionState.Path.CurrentLocation);
  $out = "PS $loc$('>' * ($nestedPromptLevel + 1)) ";
  $out += "$([char]27)]9;9;`"$loc`"$([char]27)\"
  return $out
}
```

Then I double checked that my settings included `startingDirectory`, which they were.

The behavior unfortunately persists. I still have the same problem.
Hmm. Maybe the cwd sequence is getting lost. 

Can you try opening this profile with the [debug tap](https://github.com/microsoft/terminal/wiki/Enabling-the-debug-tap) to get a trace of all the input and output/? Once it's open, `cd` once, just to make sure that CWD changes propagate out. Send us a screenshot and we might be able to figure out what the Terminal thinks it's getting here.

<hr>
Alternatively, maybe it's the `"alt+shift+minus"` that's tripping up the keybindings. Those ones are sometimes a little wonky, esp on other keyboard layouts. As a sanity check, try binding that to something like, `f1` instead. 
Here's the output from the debug tap:

![image](https://user-images.githubusercontent.com/24267968/184304413-71289833-7393-400c-afd9-6b50b2b087b7.png)

Using "f1" instead of "alt+shift+minus" in the keybinding does not make a difference.
I changed the relevant part in `settings.json` to         

```
{
            "command": 
            {
                "action": "splitPane",
                "split": "horizontal",
                "splitMode": "duplicate"
            },
            "keys": "shift+alt+-"
        }
}
```

And now it seems to duplicate the profile and CWD in all profiles except for the Visual Studio Developer command prompt. That seems to indicate that Windows Terminal works fine, and there's something about the way I've set up the Visual Studio profile which causes it to behave this way. Closing.
Huh. Interesting! I know there have been issues with that in the past with `ubuntu.exe` as well. Maybe  `Enter-VsDevShell` always switches the CWD to `%USERPROFILE%\source\repos`, regardless of the starting directory. 

Glad you (sorta) figured it out!
Sorry, I have to reopen this. I tried using the same settings file on another machine and got the same unwanted behavior. So I'm leaning more towards the idea that this is definitely Windows Terminal related.
> got the same unwanted behavior

Like, got the same behavior with cmd/powershell? I thought we had debugged this down to an issue with `Enter-VsDevShell`/?
@zadjii-msft  We hadn't debugged anything, really. I submitted the debug tap like you asked and then closed the issue before you had a chance to respond when I (incorrectly) concluded my issue was resolved.

I'm not sure what `Enter-VsDevShell` is, but I don't think we had gotten far enough to determine the root cause of this issue. I would kindly refer you back to the debug tap I submitted a couple of days ago.
I'm sorry for the radio silence here! Thanks for your patience.

Based on the symptoms and your configuration here, it seems like there are two issues:

1. If you have not configured cmd/powershell to "tell" terminal about their starting directory, duplicate doesn't work properly. This is an unfortunate but [documented](https://learn.microsoft.com/en-us/windows/terminal/tutorials/new-tab-same-directory) consequence of how processes on Windows work.
1. _the VS dev shells_ are impacted explicitly, even if you follow the above docs

For 1, the docs reign supreme. For 2, however... every VS dev shell profile is configured to use a Visual Studio command `Enter-VsDevShell`. That command(let) explicitly overrides any starting directory, which is somewhat frustrating. You can fix this by adding `-SkipAutomaticLocation` to the commandline for any VS Dev Shell profile. We should document or fix this.

<img width="304" alt="image" src="https://user-images.githubusercontent.com/189190/222282117-a556638d-d5ae-471f-90a7-53d9b07ee2bc.png">

Does that help at all?
@DHowett I see, so this appears to be specific for profiles that use `VsDevShell`. Yes, in my case adding `-SkipAutomaticLocation` does help. When I split the pane it now keeps the same base folder as the one specified in my original pane. Thanks.
Thanks for confirming!

Personally, I'd love to add that for everyone . . . @heaths, how integral to the continuity of "dev shell" is that it ignore the user's starting directory? I know that the start menu shortcuts work one way, but perhaps we can change that contract a bit?
I doubt it's needed. I imagine it was probably maintained for legacy reasons, but since env vars like `PATH` are updated I see little reason to maintain it i.e., just blindly add `-SkipAutomaticLocation` to each profile going forward. Let me track down an owner for the scripts and see if they'll comment otherwise.
@heaths did you have any luck?
@trippwill indicated in email with @DHowett and I that it should be safe to add. It's been available since the PowerShell dev prompt was added, which I seem to recall this code assumes already.

Fix patch:
diff --git a/.github/actions/spelling/expect/expect.txt b/.github/actions/spelling/expect/expect.txt
index cc86eb87236..59d0de937e0 100644
--- a/.github/actions/spelling/expect/expect.txt
+++ b/.github/actions/spelling/expect/expect.txt
@@ -1764,6 +1764,7 @@ srv
 srvinit
 srvpipe
 ssa
+startdir
 STARTF
 STARTUPINFO
 STARTUPINFOEX
diff --git a/src/cascadia/TerminalSettingsModel/VsDevCmdGenerator.cpp b/src/cascadia/TerminalSettingsModel/VsDevCmdGenerator.cpp
index 2b3f04e277d..f4e5dd0298a 100644
--- a/src/cascadia/TerminalSettingsModel/VsDevCmdGenerator.cpp
+++ b/src/cascadia/TerminalSettingsModel/VsDevCmdGenerator.cpp
@@ -42,6 +42,9 @@ std::wstring VsDevCmdGenerator::GetProfileCommandLine(const VsSetupConfiguration
     commandLine.reserve(256);
     commandLine.append(LR"(cmd.exe /k ")");
     commandLine.append(GetDevCmdScriptPath(instance));
+    // The "-startdir" parameter will prevent "vsdevcmd" from automatically
+    // setting the shell path so the path in the profile will be used instead.
+    commandLine.append(LR"(" -startdir=none)");
 #if defined(_M_ARM64)
     commandLine.append(LR"(" -arch=arm64 -host_arch=x64)");
 #elif defined(_M_AMD64)


Test patch: