Skip to content

Fixing up SDK behavior #293

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

Merged
merged 7 commits into from
May 4, 2022
Merged
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
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Version 0.5.0 of vscode-swift now requires v1.65.0 of Visual Studio Code
- Experimental background compilation option. Whenever you save a file it will instigate a build task. This is currently defaulted to off.
- Setting to set environment variables while running tests.
- Setting to output more detailed diagnostics to Swift output pane.
- Setting to set SDK folder (supporting non-standard SDK layout on Windows and custom SDKs).
- Setting to set additional runtime path (supporting non-standard SDK layout on Windows).
- Setting to set SDK folder (supporting custom SDKs).
- Setting to set additional runtime path (supporting non-standard installation on Windows).
- More informative error messaging when Swift Package fails to load.

### Changed
Expand All @@ -37,7 +37,7 @@ Version 0.5.0 of vscode-swift now requires v1.65.0 of Visual Studio Code
- Windows: Test Explorer messaging when nothing is built.
- Windows: Launching of tests
- Windows: Use Swift LLDB to improve debugging experience.
- Centos7: Fix the code finding `swift`.
- CentOS7: Fix the code finding `swift`.

### Removed

Expand Down
10 changes: 3 additions & 7 deletions src/SwiftPackage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,9 @@ export class SwiftPackage implements PackageContents {
*/
static async loadPackage(folder: vscode.Uri): Promise<SwiftPackageState> {
try {
let { stdout } = await execSwift(
["package", "describe", "--type", "json"],
{
cwd: folder.fsPath,
},
true
);
let { stdout } = await execSwift(["package", "describe", "--type", "json"], {
cwd: folder.fsPath,
});
// remove lines from `swift package describe` until we find a "{"
while (!stdout.startsWith("{")) {
const firstNewLine = stdout.indexOf("\n");
Expand Down
1 change: 0 additions & 1 deletion src/TestExplorer/TestExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ export class TestExplorer {
{
cwd: this.folderContext.folder.fsPath,
},
true,
this.folderContext
);

Expand Down
1 change: 0 additions & 1 deletion src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ async function uneditFolderDependency(
{
cwd: folder.folder.fsPath,
},
true,
folder
);
ctx.fireEvent(folder, FolderEvent.resolvedUpdated);
Expand Down
21 changes: 14 additions & 7 deletions src/toolchain/toolchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class SwiftToolchain {
targetInfo,
swiftVersion,
runtimePath,
defaultSDK
customSDK ?? defaultSDK
);
return new SwiftToolchain(
toolchainPath,
Expand Down Expand Up @@ -182,8 +182,10 @@ export class SwiftToolchain {
}

/**
* @param targetInfo swift target info
* @param swiftVersion parsed swift version
* @param runtimePath path to Swift runtime
* @param sdkroot path to default SDK
* @param sdkroot path to swift SDK
* @returns path to folder where xctest can be found
*/
private static async getXCTestPath(
Expand All @@ -198,17 +200,22 @@ export class SwiftToolchain {
return path.join(stdout.trimEnd(), "usr", "bin");
}
case "win32": {
// look up runtime library directory for XCTest alternatively
const fallbackPath =
runtimePath !== undefined &&
(await pathExists(path.join(runtimePath, "XCTest.dll")))
? runtimePath
: undefined;
if (!sdkroot) {
return undefined;
return fallbackPath;
}
const platformPath = path.dirname(path.dirname(path.dirname(sdkroot)));
const platformManifest = path.join(platformPath, "Info.plist");
if ((await pathExists(platformManifest)) !== true) {
// look up runtime library directory for XCTest
if (runtimePath && (await pathExists(path.join(runtimePath, "XCTest.dll")))) {
return runtimePath;
if (fallbackPath) {
return fallbackPath;
}
await vscode.window.showWarningMessage(
vscode.window.showWarningMessage(
"XCTest not found due to non-standardized library layout. Tests explorer won't work as expected."
);
return undefined;
Expand Down
34 changes: 19 additions & 15 deletions src/utilities/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,10 @@ export async function execFileStreamOutput(
export async function execSwift(
args: string[],
options: cp.ExecFileOptions = {},
setSDKFlags = false,
folderContext?: FolderContext
): Promise<{ stdout: string; stderr: string }> {
const swift = getSwiftExecutable();
if (setSDKFlags) {
args = withSwiftSDKFlags(args);
}
args = withSwiftSDKFlags(args);
return await execFile(swift, args, options, folderContext);
}

Expand All @@ -158,23 +155,30 @@ export async function execSwift(
* @param args original commandline arguments
*/
export function withSwiftSDKFlags(args: string[]): string[] {
switch (args.length > 0 ? args[0] : null) {
case "package": {
// swift-package requires SDK flags to be placed before subcommand options
// eg. ["package", "describe", "--type", "json"] should be turned into
// ["package", "describe", "--sdk", "/path/to/sdk", "--type", "json"]
if (args.length <= 2) {
return args.concat(swiftpmSDKFlags());
switch (args[0]) {
case "package":
switch (args[1]) {
case "dump-symbol-graph":
case "diagnose-api-breaking-changes": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any harm in including the --sdk command line parameter even when it isn't needed. What's to say another package command requires the sdk.

Copy link
Contributor Author

@stevapple stevapple May 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to add them explicitly (since they're part of SwiftPM release). Adding unnecessary SDK flags has no effect I guess, but it will make the command line long and confusing (especially when the destination flags accumulate).

// These two tools require building the package, so SDK
// flags are needed. Destination control flags are
// required to be placed before subcommand options.
const subcommand = args.splice(0, 2);
return [...subcommand, ...swiftpmSDKFlags(), ...args];
}
default:
// Other swift-package subcommands operate on the host,
// so it doesn't need to know about the destination.
return args;
}
const subcommand = args.splice(0, 2);
return [...subcommand, ...swiftpmSDKFlags(), ...args];
}
case "build":
case "run":
case "test":
return args.concat(swiftpmSDKFlags());
default:
return args.concat(swiftDriverSDKFlags());
// We're not going to call the Swift compiler directly for cross-compiling
// and the destination settings are package-only, so do nothing here.
return args;
}
}

Expand Down