diff --git a/.eslintrc.json b/.eslintrc.json index 06cc47d9..382a8325 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -24,7 +24,9 @@ { "files": ["*.ts", "*.tsx"], "extends": ["plugin:@nrwl/nx/typescript"], - "rules": {} + "rules": { + "eqeqeq": ["error", "smart"] + } }, { "files": ["*.js", "*.jsx"], diff --git a/.husky/pre-push b/.husky/pre-push old mode 100644 new mode 100755 diff --git a/packages/core/src/executors/format/executor.spec.ts b/packages/core/src/executors/format/executor.spec.ts index 84fa3d71..285d6e9d 100644 --- a/packages/core/src/executors/format/executor.spec.ts +++ b/packages/core/src/executors/format/executor.spec.ts @@ -55,7 +55,7 @@ describe('Format Executor', () => { }; dotnetClient = new DotNetClient(mockDotnetFactory()); (dotnetClient as jest.Mocked).getSdkVersion.mockReturnValue( - Buffer.from('5.0.402'), + '5.0.402', ); }); @@ -91,7 +91,7 @@ describe('Format Executor', () => { it('does not install dotnet-format if SDK is 6+', async () => { (dotnetClient as jest.Mocked).getSdkVersion.mockReturnValue( - Buffer.from('6.0.101'), + '6.0.101', ); jest.spyOn(fs, 'existsSync').mockReturnValue(true); @@ -108,7 +108,7 @@ describe('Format Executor', () => { it('passes the --check option on .NET 5 and earlier', async () => { (dotnetClient as jest.Mocked).getSdkVersion.mockReturnValue( - Buffer.from('5.0.101'), + '5.0.101', ); jest.spyOn(fs, 'existsSync').mockReturnValue(true); jest @@ -126,7 +126,7 @@ describe('Format Executor', () => { it('passes the --verify-no-changes option on .NET 6 and later', async () => { (dotnetClient as jest.Mocked).getSdkVersion.mockReturnValue( - Buffer.from('6.0.101'), + '6.0.101', ); const res = await executor(options, context, dotnetClient); diff --git a/packages/core/src/executors/format/executor.ts b/packages/core/src/executors/format/executor.ts index c42f26ae..9b5076fa 100644 --- a/packages/core/src/executors/format/executor.ts +++ b/packages/core/src/executors/format/executor.ts @@ -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; @@ -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, @@ -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; } @@ -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'); + } } diff --git a/packages/dotnet/src/lib/core/dotnet.client.ts b/packages/dotnet/src/lib/core/dotnet.client.ts index c3d7124d..90fed7c1 100644 --- a/packages/dotnet/src/lib/core/dotnet.client.ts +++ b/packages/dotnet/src/lib/core/dotnet.client.ts @@ -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); } @@ -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)); @@ -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 { diff --git a/tools/scripts/e2e.ts b/tools/scripts/e2e.ts index c95eddbc..c6b5ca5e 100644 --- a/tools/scripts/e2e.ts +++ b/tools/scripts/e2e.ts @@ -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]}"`; }