Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions sources/commands/Enable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,22 @@ export class EnableCommand extends Command<Context> {
async generatePosixLink(installDirectory: string, distFolder: string, binName: string) {
const file = path.join(installDirectory, binName);
const symlink = path.relative(installDirectory, path.join(distFolder, `${binName}.js`));
const stats = fs.lstatSync(file, {throwIfNoEntry: false});

if (fs.existsSync(file)) {
const currentSymlink = await fs.promises.realpath(file);
if (stats) {
if (stats.isSymbolicLink()) {
const currentSymlink = await fs.promises.readlink(file);

if (binName.includes(`yarn`) && corepackUtils.isYarnSwitchPath(currentSymlink)) {
console.warn(`${binName} is already installed in ${file} and points to a Yarn Switch install - skipping`);
return;
}
if (binName.includes(`yarn`) && corepackUtils.isYarnSwitchPath(await fs.promises.realpath(file))) {
console.warn(`${binName} is already installed in ${file} and points to a Yarn Switch install - skipping`);
return;
}

if (currentSymlink !== symlink) {
await fs.promises.unlink(file);
} else {
return;
if (currentSymlink === symlink) {
return;
}
}
await fs.promises.unlink(file);
}

await fs.promises.symlink(symlink, file);
Expand Down
45 changes: 43 additions & 2 deletions tests/Enable.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Filename, ppath, xfs, npath} from '@yarnpkg/fslib';
import {delimiter} from 'node:path';
import process from 'node:process';
import {setTimeout} from 'node:timers/promises';
import {describe, beforeEach, it, expect, test} from 'vitest';

import {Engine} from '../sources/Engine';
Expand Down Expand Up @@ -92,7 +93,6 @@ describe(`EnableCommand`, () => {
await xfs.mktempPromise(async cwd => {
await xfs.writeFilePromise(ppath.join(cwd, `yarn`), `hello`);

process.env.PATH = `${npath.fromPortablePath(cwd)}${delimiter}${process.env.PATH}`;
await expect(runCli(cwd, [`enable`, `--install-directory`, npath.fromPortablePath(cwd)])).resolves.toMatchObject({
stdout: ``,
stderr: ``,
Expand All @@ -114,7 +114,6 @@ describe(`EnableCommand`, () => {
ppath.join(cwd, `yarn`),
);

process.env.PATH = `${npath.fromPortablePath(cwd)}${delimiter}${process.env.PATH}`;
await expect(runCli(cwd, [`enable`, `--install-directory`, npath.fromPortablePath(cwd)])).resolves.toMatchObject({
stdout: ``,
stderr: expect.stringMatching(/^yarn is already installed in .+ and points to a Yarn Switch install - skipping\n$/),
Expand All @@ -125,4 +124,46 @@ describe(`EnableCommand`, () => {
expect(file).toBe(`hello`);
});
});

test.skipIf(process.platform === `win32`)(`should not re-link if binaries are already correct`, async () => {
await xfs.mktempPromise(async cwd => {
await makeBin(cwd, `corepack` as Filename);

await expect(runCli(cwd, [`enable`, `--install-directory`, npath.fromPortablePath(cwd)])).resolves.toMatchObject({
stdout: ``,
stderr: ``,
exitCode: 0,
});
const yarnStat1 = await xfs.lstatPromise(ppath.join(cwd, `yarn`));

await setTimeout(10);

await expect(runCli(cwd, [`enable`, `--install-directory`, npath.fromPortablePath(cwd)])).resolves.toMatchObject({
stdout: ``,
stderr: ``,
exitCode: 0,
});
const yarnStat2 = await xfs.lstatPromise(ppath.join(cwd, `yarn`));

expect(yarnStat2.mtimeMs).toBe(yarnStat1.mtimeMs);
});
});

test.skipIf(process.platform === `win32`)(`should overwrite existing symlinks if they are incorrect`, async () => {
await xfs.mktempPromise(async cwd => {
await makeBin(cwd, `corepack` as Filename);

await xfs.writeFilePromise(ppath.join(cwd, `dummy-target`), `hello`);
await xfs.symlinkPromise(ppath.join(cwd, `dummy-target`), ppath.join(cwd, `yarn`));

await expect(runCli(cwd, [`enable`, `--install-directory`, npath.fromPortablePath(cwd)])).resolves.toMatchObject({
stdout: ``,
stderr: ``,
exitCode: 0,
});

const newLink = await xfs.readlinkPromise(ppath.join(cwd, `yarn`));
expect(newLink).toContain(`yarn.js`);
});
});
});