Skip to content

Commit 2e7dcdb

Browse files
author
Nicholas Pape
authored
Fix issues with file locks during tarball creation (microsoft#445)
* Fix weirdness in tarball creation algorithm * Make it debugabble * Revert "Make it debugabble" This reverts commit 1a95239. * We should regenerate the tarball files if we are doing a rush generate or a clean install * Make it non optional
1 parent f320d7b commit 2e7dcdb

3 files changed

Lines changed: 50 additions & 55 deletions

File tree

‎apps/rush-lib/src/cli/actions/GenerateAction.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export default class GenerateAction extends BaseRushAction {
7171

7272
if (shrinkwrapFile
7373
&& !this._forceParameter.value
74-
&& installManager.createTempModulesAndCheckShrinkwrap(shrinkwrapFile)) {
74+
&& installManager.createTempModulesAndCheckShrinkwrap(shrinkwrapFile, false)) {
7575
console.log();
7676
console.log(colors.yellow('Skipping generate, since all project dependencies are already satisfied.'));
7777
console.log();
@@ -86,7 +86,7 @@ export default class GenerateAction extends BaseRushAction {
8686

8787
installManager.ensureLocalNpmTool(false);
8888

89-
installManager.createTempModules();
89+
installManager.createTempModules(true);
9090

9191
// Delete both copies of the shrinkwrap file
9292
if (fsx.existsSync(this.rushConfiguration.committedShrinkwrapFilename)) {

‎apps/rush-lib/src/cli/actions/InstallAction.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,20 @@ export default class InstallAction extends BaseRushAction {
9393
return;
9494
}
9595

96-
if (!installManager.createTempModulesAndCheckShrinkwrap(shrinkwrapFile)) {
97-
console.log('');
98-
console.log(colors.red('You need to run "rush generate" to update your NPM shrinkwrap file.'));
99-
process.exit(1);
100-
return;
101-
}
102-
10396
let installType: InstallType = InstallType.Normal;
10497
if (this._cleanInstallFull.value) {
10598
installType = InstallType.UnsafePurge;
10699
} else if (this._cleanInstall.value) {
107100
installType = InstallType.ForceClean;
108101
}
109102

103+
if (!installManager.createTempModulesAndCheckShrinkwrap(shrinkwrapFile, installType !== InstallType.Normal)) {
104+
console.log('');
105+
console.log(colors.red('You need to run "rush generate" to update your NPM shrinkwrap file.'));
106+
process.exit(1);
107+
return;
108+
}
109+
110110
installManager.installCommonModules(installType);
111111

112112
stopwatch.stop();

‎apps/rush-lib/src/cli/utilities/InstallManager.ts‎

Lines changed: 41 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ export default class InstallManager {
195195
/**
196196
* Regenerates the common/package.json and all temp_modules projects.
197197
*/
198-
public createTempModules(): void {
199-
this.createTempModulesAndCheckShrinkwrap(undefined);
198+
public createTempModules(forceCreate: boolean): void {
199+
this.createTempModulesAndCheckShrinkwrap(undefined, forceCreate);
200200
}
201201

202202
/**
@@ -205,7 +205,9 @@ export default class InstallManager {
205205
* everything we need to install and returns true if so; in all other cases,
206206
* the return value is false.
207207
*/
208-
public createTempModulesAndCheckShrinkwrap(shrinkwrapFile: ShrinkwrapFile | undefined): boolean {
208+
public createTempModulesAndCheckShrinkwrap(
209+
shrinkwrapFile: ShrinkwrapFile | undefined,
210+
forceCreate: boolean): boolean {
209211
const stopwatch: Stopwatch = Stopwatch.start();
210212

211213
// Example: "C:\MyRepo\common\temp\projects"
@@ -379,38 +381,23 @@ export default class InstallManager {
379381
// NPM expects the root of the tarball to have a directory called 'package'
380382
const npmPackageFolder: string = 'package';
381383

382-
// Example: "C:\MyRepo\common\temp\projects\my-project-2.new"
384+
// Example: "C:\MyRepo\common\temp\projects\my-project-2"
383385
const tempProjectFolder: string = path.join(
384386
this._rushConfiguration.commonTempFolder,
385387
RushConstants.rushTempProjectsFolderName,
386-
unscopedTempProjectName + '.new');
388+
unscopedTempProjectName);
387389

388390
// Example: "C:\MyRepo\common\temp\projects\my-project-2\package.json"
389391
const tempPackageJsonFilename: string = path.join(tempProjectFolder, RushConstants.packageJsonFilename);
390392

391-
// Example: "C:\MyRepo\common\temp\projects\my-project-2.old"
392-
const extractedFolder: string = tempProjectFolder + '.old';
393-
394393
// we only want to overwrite the package if the existing tarball's package.json is different from tempPackageJson
395394
let shouldOverwrite: boolean = true;
396395
try {
397-
// extract the tarball and compare the package.json directly
398-
if (fsx.existsSync(tarballFile)) {
399-
400-
// ensure the folder we are about to extract into exists
401-
Utilities.createFolderWithRetry(extractedFolder);
402-
403-
tar.extract({
404-
cwd: extractedFolder,
405-
file: tarballFile,
406-
sync: true
407-
});
408-
409-
const extractedPackageJsonFilename: string =
410-
path.join(extractedFolder, npmPackageFolder, RushConstants.packageJsonFilename);
396+
// if the tarball and the temp file still exist, then compare the contents
397+
if (fsx.existsSync(tarballFile) && fsx.existsSync(tempPackageJsonFilename)) {
411398

412399
// compare the extracted package.json with the one we are about to write
413-
const oldBuffer: Buffer = fsx.readFileSync(extractedPackageJsonFilename);
400+
const oldBuffer: Buffer = fsx.readFileSync(tempPackageJsonFilename);
414401
const newBuffer: Buffer = new Buffer(JsonFile.stringify(tempPackageJson));
415402

416403
if (Buffer.compare(oldBuffer, newBuffer) === 0) {
@@ -421,30 +408,38 @@ export default class InstallManager {
421408
// ignore the error, we will go ahead and create a new tarball
422409
}
423410

424-
if (shouldOverwrite) {
425-
// ensure the folder we are about to zip exists
426-
Utilities.createFolderWithRetry(tempProjectFolder);
427-
428-
// write the expected package.json file into the zip staging folder
429-
JsonFile.save(tempPackageJson, tempPackageJsonFilename);
430-
431-
// create the new tarball, this overwrites the existing one
432-
tar.create({
433-
gzip: true,
434-
file: tarballFile,
435-
cwd: tempProjectFolder,
436-
portable: true,
437-
noPax: true,
438-
sync: true,
439-
prefix: npmPackageFolder
440-
}, ['package.json']);
441-
442-
console.log(`Updating ${tarballFile}`);
443-
}
411+
if (shouldOverwrite || forceCreate) {
412+
try {
413+
// ensure the folder we are about to zip exists
414+
Utilities.createFolderWithRetry(tempProjectFolder);
444415

445-
// clean up the old tarball & the temp folder
446-
fsx.removeSync(tempProjectFolder);
447-
fsx.removeSync(extractedFolder);
416+
// remove the old tarball & old temp package json, this is for any cases where new tarball creation
417+
// fails, and the shouldOverwrite logic is messed up because the my-project-2\package.json
418+
// exists and is updated, but the tarball is not accurate
419+
fsx.removeSync(tarballFile);
420+
fsx.removeSync(tempPackageJsonFilename);
421+
422+
// write the expected package.json file into the zip staging folder
423+
JsonFile.save(tempPackageJson, tempPackageJsonFilename);
424+
425+
// create the new tarball
426+
tar.create({
427+
gzip: true,
428+
file: tarballFile,
429+
cwd: tempProjectFolder,
430+
portable: true,
431+
noPax: true,
432+
sync: true,
433+
prefix: npmPackageFolder
434+
}, ['package.json']);
435+
436+
console.log(`Updating ${tarballFile}`);
437+
} catch (error) {
438+
// delete everything in case of any error
439+
fsx.removeSync(tarballFile);
440+
fsx.removeSync(tempPackageJsonFilename);
441+
}
442+
}
448443
}
449444

450445
// Example: "C:\MyRepo\common\temp\package.json"

0 commit comments

Comments
 (0)