-
Notifications
You must be signed in to change notification settings - Fork 699
[rush] Avoid unnecessary package manager install lock #5844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
cba38b3
515082e
238b3fe
12393bd
6d020d5
37fea3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "changes": [ | ||
| { | ||
| "packageName": "@microsoft/rush", | ||
| "comment": "Avoid acquiring the global package manager install lock when the requested package manager is already installed and no install lock file exists.", | ||
| "type": "patch" | ||
| } | ||
| ], | ||
| "packageName": "@microsoft/rush", | ||
| "email": "EscapeB@users.noreply.github.com" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,14 @@ | |
|
|
||
| import { pnpmSyncGetJsonVersion } from 'pnpm-sync-lib'; | ||
|
|
||
| import { JsonFile, type JsonObject, Path, type IPackageJson, Objects } from '@rushstack/node-core-library'; | ||
| import { | ||
| FileSystem, | ||
| JsonFile, | ||
| type JsonObject, | ||
| Path, | ||
| type IPackageJson, | ||
| Objects | ||
| } from '@rushstack/node-core-library'; | ||
|
|
||
| import type { PackageManagerName } from './packageManager/PackageManager'; | ||
| import type { RushConfiguration } from './RushConfiguration'; | ||
|
|
@@ -182,6 +189,28 @@ export class LastInstallFlag extends FlagFile<Partial<ILastInstallFlagJson>> { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Checks for a LockFile associated with the same install resource as a LastInstallFlag. | ||
| * | ||
| * @internal | ||
| */ | ||
| export async function doesLastInstallFlagLockFileExistAsync( | ||
| folderPath: string, | ||
| lockFileResourceName: string | ||
| ): Promise<boolean> { | ||
| for (const itemName of await FileSystem.readFolderItemNamesAsync(folderPath)) { | ||
| if (itemName === `${lockFileResourceName}.lock`) { | ||
| return true; | ||
| } | ||
|
|
||
| if (itemName.startsWith(`${lockFileResourceName}#`) && itemName.endsWith('.lock')) { | ||
| return true; | ||
| } | ||
|
Comment on lines
+202
to
+208
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where do these names come from? Can you share code with whatever generates them when they're written? |
||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the LastInstall flag and sets the current state. This state is used to compare | ||
| * against the last-known-good state tracked by the LastInstall flag. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,14 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
| // See LICENSE in the project root for license information. | ||
|
|
||
| import { type IPackageJson, JsonFile } from '@rushstack/node-core-library'; | ||
| import { FileSystem, type IPackageJson, JsonFile, LockFile } from '@rushstack/node-core-library'; | ||
| import { StringBufferTerminalProvider, Terminal } from '@rushstack/terminal'; | ||
|
|
||
| import { InstallHelpers } from '../installManager/InstallHelpers'; | ||
| import { RushConfiguration } from '../../api/RushConfiguration'; | ||
| import { LastInstallFlag } from '../../api/LastInstallFlag'; | ||
| import type { RushGlobalFolder } from '../../api/RushGlobalFolder'; | ||
| import { Utilities } from '../../utilities/Utilities'; | ||
|
|
||
| describe(InstallHelpers.name, () => { | ||
| describe(InstallHelpers.generateCommonPackageJsonAsync.name, () => { | ||
|
|
@@ -32,6 +35,10 @@ describe(InstallHelpers.name, () => { | |
| mockJsonFileSaveAsync.mockClear(); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('generates correct package json with pnpm configurations', async () => { | ||
| const RUSH_JSON_FILENAME: string = `${__dirname}/pnpmConfig/rush.json`; | ||
| const rushConfiguration: RushConfiguration = | ||
|
|
@@ -114,4 +121,122 @@ describe(InstallHelpers.name, () => { | |
| expect(pnpmField).not.toHaveProperty('patchedDependencies'); | ||
| }); | ||
| }); | ||
|
|
||
| describe(InstallHelpers.ensureLocalPackageManagerAsync.name, () => { | ||
| const tempFolderPath: string = `${__dirname}/temp/${InstallHelpers.name}`; | ||
| const packageManager: 'pnpm' = 'pnpm'; | ||
| const packageManagerVersion: string = '10.27.0'; | ||
|
|
||
| function getRushGlobalFolder(): RushGlobalFolder { | ||
| return { | ||
| path: `${tempFolderPath}/rush-global`, | ||
| nodeSpecificPath: `${tempFolderPath}/rush-global/node-${process.version}` | ||
| } as RushGlobalFolder; | ||
| } | ||
|
|
||
| function getRushConfiguration(): RushConfiguration { | ||
| return { | ||
| commonRushConfigFolder: `${tempFolderPath}/common/config/rush`, | ||
| commonTempFolder: `${tempFolderPath}/common/temp`, | ||
| packageManager, | ||
| packageManagerToolVersion: packageManagerVersion | ||
| } as RushConfiguration; | ||
| } | ||
|
|
||
| function getPackageManagerToolFolder(rushGlobalFolder: RushGlobalFolder): string { | ||
| return `${rushGlobalFolder.nodeSpecificPath}/${packageManager}-${packageManagerVersion}`; | ||
| } | ||
|
|
||
| async function writeInstalledPackageManagerAsync(rushGlobalFolder: RushGlobalFolder): Promise<void> { | ||
| const packageManagerToolFolder: string = getPackageManagerToolFolder(rushGlobalFolder); | ||
|
|
||
| await Promise.all([ | ||
| JsonFile.saveAsync( | ||
| { | ||
| dependencies: { | ||
| [packageManager]: packageManagerVersion | ||
| }, | ||
| description: 'Temporary file generated by the Rush tool', | ||
| name: `${packageManager}-local-install`, | ||
| private: true, | ||
| version: '0.0.0' | ||
| }, | ||
| `${packageManagerToolFolder}/package.json`, | ||
| { ensureFolderExists: true } | ||
| ), | ||
| JsonFile.saveAsync( | ||
| { | ||
| name: packageManager, | ||
| version: packageManagerVersion | ||
| }, | ||
| `${packageManagerToolFolder}/node_modules/${packageManager}/package.json`, | ||
| { ensureFolderExists: true } | ||
| ), | ||
| FileSystem.writeFileAsync(`${packageManagerToolFolder}/node_modules/.bin/${packageManager}`, '', { | ||
| ensureFolderExists: true | ||
| }) | ||
| ]); | ||
|
|
||
| await new LastInstallFlag(packageManagerToolFolder, { node: process.versions.node }).createAsync(); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| jest.restoreAllMocks(); | ||
| FileSystem.ensureEmptyFolder(tempFolderPath); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| FileSystem.deleteFolder(tempFolderPath); | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('does not acquire the global lock when the package manager is already installed', async () => { | ||
| const rushGlobalFolder: RushGlobalFolder = getRushGlobalFolder(); | ||
| await writeInstalledPackageManagerAsync(rushGlobalFolder); | ||
|
|
||
| const lockAcquireSpy: jest.SpyInstance = jest | ||
| .spyOn(LockFile, 'acquireAsync') | ||
| .mockResolvedValue(false as unknown as LockFile); | ||
| const installSpy: jest.SpyInstance = jest | ||
| .spyOn(Utilities, 'installPackageInDirectoryAsync') | ||
| .mockRejectedValue(new Error('The package manager should already be installed.')); | ||
| const rushConfiguration: RushConfiguration = getRushConfiguration(); | ||
|
|
||
| await InstallHelpers.ensureLocalPackageManagerAsync(rushConfiguration, rushGlobalFolder, 1, true); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that the lockAcquireSpy returns false so that the test doesn't try to actually install the package if something goes wrong.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated |
||
|
|
||
| expect(lockAcquireSpy).not.toHaveBeenCalled(); | ||
| expect(installSpy).not.toHaveBeenCalled(); | ||
| await expect( | ||
| FileSystem.existsAsync(`${rushConfiguration.commonTempFolder}/pnpm-local`) | ||
| ).resolves.toEqual(true); | ||
| }); | ||
|
|
||
| it('acquires the global lock if an install lock file is present', async () => { | ||
| const rushGlobalFolder: RushGlobalFolder = getRushGlobalFolder(); | ||
| await writeInstalledPackageManagerAsync(rushGlobalFolder); | ||
| await FileSystem.writeFileAsync( | ||
| `${rushGlobalFolder.nodeSpecificPath}/${packageManager}-${packageManagerVersion}#123.lock`, | ||
| '' | ||
| ); | ||
|
|
||
| const releaseLockMock: jest.Mock = jest.fn(); | ||
| const lockAcquireSpy: jest.SpyInstance = jest.spyOn(LockFile, 'acquireAsync').mockResolvedValue({ | ||
| dirtyWhenAcquired: true, | ||
| release: releaseLockMock | ||
| } as unknown as LockFile); | ||
| const installSpy: jest.SpyInstance = jest | ||
| .spyOn(Utilities, 'installPackageInDirectoryAsync') | ||
| .mockResolvedValue(); | ||
| const rushConfiguration: RushConfiguration = getRushConfiguration(); | ||
|
|
||
| await InstallHelpers.ensureLocalPackageManagerAsync(rushConfiguration, rushGlobalFolder, 1, true); | ||
|
|
||
| expect(lockAcquireSpy).toHaveBeenCalledTimes(1); | ||
| expect(installSpy).toHaveBeenCalledTimes(1); | ||
| expect(releaseLockMock).toHaveBeenCalledTimes(1); | ||
| await expect( | ||
| FileSystem.existsAsync(`${rushConfiguration.commonTempFolder}/pnpm-local`) | ||
| ).resolves.toEqual(true); | ||
| }); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.