Skip to content

Commit

Permalink
fix(core): add workaround for broken .NET format command in v6+ (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder authored Mar 10, 2022
1 parent f65538e commit 2d09657
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 17 deletions.
4 changes: 3 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
{
"files": ["*.ts", "*.tsx"],
"extends": ["plugin:@nrwl/nx/typescript"],
"rules": {}
"rules": {
"eqeqeq": ["error", "smart"]
}
},
{
"files": ["*.js", "*.jsx"],
Expand Down
Empty file modified .husky/pre-push
100644 → 100755
Empty file.
8 changes: 4 additions & 4 deletions packages/core/src/executors/format/executor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('Format Executor', () => {
};
dotnetClient = new DotNetClient(mockDotnetFactory());
(dotnetClient as jest.Mocked<DotNetClient>).getSdkVersion.mockReturnValue(
Buffer.from('5.0.402'),
'5.0.402',
);
});

Expand Down Expand Up @@ -91,7 +91,7 @@ describe('Format Executor', () => {

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

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

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

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

const res = await executor(options, context, dotnetClient);
Expand Down
29 changes: 23 additions & 6 deletions packages/core/src/executors/format/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default async function runExecutor(
context: ExecutorContext,
dotnetClient: DotNetClient = new DotNetClient(dotnetFactory()),
) {
const sdkVersion = dotnetClient.getSdkVersion().toString();
const sdkVersion = dotnetClient.getSdkVersion();
const majorVersion = parseInt(sdkVersion.split('.')[0]);
const isNet6OrHigher = majorVersion >= 6;

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

const normalized = normalizeOptions(options, isNet6OrHigher);

ensureFormatToolInstalled(context, dotnetClient, isNet6OrHigher);
dotnetClient.format(projectFilePath, normalized);
ensureFormatToolInstalled(context, dotnetClient, majorVersion);
dotnetClient.format(projectFilePath, normalized, isNet6OrHigher);

return {
success: true,
Expand All @@ -56,9 +56,12 @@ export default async function runExecutor(
function ensureFormatToolInstalled(
context: ExecutorContext,
dotnetClient: DotNetClient,
isNet6OrHigher: boolean,
majorVersion: number,
) {
if (isNet6OrHigher) {
// Currently the built-in .NET Format executor is broken on .NET 6
// Fall back to installing and using the tool directly
// eslint-disable-next-line no-constant-condition
if (false && majorVersion >= 6) {
// dotnet-format is already included as part of .NET SDK 6+
return;
}
Expand All @@ -73,5 +76,19 @@ function ensureFormatToolInstalled(
return;
}

dotnetClient.installTool('dotnet-format');
if (majorVersion === 6) {
dotnetClient.installTool(
'dotnet-format',
'6.*',
'https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet6/nuget/v3/index.json',
);
} else if (majorVersion === 7) {
dotnetClient.installTool(
'dotnet-format',
'7.*',
'https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet7/nuget/v3/index.json',
);
} else {
dotnetClient.installTool('dotnet-format');
}
}
22 changes: 17 additions & 5 deletions packages/dotnet/src/lib/core/dotnet.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,14 @@ export class DotNetClient {
return this.logAndExecute(params);
}

installTool(tool: string): void {
installTool(tool: string, version?: string, source?: string): void {
const cmd = [`tool`, `install`, tool];
if (version) {
cmd.push('--version', version);
}
if (source) {
cmd.push('--add-source', source);
}
return this.logAndExecute(cmd);
}

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

format(project: string, parameters?: dotnetFormatOptions): void {
const params = [`format`, project];
format(
project: string,
parameters?: dotnetFormatOptions,
forceToolUsage?: boolean,
): void {
const params = forceToolUsage
? ['tool', 'run', 'dotnet-format', project]
: [`format`, project];
if (parameters) {
parameters = swapKeysUsingMap(parameters, formatKeyMap);
params.push(...getSpawnParameterArray(parameters));
Expand All @@ -144,8 +156,8 @@ export class DotNetClient {
this.logAndExecute(params);
}

getSdkVersion(): Buffer {
return this.execute(['--version']);
getSdkVersion(): string {
return this.cliCommand.info.version.toString();
}

printSdkVersion(): void {
Expand Down
2 changes: 1 addition & 1 deletion tools/scripts/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async function runTest() {
let selectedProjects = process.argv[2];

let testNamePattern = '';
if (process.argv[3] === '-t' || process.argv[3] == '--testNamePattern') {
if (process.argv[3] === '-t' || process.argv[3] === '--testNamePattern') {
testNamePattern = `--testNamePattern "${process.argv[4]}"`;
}

Expand Down

0 comments on commit 2d09657

Please sign in to comment.