Skip to content

Notify user if the xcode-selected Xcode doesn't match setting #1563

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export enum Commands {
COVER_ALL_TESTS = "swift.coverAllTests",
OPEN_MANIFEST = "swift.openManifest",
RESTART_LSP = "swift.restartLSPServer",
SELECT_TOOLCHAIN = "swift.selectToolchain",
}

/**
Expand Down
29 changes: 25 additions & 4 deletions src/toolchain/SelectedXcodeWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as vscode from "vscode";
import { SwiftOutputChannel } from "../ui/SwiftOutputChannel";
import { showReloadExtensionNotification } from "../ui/ReloadExtension";
import configuration from "../configuration";
import { removeToolchainPath, selectToolchain } from "../ui/ToolchainSelection";

export class SelectedXcodeWatcher implements vscode.Disposable {
private xcodePath: string | undefined;
Expand Down Expand Up @@ -66,20 +67,40 @@ export class SelectedXcodeWatcher implements vscode.Disposable {
*/
private async setup() {
this.xcodePath = await this.xcodeSymlink();
if (
this.xcodePath &&
configuration.path &&
!configuration.path.startsWith(this.xcodePath)
) {
this.xcodePath = undefined; // Notify user when initially launching that xcode changed since last session
}
this.interval = setInterval(async () => {
if (this.disposed) {
return clearInterval(this.interval);
}

const newXcodePath = await this.xcodeSymlink();
if (!configuration.path && newXcodePath && this.xcodePath !== newXcodePath) {
if (newXcodePath && this.xcodePath !== newXcodePath) {
this.outputChannel.appendLine(
`Selected Xcode changed from ${this.xcodePath} to ${newXcodePath}`
);
this.xcodePath = newXcodePath;
await showReloadExtensionNotification(
"The Swift Extension has detected a change in the selected Xcode. Please reload the extension to apply the changes."
);
if (!configuration.path) {
await showReloadExtensionNotification(
"The Swift Extension has detected a change in the selected Xcode. Please reload the extension to apply the changes."
);
} else {
const selected = await vscode.window.showWarningMessage(
'The Swift Extension has detected a change in the selected Xcode which does not match the value of your "swift.path" setting. Would you like to update your configured "swift.path" setting?',
"Remove From Settings",
"Select Toolchain"
);
if (selected === "Remove From Settings") {
await removeToolchainPath();
} else if (selected === "Select Toolchain") {
await selectToolchain();
}
}
}
}, this.checkIntervalMs);
}
Expand Down
13 changes: 9 additions & 4 deletions src/ui/ToolchainSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as path from "path";
import { showReloadExtensionNotification } from "./ReloadExtension";
import { SwiftToolchain } from "../toolchain/toolchain";
import configuration from "../configuration";
import { Commands } from "../commands";

/**
* Open the installation page on Swift.org
Expand All @@ -28,7 +29,7 @@ export async function downloadToolchain() {
"Select Toolchain"
);
if (selected === "Select Toolchain") {
await vscode.commands.executeCommand("swift.selectToolchain");
await selectToolchain();
}
}
}
Expand All @@ -43,7 +44,7 @@ export async function installSwiftly() {
"Select Toolchain"
);
if (selected === "Select Toolchain") {
await vscode.commands.executeCommand("swift.selectToolchain");
await selectToolchain();
}
}
}
Expand Down Expand Up @@ -87,10 +88,14 @@ export async function showToolchainError(): Promise<void> {
if (selected === "Remove From Settings") {
await removeToolchainPath();
} else if (selected === "Select Toolchain") {
await vscode.commands.executeCommand("swift.selectToolchain");
await selectToolchain();
}
}

export async function selectToolchain() {
await vscode.commands.executeCommand(Commands.SELECT_TOOLCHAIN);
}

/** A {@link vscode.QuickPickItem} that contains the path to an installed Swift toolchain */
type SwiftToolchainItem = PublicSwiftToolchainItem | XcodeToolchainItem;

Expand Down Expand Up @@ -351,7 +356,7 @@ async function showDeveloperDirQuickPick(xcodePaths: string[]): Promise<string |
/**
* Delete all set Swift path settings.
*/
async function removeToolchainPath() {
export async function removeToolchainPath() {
const swiftSettings = vscode.workspace.getConfiguration("swift");
const swiftEnvironmentSettings = swiftSettings.inspect("swiftEnvironmentVariables");
if (swiftEnvironmentSettings?.globalValue) {
Expand Down
55 changes: 52 additions & 3 deletions test/unit-tests/toolchain/SelectedXcodeWatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ import {
mockObject,
} from "../../MockUtils";
import configuration from "../../../src/configuration";
import { Commands } from "../../../src/commands";

suite("Selected Xcode Watcher", () => {
const mockedVSCodeWindow = mockGlobalObject(vscode, "window");
let mockOutputChannel: MockedObject<SwiftOutputChannel>;
const pathConfig = mockGlobalValue(configuration, "path");
const mockWorkspace = mockGlobalObject(vscode, "workspace");
const mockCommands = mockGlobalObject(vscode, "commands");
let mockSwiftConfig: MockedObject<vscode.WorkspaceConfiguration>;

setup(function () {
// Xcode only exists on macOS, so the SelectedXcodeWatcher is macOS-only.
Expand All @@ -42,6 +46,12 @@ suite("Selected Xcode Watcher", () => {
});

pathConfig.setValue("");

mockSwiftConfig = mockObject<vscode.WorkspaceConfiguration>({
inspect: mockFn(),
update: mockFn(),
});
mockWorkspace.getConfiguration.returns(instance(mockSwiftConfig));
});

async function run(symLinksOnCallback: (string | undefined)[]) {
Expand Down Expand Up @@ -84,11 +94,50 @@ suite("Selected Xcode Watcher", () => {
);
});

test("Ignores when path is explicitly set", async () => {
test("Warns that setting is out of date", async () => {
pathConfig.setValue("/path/to/swift/bin");

await run(["/foo", "/bar"]);
await run(["/path/to/swift/bin", "/foo", "/foo"]);

expect(mockedVSCodeWindow.showWarningMessage).to.have.not.been.called;
expect(mockedVSCodeWindow.showWarningMessage).to.have.been.calledOnceWithExactly(
'The Swift Extension has detected a change in the selected Xcode which does not match the value of your "swift.path" setting. Would you like to update your configured "swift.path" setting?',
"Remove From Settings",
"Select Toolchain"
);
});

test("Warns that setting is out of date on startup", async () => {
pathConfig.setValue("/path/to/swift/bin");

await run(["/foo", "/foo"]);

expect(mockedVSCodeWindow.showWarningMessage).to.have.been.calledOnceWithExactly(
'The Swift Extension has detected a change in the selected Xcode which does not match the value of your "swift.path" setting. Would you like to update your configured "swift.path" setting?',
"Remove From Settings",
"Select Toolchain"
);
});

test("Remove setting", async () => {
pathConfig.setValue("/path/to/swift/bin");

mockedVSCodeWindow.showWarningMessage.resolves("Remove From Settings" as any);

await run(["/foo", "/foo"]);

expect(mockSwiftConfig.update.args).to.deep.equal([
["path", undefined, vscode.ConfigurationTarget.Global],
["path", undefined, vscode.ConfigurationTarget.Workspace],
]);
});

test("Select toolchain", async () => {
pathConfig.setValue("/path/to/swift/bin");

mockedVSCodeWindow.showWarningMessage.resolves("Select Toolchain" as any);

await run(["/foo", "/foo"]);

expect(mockCommands.executeCommand).to.have.been.calledOnceWith(Commands.SELECT_TOOLCHAIN);
});
});