diff --git a/common/changes/@microsoft/rush/fix-log-double-open_2023-06-14-18-49.json b/common/changes/@microsoft/rush/fix-log-double-open_2023-06-14-18-49.json new file mode 100644 index 00000000000..83acdc6e026 --- /dev/null +++ b/common/changes/@microsoft/rush/fix-log-double-open_2023-06-14-18-49.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@microsoft/rush", + "comment": "Fix an issue where Rush would attempt to open a project's log file for writing twice.", + "type": "none" + } + ], + "packageName": "@microsoft/rush" +} \ No newline at end of file diff --git a/libraries/rush-lib/src/logic/operations/ProjectLogWritable.ts b/libraries/rush-lib/src/logic/operations/ProjectLogWritable.ts index f9fb3da0373..adf338a1fbd 100644 --- a/libraries/rush-lib/src/logic/operations/ProjectLogWritable.ts +++ b/libraries/rush-lib/src/logic/operations/ProjectLogWritable.ts @@ -60,39 +60,39 @@ export class ProjectLogWritable extends TerminalWritable { } const projectFolder: string = this._project.projectFolder; - const { - logPath: legacyLogPath, - errorLogPath: legacyErrorLogPath, - relativeLogPath: legacyRelativeLogPath, - relativeErrorLogPath: legacyRelativeErrorLogPath - } = getLogFilePaths(projectFolder, 'build'); + // Delete the legacy logs + const { logPath: legacyLogPath, errorLogPath: legacyErrorLogPath } = getLogFilePaths( + projectFolder, + 'build' + ); + FileSystem.deleteFile(legacyLogPath); + FileSystem.deleteFile(legacyErrorLogPath); + // If the phased commands experiment is enabled, put logs under `rush-logs` + let logFolder: string | undefined; if (project.rushConfiguration.experimentsConfiguration.configuration.phasedCommands) { - // Delete the legacy logs - FileSystem.deleteFile(legacyLogPath); - FileSystem.deleteFile(legacyErrorLogPath); - const logPathPrefix: string = `${projectFolder}/${RushConstants.rushLogsFolderName}`; FileSystem.ensureFolder(logPathPrefix); + logFolder = RushConstants.rushLogsFolderName; + } - const { logPath, errorLogPath, relativeLogPath, relativeErrorLogPath } = getLogFilePaths( - projectFolder, - logFilenameIdentifier, - RushConstants.rushLogsFolderName - ); - this.logPath = logPath; - this.errorLogPath = errorLogPath; - this.relativeLogPath = relativeLogPath; - this.relativeErrorLogPath = relativeErrorLogPath; - } else { - this.logPath = legacyLogPath; - this.errorLogPath = legacyErrorLogPath; - this.relativeLogPath = legacyRelativeLogPath; - this.relativeErrorLogPath = legacyRelativeErrorLogPath; + const { logPath, errorLogPath, relativeLogPath, relativeErrorLogPath } = getLogFilePaths( + projectFolder, + logFilenameIdentifier, + logFolder + ); + this.logPath = logPath; + this.errorLogPath = errorLogPath; + this.relativeLogPath = relativeLogPath; + this.relativeErrorLogPath = relativeErrorLogPath; + + if (legacyLogPath !== this.logPath) { + FileSystem.deleteFile(this.logPath); } - FileSystem.deleteFile(this.logPath); - FileSystem.deleteFile(this.errorLogPath); + if (legacyErrorLogPath !== this.errorLogPath) { + FileSystem.deleteFile(this.errorLogPath); + } this._logWriter = FileWriter.open(this.logPath); } diff --git a/libraries/rush-lib/src/logic/operations/ShellOperationRunner.ts b/libraries/rush-lib/src/logic/operations/ShellOperationRunner.ts index e0fcfd9269f..9a909ae10c7 100644 --- a/libraries/rush-lib/src/logic/operations/ShellOperationRunner.ts +++ b/libraries/rush-lib/src/logic/operations/ShellOperationRunner.ts @@ -142,6 +142,7 @@ export class ShellOperationRunner implements IOperationRunner { private async _executeAsync(context: IOperationRunnerContext): Promise { // Only open the *.cache.log file(s) if the cache is enabled. const cacheProjectLogWritable: ProjectLogWritable | undefined = this._buildCacheConfiguration + ?.buildCacheEnabled ? new ProjectLogWritable( this._rushProject, context.collatedWriter.terminal,