Skip to content

Commit

Permalink
fix: remove unsafe remove of install folder
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed Feb 10, 2024
1 parent 54e9510 commit 0f66d1b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
14 changes: 2 additions & 12 deletions sources/corepackUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,9 @@ export async function installVersion(installTarget: string, locator: Locator, {s
const {version, build} = locatorReference;

const installFolder = path.join(installTarget, locator.name, version);
const corepackFile = path.join(installFolder, `.corepack`);

// Older versions of Corepack didn't generate the `.corepack` file; in
// that case we just download the package manager anew.
if (fs.existsSync(corepackFile)) {
if (fs.existsSync(installFolder)) {
const corepackFile = path.join(installFolder, `.corepack`);
const corepackContent = await fs.promises.readFile(corepackFile, `utf8`);
const corepackData = JSON.parse(corepackContent);

Expand Down Expand Up @@ -172,14 +170,6 @@ export async function installVersion(installTarget: string, locator: Locator, {s
hash: serializedHash,
}));

// The target folder may exist if a previous version of Corepack installed
// it but didn't create the `.corepack` file. In this case we need to
// remove it first.
await fs.promises.rm(installFolder, {
recursive: true,
force: true,
});

await fs.promises.mkdir(path.dirname(installFolder), {recursive: true});
try {
await fs.promises.rename(tmpFolder, installFolder);
Expand Down
8 changes: 7 additions & 1 deletion sources/folderUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ import process from 'process';

import type {NodeError} from './nodeUtils';

/**
* If the install folder structure changes then increment this number.
* Note that changing this should be treated as a breaking change.
*/
const INSTALL_FOLDER_VERSION = 1;

export function getInstallFolder() {
return (
process.env.COREPACK_HOME ??
join(
process.env.XDG_CACHE_HOME ??
process.env.LOCALAPPDATA ??
join(homedir(), process.platform === `win32` ? `AppData/Local` : `.cache`),
`node/corepack`,
`node/corepack/v${INSTALL_FOLDER_VERSION}`,
)
);
}
Expand Down

0 comments on commit 0f66d1b

Please sign in to comment.