Skip to content

Commit 2d09657

Browse files
authored
fix(core): add workaround for broken .NET format command in v6+ (#397)
1 parent f65538e commit 2d09657

File tree

6 files changed

+48
-17
lines changed

6 files changed

+48
-17
lines changed

.eslintrc.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
{
2525
"files": ["*.ts", "*.tsx"],
2626
"extends": ["plugin:@nrwl/nx/typescript"],
27-
"rules": {}
27+
"rules": {
28+
"eqeqeq": ["error", "smart"]
29+
}
2830
},
2931
{
3032
"files": ["*.js", "*.jsx"],

.husky/pre-push

100644100755
File mode changed.

packages/core/src/executors/format/executor.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('Format Executor', () => {
5555
};
5656
dotnetClient = new DotNetClient(mockDotnetFactory());
5757
(dotnetClient as jest.Mocked<DotNetClient>).getSdkVersion.mockReturnValue(
58-
Buffer.from('5.0.402'),
58+
'5.0.402',
5959
);
6060
});
6161

@@ -91,7 +91,7 @@ describe('Format Executor', () => {
9191

9292
it('does not install dotnet-format if SDK is 6+', async () => {
9393
(dotnetClient as jest.Mocked<DotNetClient>).getSdkVersion.mockReturnValue(
94-
Buffer.from('6.0.101'),
94+
'6.0.101',
9595
);
9696

9797
jest.spyOn(fs, 'existsSync').mockReturnValue(true);
@@ -108,7 +108,7 @@ describe('Format Executor', () => {
108108

109109
it('passes the --check option on .NET 5 and earlier', async () => {
110110
(dotnetClient as jest.Mocked<DotNetClient>).getSdkVersion.mockReturnValue(
111-
Buffer.from('5.0.101'),
111+
'5.0.101',
112112
);
113113
jest.spyOn(fs, 'existsSync').mockReturnValue(true);
114114
jest
@@ -126,7 +126,7 @@ describe('Format Executor', () => {
126126

127127
it('passes the --verify-no-changes option on .NET 6 and later', async () => {
128128
(dotnetClient as jest.Mocked<DotNetClient>).getSdkVersion.mockReturnValue(
129-
Buffer.from('6.0.101'),
129+
'6.0.101',
130130
);
131131

132132
const res = await executor(options, context, dotnetClient);

packages/core/src/executors/format/executor.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default async function runExecutor(
3434
context: ExecutorContext,
3535
dotnetClient: DotNetClient = new DotNetClient(dotnetFactory()),
3636
) {
37-
const sdkVersion = dotnetClient.getSdkVersion().toString();
37+
const sdkVersion = dotnetClient.getSdkVersion();
3838
const majorVersion = parseInt(sdkVersion.split('.')[0]);
3939
const isNet6OrHigher = majorVersion >= 6;
4040

@@ -45,8 +45,8 @@ export default async function runExecutor(
4545

4646
const normalized = normalizeOptions(options, isNet6OrHigher);
4747

48-
ensureFormatToolInstalled(context, dotnetClient, isNet6OrHigher);
49-
dotnetClient.format(projectFilePath, normalized);
48+
ensureFormatToolInstalled(context, dotnetClient, majorVersion);
49+
dotnetClient.format(projectFilePath, normalized, isNet6OrHigher);
5050

5151
return {
5252
success: true,
@@ -56,9 +56,12 @@ export default async function runExecutor(
5656
function ensureFormatToolInstalled(
5757
context: ExecutorContext,
5858
dotnetClient: DotNetClient,
59-
isNet6OrHigher: boolean,
59+
majorVersion: number,
6060
) {
61-
if (isNet6OrHigher) {
61+
// Currently the built-in .NET Format executor is broken on .NET 6
62+
// Fall back to installing and using the tool directly
63+
// eslint-disable-next-line no-constant-condition
64+
if (false && majorVersion >= 6) {
6265
// dotnet-format is already included as part of .NET SDK 6+
6366
return;
6467
}
@@ -73,5 +76,19 @@ function ensureFormatToolInstalled(
7376
return;
7477
}
7578

76-
dotnetClient.installTool('dotnet-format');
79+
if (majorVersion === 6) {
80+
dotnetClient.installTool(
81+
'dotnet-format',
82+
'6.*',
83+
'https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet6/nuget/v3/index.json',
84+
);
85+
} else if (majorVersion === 7) {
86+
dotnetClient.installTool(
87+
'dotnet-format',
88+
'7.*',
89+
'https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet7/nuget/v3/index.json',
90+
);
91+
} else {
92+
dotnetClient.installTool('dotnet-format');
93+
}
7794
}

packages/dotnet/src/lib/core/dotnet.client.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,14 @@ export class DotNetClient {
115115
return this.logAndExecute(params);
116116
}
117117

118-
installTool(tool: string): void {
118+
installTool(tool: string, version?: string, source?: string): void {
119119
const cmd = [`tool`, `install`, tool];
120+
if (version) {
121+
cmd.push('--version', version);
122+
}
123+
if (source) {
124+
cmd.push('--add-source', source);
125+
}
120126
return this.logAndExecute(cmd);
121127
}
122128

@@ -130,8 +136,14 @@ export class DotNetClient {
130136
return this.logAndExecute(cmd);
131137
}
132138

133-
format(project: string, parameters?: dotnetFormatOptions): void {
134-
const params = [`format`, project];
139+
format(
140+
project: string,
141+
parameters?: dotnetFormatOptions,
142+
forceToolUsage?: boolean,
143+
): void {
144+
const params = forceToolUsage
145+
? ['tool', 'run', 'dotnet-format', project]
146+
: [`format`, project];
135147
if (parameters) {
136148
parameters = swapKeysUsingMap(parameters, formatKeyMap);
137149
params.push(...getSpawnParameterArray(parameters));
@@ -144,8 +156,8 @@ export class DotNetClient {
144156
this.logAndExecute(params);
145157
}
146158

147-
getSdkVersion(): Buffer {
148-
return this.execute(['--version']);
159+
getSdkVersion(): string {
160+
return this.cliCommand.info.version.toString();
149161
}
150162

151163
printSdkVersion(): void {

tools/scripts/e2e.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async function runTest() {
1717
let selectedProjects = process.argv[2];
1818

1919
let testNamePattern = '';
20-
if (process.argv[3] === '-t' || process.argv[3] == '--testNamePattern') {
20+
if (process.argv[3] === '-t' || process.argv[3] === '--testNamePattern') {
2121
testNamePattern = `--testNamePattern "${process.argv[4]}"`;
2222
}
2323

0 commit comments

Comments
 (0)