Skip to content

feat(patch)!: Improves patches #4218

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

Merged
merged 4 commits into from
Mar 18, 2022
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
146 changes: 73 additions & 73 deletions .pnp.cjs

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions .yarn/patches/lodash-npm-4.17.21-6382451519.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/package.json b/package.json
index b35fd95cc7aa288e29b3d9b18defc946d610833b..1b05eed0c23c59d907e83839e6247e9a106c347f 100644
--- a/package.json
+++ b/package.json
@@ -6,6 +6,7 @@
"homepage": "https://lodash.com/",
"repository": "lodash/lodash",
"icon": "https://lodash.com/icon.svg",
+ "test": "foo",
"license": "MIT",
"main": "lodash.js",
"author": "John-David Dalton <john.david.dalton@gmail.com>",
38 changes: 38 additions & 0 deletions .yarn/versions/434fcaba.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
releases:
"@yarnpkg/core": minor
"@yarnpkg/fslib": minor
"@yarnpkg/plugin-compat": major
"@yarnpkg/plugin-patch": major

declined:
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-git"
- "@yarnpkg/plugin-github"
- "@yarnpkg/plugin-http"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- vscode-zipfs
- "@yarnpkg/builder"
- "@yarnpkg/cli"
- "@yarnpkg/doctor"
- "@yarnpkg/json-proxy"
- "@yarnpkg/nm"
- "@yarnpkg/pnp"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
- "@yarnpkg/shell"
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import {Filename, npath, ppath, xfs} from '@yarnpkg/fslib';

describe(`Commands`, () => {
describe(`patch`, () => {
test(
`it should restart the patch from scratch on subsequent patches by default`,
makeTemporaryEnv({
dependencies: {
[`no-deps`]: `1.0.0`,
},
}, async ({path, run, source}) => {
await run(`install`);

const notFound = {
externalException: {
code: `MODULE_NOT_FOUND`,
},
};

await expect(source(`require('no-deps/foo')`)).rejects.toMatchObject(notFound);
await expect(source(`require('no-deps/bar')`)).rejects.toMatchObject(notFound);

{
const {stdout} = await run(`patch`, `no-deps`, `--json`);
const {path: updateFolderN} = JSON.parse(stdout);

const updateFolder = npath.toPortablePath(updateFolderN);
const updateFile = ppath.join(updateFolder, `foo.js` as Filename);

const fileUser = `module.exports = 'foo';\n`;
await xfs.writeFilePromise(updateFile, fileUser);

await run(`patch-commit`, `-s`, npath.fromPortablePath(updateFolder));
await run(`install`);

await expect(source(`require('no-deps/foo')`)).resolves.toEqual(`foo`);
await expect(source(`require('no-deps/bar')`)).rejects.toMatchObject(notFound);
}

{
const {stdout} = await run(`patch`, `no-deps`, `--json`);
const {path: updateFolderN} = JSON.parse(stdout);

const updateFolder = npath.toPortablePath(updateFolderN);
const updateFile = ppath.join(updateFolder, `bar.js` as Filename);

const fileUser = `module.exports = 'bar';\n`;
await xfs.writeFilePromise(updateFile, fileUser);

await run(`patch-commit`, `-s`, npath.fromPortablePath(updateFolder));
await run(`install`);

await expect(source(`require('no-deps/foo')`)).rejects.toMatchObject(notFound);
await expect(source(`require('no-deps/bar')`)).resolves.toEqual(`bar`);
}
}),
);

test(
`it should augment current patches when using the -u flag`,
makeTemporaryEnv({
dependencies: {
[`no-deps`]: `1.0.0`,
},
}, async ({path, run, source}) => {
await run(`install`);

const notFound = {
externalException: {
code: `MODULE_NOT_FOUND`,
},
};

await expect(source(`require('no-deps/foo')`)).rejects.toMatchObject(notFound);
await expect(source(`require('no-deps/bar')`)).rejects.toMatchObject(notFound);

{
const {stdout} = await run(`patch`, `no-deps`, `--json`);
const {path: updateFolderN} = JSON.parse(stdout);

const updateFolder = npath.toPortablePath(updateFolderN);
const updateFile = ppath.join(updateFolder, `foo.js` as Filename);

const fileUser = `module.exports = 'foo';\n`;
await xfs.writeFilePromise(updateFile, fileUser);

await run(`patch-commit`, `-s`, npath.fromPortablePath(updateFolder));
await run(`install`);

await expect(source(`require('no-deps/foo')`)).resolves.toEqual(`foo`);
await expect(source(`require('no-deps/bar')`)).rejects.toMatchObject(notFound);
}

{
const {stdout} = await run(`patch`, `no-deps`, `-u`, `--json`);
const {path: updateFolderN} = JSON.parse(stdout);

const updateFolder = npath.toPortablePath(updateFolderN);
const updateFile = ppath.join(updateFolder, `bar.js` as Filename);

const fileUser = `module.exports = 'bar';\n`;
await xfs.writeFilePromise(updateFile, fileUser);

await run(`patch-commit`, `-s`, npath.fromPortablePath(updateFolder));
await run(`install`);

await expect(source(`require('no-deps/foo')`)).resolves.toEqual(`foo`);
await expect(source(`require('no-deps/bar')`)).resolves.toEqual(`bar`);
}
}),
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,151 @@ describe(`Commands`, () => {
});
}),
);

test(
`it should reference patches from the workspace dependencies when possible`,
makeTemporaryEnv({
dependencies: {
[`one-fixed-dep`]: `1.0.0`,
},
}, async ({path, run, source}) => {
await run(`install`);

const {stdout} = await run(`patch`, `one-fixed-dep`, `--json`);
const {path: updateFolderN} = JSON.parse(stdout);

const updateFolder = npath.toPortablePath(updateFolderN);
const updateFile = ppath.join(updateFolder, `index.js` as Filename);

const fileSource = await xfs.readFilePromise(updateFile, `utf8`);
const fileUser = fileSource.replace(`require(dep)`, `42`);
await xfs.writeFilePromise(updateFile, fileUser);

await run(`patch-commit`, `-s`, npath.fromPortablePath(updateFolder));

const manifest = await xfs.readJsonPromise(ppath.join(path, Filename.manifest));

expect(manifest.dependencies).toEqual({
[`one-fixed-dep`]: `patch:one-fixed-dep@npm%3A1.0.0#~/.yarn/patches/one-fixed-dep-npm-1.0.0-b02516a4af.patch`,
});

expect(manifest).not.toHaveProperty(`resolutions`);
}),
);

test(
`it should reference patches using the 'resolutions' field when required`,
makeTemporaryEnv({
dependencies: {
[`one-fixed-dep`]: `1.0.0`,
},
}, async ({path, run, source}) => {
await run(`install`);

const {stdout} = await run(`patch`, `no-deps`, `--json`);
const {path: updateFolderN} = JSON.parse(stdout);

const updateFolder = npath.toPortablePath(updateFolderN);
const updateFile = ppath.join(updateFolder, `index.js` as Filename);

const fileSource = await xfs.readFilePromise(updateFile, `utf8`);
const fileUser = fileSource.replace(`require(dep)`, `42`);
await xfs.writeFilePromise(updateFile, fileUser);

await run(`patch-commit`, `-s`, npath.fromPortablePath(updateFolder));

const manifest = await xfs.readJsonPromise(ppath.join(path, Filename.manifest));

expect(manifest.dependencies).toEqual({
[`one-fixed-dep`]: `1.0.0`,
});

expect(manifest.resolutions).toEqual({
[`no-deps@1.0.0`]: `patch:no-deps@npm%3A1.0.0#~/.yarn/patches/no-deps-npm-1.0.0-cf533b267a.patch`,
});
}),
);

test(
`it should replace the patch when calling patch-commit again, not wrap it into another patch layer`,
makeTemporaryEnv({
dependencies: {
[`one-fixed-dep`]: `1.0.0`,
},
}, async ({path, run, source}) => {
await run(`install`);

{
const {stdout} = await run(`patch`, `one-fixed-dep`, `--json`);
const {path: updateFolderN} = JSON.parse(stdout);

const updateFolder = npath.toPortablePath(updateFolderN);
const updateFile = ppath.join(updateFolder, `new.js` as Filename);

const fileUser = `module.exports = 42;\n`;
await xfs.writeFilePromise(updateFile, fileUser);

await run(`patch-commit`, `-s`, npath.fromPortablePath(updateFolder));

const manifest = await xfs.readJsonPromise(ppath.join(path, Filename.manifest));

expect(manifest.dependencies).toEqual({
[`one-fixed-dep`]: `patch:one-fixed-dep@npm%3A1.0.0#~/.yarn/patches/one-fixed-dep-npm-1.0.0-b02516a4af.patch`,
});
}

{
const {stdout} = await run(`patch`, `one-fixed-dep`, `--json`);
const {path: updateFolderN} = JSON.parse(stdout);

const updateFolder = npath.toPortablePath(updateFolderN);
const updateFile = ppath.join(updateFolder, `new.js` as Filename);

const fileUser = `module.exports = 21;\n`;
await xfs.writeFilePromise(updateFile, fileUser);

await run(`patch-commit`, `-s`, npath.fromPortablePath(updateFolder));

const manifest = await xfs.readJsonPromise(ppath.join(path, Filename.manifest));

expect(manifest.dependencies).toEqual({
[`one-fixed-dep`]: `patch:one-fixed-dep@npm%3A1.0.0#~/.yarn/patches/one-fixed-dep-npm-1.0.0-b02516a4af.patch`,
});
}
}),
);

test(
`it should be able to patch virtual packages`,
makeTemporaryEnv({
dependencies: {
[`peer-deps-lvl0`]: `1.0.0`,
},
}, async ({path, run, source}) => {
await run(`install`);

const {stdout} = await run(`patch`, `peer-deps-lvl1`, `--json`);
const {path: updateFolderN} = JSON.parse(stdout);

const updateFolder = npath.toPortablePath(updateFolderN);
const updateFile = ppath.join(updateFolder, `index.js` as Filename);

const fileSource = await xfs.readFilePromise(updateFile, `utf8`);
const fileUser = fileSource.replace(`require(dep)`, `42`);
await xfs.writeFilePromise(updateFile, fileUser);

await run(`patch-commit`, `-s`, npath.fromPortablePath(updateFolder));

const manifest = await xfs.readJsonPromise(ppath.join(path, Filename.manifest));

expect(manifest.dependencies).toEqual({
[`peer-deps-lvl0`]: `1.0.0`,
});

expect(manifest.resolutions).toEqual({
[`peer-deps-lvl1@1.0.0`]: `patch:peer-deps-lvl1@npm%3A1.0.0#~/.yarn/patches/peer-deps-lvl1-npm-1.0.0-894d37389e.patch`,
});
}),
);
});
});
2 changes: 1 addition & 1 deletion packages/plugin-compat/sources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const plugin: Plugin<CoreHooks & PatchHooks> = {
return structUtils.makeDescriptor(dependency, structUtils.makeRange({
protocol: `patch:`,
source: structUtils.stringifyDescriptor(dependency),
selector: `~builtin<compat/${structUtils.stringifyIdent(dependency)}>`,
selector: `optional!builtin<compat/${structUtils.stringifyIdent(dependency)}>`,
params: null,
}));
},
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-patch/sources/PatchFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {reportHunk}

export class PatchFetcher implements Fetcher {
supports(locator: Locator, opts: MinimalFetchOptions) {
if (!locator.reference.startsWith(`patch:`))
if (!patchUtils.isPatchLocator(locator))
return false;

return true;
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-patch/sources/PatchResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ const CACHE_VERSION = 3;

export class PatchResolver implements Resolver {
supportsDescriptor(descriptor: Descriptor, opts: MinimalResolveOptions) {
if (!descriptor.range.startsWith(`patch:`))
if (!patchUtils.isPatchDescriptor(descriptor))
return false;

return true;
}

supportsLocator(locator: Locator, opts: MinimalResolveOptions) {
if (!locator.reference.startsWith(`patch:`))
if (!patchUtils.isPatchLocator(locator))
return false;

return true;
Expand Down
Loading