swiftlang / vscode-swift

Visual Studio Code Extension for Swift
https://marketplace.visualstudio.com/items?itemName=sswg.swift-lang
Apache License 2.0
755 stars 54 forks source link

Swift terminal profile #914

Closed award999 closed 4 months ago

award999 commented 4 months ago

Is your feature request related to a problem? Please describe. I've made the mistake a few times where I have select a toolchain in vscode and it has been built for via tasks. I then go to the integrated vscode terminal to issue to run some swift command that is a different version of swift.

Describe the solution you'd like You can provide different terminal profiles https://code.visualstudio.com/api/references/vscode-api#TerminalProfileProvider so we could provide a Swift terminal profile with selected toolchain in the PATH so that user can select this swift profile in settings, for example terminal.integrated.defaultProfile.osx

Describe alternatives you've considered N/A

Additional context N/A

matthewbastien commented 4 months ago

There's also the option of just adding to the terminal environment directly using the ExtensionContext:

context.environmentVariableCollection.clear();
context.environmentVariableCollection.prepend(
    "PATH",
    "/path/to/usr/bin:",
    { applyAtShellIntegration: true }
);
for (const variable in configuration.swiftEnvironmentVariables) {
    context.environmentVariableCollection.replace(
        variable,
        configuration.swiftEnvironmentVariables[variable],
        { applyAtShellIntegration: true }
    );
}

Using this approach the user will have the environment automatically populated in their VS Code terminal without having to launch a special profile. The only minor annoyance being that modifying certain variables (e.g. PATH) triggers a warning before the terminal will be updated:

Screenshot 2024-06-27 at 2 30 02 PM
adam-fowler commented 4 months ago

@matthewbastien so that appears every time you start up a terminal window or only for existing terminal windows?

matthewbastien commented 4 months ago

@adam-fowler the warning will only appear for existing terminal windows that need to be reloaded to capture new environment variables.

award999 commented 4 months ago

@matthewbastien if I disable swift.enableTerminalEnvironment and then create a new terminal with the swift profile, swift.swiftEnvironmentVariables values are added but which swift still indicates /usr/bin/swift is being used not the selected toolchain path.

award999 commented 4 months ago

Verified with 8d6f68f