Skip to content

Commit b032adb

Browse files
authored
[Fix] Adding default sdk version incase config inspect returns null (#21449)
* Adding default sdk version incase config inspect returns null * get the fallback version from package.json during runtime * updating tests * adding fall back version * removing the parameter that can be accessed from constants directly
1 parent 2bfe915 commit b032adb

5 files changed

Lines changed: 80 additions & 17 deletions

File tree

extensions/sql-database-projects/src/controllers/projectController.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,7 @@ export class ProjectsController {
138138
}
139139

140140
const sqlProjectsService = await utils.getSqlProjectsService();
141-
const microsoftBuildSqlSDKStyleDefaultVersion = getMicrosoftBuildSqlVersion(
142-
constants.microsoftBuildSqlVersionKey,
143-
);
141+
const microsoftBuildSqlSDKStyleDefaultVersion = getMicrosoftBuildSqlVersion();
144142
const projectStyle = creationParams.sdkStyle
145143
? mssqlVscode.ProjectType.SdkStyle
146144
: mssqlVscode.ProjectType.LegacyStyle;

extensions/sql-database-projects/src/templates/templates.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,7 @@ export async function loadTemplates(templateFolderPath: string) {
8686
Promise.resolve(
8787
(newSdkSqlProjectTemplate = macroExpansion(
8888
await loadTemplate(templateFolderPath, "newSdkSqlProjectTemplate.xml"),
89-
new Map([
90-
[
91-
"MICROSOFT_BUILD_SQL_VERSION",
92-
getMicrosoftBuildSqlVersion(constants.microsoftBuildSqlVersionKey),
93-
],
94-
]),
89+
new Map([["MICROSOFT_BUILD_SQL_VERSION", getMicrosoftBuildSqlVersion()]]),
9590
)),
9691
),
9792
loadObjectTypeInfo(

extensions/sql-database-projects/src/tools/buildHelper.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ export class BuildHelper {
6969
"Microsoft.SqlServer.Server.dll",
7070
];
7171

72-
const sdkVersion = getMicrosoftBuildSqlVersion(constants.microsoftBuildSqlVersionKey);
73-
72+
const sdkVersion = getMicrosoftBuildSqlVersion();
7473
const microsoftBuildSqlDllLocation = path.join("tools", "net8.0");
7574
return this.ensureNugetAndFilesPresence(
7675
sdkName,

extensions/sql-database-projects/src/tools/netcoreTool.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
DotnetInstallationConfirmation,
1818
NetCoreSupportedVersionInstallationConfirmation,
1919
UpdateDotnetLocation,
20+
microsoftBuildSqlVersionKey,
2021
} from "../common/constants";
2122
import * as utils from "../common/utils";
2223
import { ShellCommandOptions, ShellExecutionHelper } from "./shellExecutionHelper";
@@ -32,6 +33,13 @@ export const macPlatform = "darwin";
3233
export const linuxPlatform = "linux";
3334
export const minSupportedNetCoreVersionForBuild = "8.0.0";
3435

36+
/**
37+
* Fallback version for Microsoft.Build.Sql when the setting is not configured or invalid.
38+
* NOTE: Keep this in sync with the default value in package.json:
39+
* sqlDatabaseProjects.microsoftBuildSqlVersion.default
40+
*/
41+
export const FALLBACK_MICROSOFT_BUILD_SQL_VERSION = "2.1.0";
42+
3543
export const enum netCoreInstallState {
3644
netCoreNotPresent,
3745
netCoreVersionNotSupported,
@@ -41,18 +49,24 @@ export const enum netCoreInstallState {
4149
const dotnet = os.platform() === "win32" ? "dotnet.exe" : "dotnet";
4250

4351
/**
44-
* Returns the configured Microsoft.Build.Sql version, falling back to the extension's
45-
* registered default (from package.json) when the setting is blank or not a valid semver.
52+
* Returns the configured Microsoft.Build.Sql version.
53+
*
54+
* Resolution order:
55+
* 1. User's configured value (global or workspace settings.json) — if it is a valid semver.
56+
* 2. Package.json default value — returned by config.get() when the user has not overridden the setting.
57+
* 3. FALLBACK_MICROSOFT_BUILD_SQL_VERSION — used only when both of the above are unavailable or
58+
* not a valid semver (e.g. the extension package.json default is missing or the user typed an
59+
* invalid version string).
4660
*/
47-
export function getMicrosoftBuildSqlVersion(microsoftBuildSqlVersionKey: string): string {
61+
export function getMicrosoftBuildSqlVersion(): string {
4862
const config = vscode.workspace.getConfiguration(DBProjectConfigurationKey);
4963
const configured = config.get<string>(microsoftBuildSqlVersionKey)?.trim();
5064
if (configured && semver.valid(configured)) {
5165
return configured;
5266
}
53-
// Fall back to the default registered in package.json
54-
const defaultValue = config.inspect<string>(microsoftBuildSqlVersionKey)?.defaultValue ?? "";
55-
return defaultValue;
67+
68+
// Fall back to the hardcoded constant if config value is unavailable or invalid
69+
return FALLBACK_MICROSOFT_BUILD_SQL_VERSION;
5670
}
5771

5872
export class NetCoreTool extends ShellExecutionHelper {

extensions/sql-database-projects/test/netCoreTool.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ import {
1313
NetCoreTool,
1414
DBProjectConfigurationKey,
1515
DotnetInstallLocationKey,
16+
FALLBACK_MICROSOFT_BUILD_SQL_VERSION,
17+
getMicrosoftBuildSqlVersion,
1618
} from "../src/tools/netcoreTool";
1719
import { getQuotedPath } from "../src/common/utils";
20+
import * as constants from "../src/common/constants";
1821
import { deleteGeneratedTestFolder, generateTestFolderPath } from "./testUtils";
1922
import { createContext, TestContext } from "./testContext";
2023

@@ -102,4 +105,58 @@ suite("NetCoreTool: Net core tests", function (): void {
102105
}
103106
}
104107
});
108+
109+
suite("getMicrosoftBuildSqlVersion tests", function (): void {
110+
teardown(async function (): Promise<void> {
111+
// Clean up configuration after each test
112+
await vscode.workspace
113+
.getConfiguration(DBProjectConfigurationKey)
114+
.update(
115+
constants.microsoftBuildSqlVersionKey,
116+
undefined,
117+
vscode.ConfigurationTarget.Global,
118+
);
119+
});
120+
121+
test("Should return valid configured value when set", async function (): Promise<void> {
122+
// Arrange: Set a valid semver version
123+
await vscode.workspace
124+
.getConfiguration(DBProjectConfigurationKey)
125+
.update(
126+
constants.microsoftBuildSqlVersionKey,
127+
"3.0.0",
128+
vscode.ConfigurationTarget.Global,
129+
);
130+
131+
// Act
132+
const result = getMicrosoftBuildSqlVersion();
133+
134+
// Assert
135+
expect(result).to.equal("3.0.0");
136+
});
137+
138+
test("Should fall back to FALLBACK_MICROSOFT_BUILD_SQL_VERSION when configured value is invalid or empty", async function (): Promise<void> {
139+
// Test with invalid semver
140+
await vscode.workspace
141+
.getConfiguration(DBProjectConfigurationKey)
142+
.update(
143+
constants.microsoftBuildSqlVersionKey,
144+
"not-a-valid-version",
145+
vscode.ConfigurationTarget.Global,
146+
);
147+
let result = getMicrosoftBuildSqlVersion();
148+
expect(result).to.equal(FALLBACK_MICROSOFT_BUILD_SQL_VERSION);
149+
150+
// Test with empty config
151+
await vscode.workspace
152+
.getConfiguration(DBProjectConfigurationKey)
153+
.update(
154+
constants.microsoftBuildSqlVersionKey,
155+
undefined,
156+
vscode.ConfigurationTarget.Global,
157+
);
158+
result = getMicrosoftBuildSqlVersion();
159+
expect(result).to.equal(FALLBACK_MICROSOFT_BUILD_SQL_VERSION);
160+
});
161+
});
105162
});

0 commit comments

Comments
 (0)