Skip to content

Commit 28b0131

Browse files
committed
feat: move getCustomDataKey to Linker
1 parent d1961ee commit 28b0131

File tree

8 files changed

+82
-53
lines changed

8 files changed

+82
-53
lines changed

.yarn/versions/fd50b4cb.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
releases:
2+
"@yarnpkg/core": major
3+
"@yarnpkg/plugin-nm": patch
4+
"@yarnpkg/plugin-pnp": patch
5+
"@yarnpkg/plugin-pnpm": patch
6+
7+
declined:
8+
- "@yarnpkg/plugin-compat"
9+
- "@yarnpkg/plugin-constraints"
10+
- "@yarnpkg/plugin-dlx"
11+
- "@yarnpkg/plugin-essentials"
12+
- "@yarnpkg/plugin-exec"
13+
- "@yarnpkg/plugin-file"
14+
- "@yarnpkg/plugin-git"
15+
- "@yarnpkg/plugin-github"
16+
- "@yarnpkg/plugin-http"
17+
- "@yarnpkg/plugin-init"
18+
- "@yarnpkg/plugin-interactive-tools"
19+
- "@yarnpkg/plugin-link"
20+
- "@yarnpkg/plugin-npm"
21+
- "@yarnpkg/plugin-npm-cli"
22+
- "@yarnpkg/plugin-pack"
23+
- "@yarnpkg/plugin-patch"
24+
- "@yarnpkg/plugin-stage"
25+
- "@yarnpkg/plugin-typescript"
26+
- "@yarnpkg/plugin-version"
27+
- "@yarnpkg/plugin-workspace-tools"
28+
- "@yarnpkg/builder"
29+
- "@yarnpkg/cli"
30+
- "@yarnpkg/doctor"
31+
- "@yarnpkg/extensions"
32+
- "@yarnpkg/nm"
33+
- "@yarnpkg/pnpify"
34+
- "@yarnpkg/sdks"

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ The following changes only affect people writing Yarn plugins:
3131

3232
- The `generateLoader` function in `@yarnpkg/pnp` no longer generates the `$$SETUP_STATE` function, it now needs to be present in the `loader` passed to the function.
3333

34+
- The `getCustomDataKey` function in `Installer` from `@yarnpkg/core` has been moved to `Linker`.
35+
3436
### Compatibility
3537

3638
- The patched filesystem now supports `ftruncate`.

packages/plugin-nm/sources/NodeModulesLinker.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ export enum NodeModulesMode {
3434
export class NodeModulesLinker implements Linker {
3535
private installStateCache: Map<string, Promise<InstallState | null>> = new Map();
3636

37+
getCustomDataKey() {
38+
return JSON.stringify({
39+
name: `NodeModulesLinker`,
40+
version: 2,
41+
});
42+
}
43+
3744
supportsPackage(pkg: Package, opts: MinimalLinkOptions) {
3845
return this.isEnabled(opts);
3946
}
@@ -119,13 +126,6 @@ class NodeModulesInstaller implements Installer {
119126
// Nothing to do
120127
}
121128

122-
getCustomDataKey() {
123-
return JSON.stringify({
124-
name: `NodeModulesInstaller`,
125-
version: 2,
126-
});
127-
}
128-
129129
private customData: {
130130
store: Map<LocatorHash, CustomPackageData>;
131131
} = {

packages/plugin-pnp/sources/PnpLinker.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ export class PnpLinker implements Linker {
2424

2525
private pnpCache: Map<string, PnpApi> = new Map();
2626

27+
getCustomDataKey() {
28+
return JSON.stringify({
29+
name: `PnpLinker`,
30+
version: 2,
31+
});
32+
}
33+
2734
supportsPackage(pkg: Package, opts: MinimalLinkOptions) {
2835
return this.isEnabled(opts);
2936
}
@@ -101,13 +108,6 @@ export class PnpInstaller implements Installer {
101108
this.opts = opts;
102109
}
103110

104-
getCustomDataKey() {
105-
return JSON.stringify({
106-
name: `PnpInstaller`,
107-
version: 2,
108-
});
109-
}
110-
111111
private customData: {
112112
store: Map<LocatorHash, CustomPackageData>;
113113
} = {

packages/plugin-pnpm/sources/PnpmLinker.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ export type PnpmCustomData = {
99
};
1010

1111
export class PnpmLinker implements Linker {
12+
getCustomDataKey() {
13+
return JSON.stringify({
14+
name: `PnpmLinker`,
15+
version: 2,
16+
});
17+
}
18+
1219
supportsPackage(pkg: Package, opts: MinimalLinkOptions) {
1320
return this.isEnabled(opts);
1421
}
@@ -18,7 +25,7 @@ export class PnpmLinker implements Linker {
1825
throw new Error(`Assertion failed: Expected the pnpm linker to be enabled`);
1926

2027
const customDataKey = getCustomDataKey();
21-
const customData = opts.project.installersCustomData.get(customDataKey) as PnpmCustomData | undefined;
28+
const customData = opts.project.linkersCustomData.get(customDataKey) as PnpmCustomData | undefined;
2229
if (!customData)
2330
throw new UsageError(`The project in ${formatUtils.pretty(opts.project.configuration, `${opts.project.cwd}/package.json`, formatUtils.Type.PATH)} doesn't seem to have been installed - running an install there might help`);
2431

@@ -34,7 +41,7 @@ export class PnpmLinker implements Linker {
3441
return null;
3542

3643
const customDataKey = getCustomDataKey();
37-
const customData = opts.project.installersCustomData.get(customDataKey) as any;
44+
const customData = opts.project.linkersCustomData.get(customDataKey) as any;
3845
if (!customData)
3946
throw new UsageError(`The project in ${formatUtils.pretty(opts.project.configuration, `${opts.project.cwd}/package.json`, formatUtils.Type.PATH)} doesn't seem to have been installed - running an install there might help`);
4047

@@ -77,10 +84,6 @@ class PnpmInstaller implements Installer {
7784
// Nothing to do
7885
}
7986

80-
getCustomDataKey() {
81-
return getCustomDataKey();
82-
}
83-
8487
private customData: PnpmCustomData = {
8588
pathByLocator: new Map(),
8689
locatorByPath: new Map(),
@@ -286,13 +289,6 @@ class PnpmInstaller implements Installer {
286289
}
287290
}
288291

289-
function getCustomDataKey() {
290-
return JSON.stringify({
291-
name: `PnpmInstaller`,
292-
version: 2,
293-
});
294-
}
295-
296292
function getNodeModulesLocation(project: Project) {
297293
return ppath.join(project.cwd, Filename.nodeModules);
298294
}

packages/yarnpkg-core/sources/Installer.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,6 @@ export type InstallPackageExtraApi = {
7171
};
7272

7373
export interface Installer {
74-
/**
75-
* Return an arbitrary key.
76-
*
77-
* This key will be used to save and restore the installer's custom data. You
78-
* typically will want to return the installer's name, but you can be fancy
79-
* and send a stringified JSON payload that include the cache version, etc.
80-
*
81-
* TODO (Yarn 4): Move this method into `Linker` so that linkers can use it
82-
* to save some state useful to findPackageLocator (cf PnpmLinker).
83-
*/
84-
getCustomDataKey(): string;
85-
8674
/**
8775
* Only called if the installer has a custom data key matching one currently
8876
* stored. Will be called with whatever `finalizeInstall` returned in its

packages/yarnpkg-core/sources/Linker.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ export interface Linker {
6969
*/
7070
findPackageLocator(location: PortablePath, opts: LinkOptions): Promise<Locator | null>;
7171

72+
/**
73+
* Return an arbitrary key.
74+
*
75+
* This key will be used to save and restore the installer's custom data. You
76+
* typically will want to return the installer's name, but you can be fancy
77+
* and send a stringified JSON payload that include the cache version, etc.
78+
*/
79+
getCustomDataKey(): string;
80+
7281
/**
7382
* This function must instantiate an Installer object that describes how to
7483
* install the packages on the disk. Check the Installer file for more

packages/yarnpkg-core/sources/Project.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ export type InstallOptions = {
138138
};
139139

140140
const INSTALL_STATE_FIELDS = {
141-
restoreInstallersCustomData: [
142-
`installersCustomData`,
141+
restoreLinkersCustomData: [
142+
`linkersCustomData`,
143143
] as const,
144144

145145
restoreResolutions: [
@@ -223,10 +223,10 @@ export class Project {
223223
public peerRequirements: Map<string, PeerRequirement> = new Map();
224224

225225
/**
226-
* Contains whatever data the installers (cf `Linker.ts`) want to persist
226+
* Contains whatever data the linkers (cf `Linker.ts`) want to persist
227227
* from an install to another.
228228
*/
229-
public installersCustomData: Map<string, unknown> = new Map();
229+
public linkersCustomData: Map<string, unknown> = new Map();
230230

231231
/**
232232
* Those checksums are used to detect whether the relevant files actually
@@ -1048,8 +1048,8 @@ export class Project {
10481048
const installers = new Map(linkers.map(linker => {
10491049
const installer = linker.makeInstaller(linkerOptions);
10501050

1051-
const customDataKey = installer.getCustomDataKey();
1052-
const customData = this.installersCustomData.get(customDataKey);
1051+
const customDataKey = linker.getCustomDataKey();
1052+
const customData = this.linkersCustomData.get(customDataKey);
10531053
if (typeof customData !== `undefined`)
10541054
installer.attachCustomData(customData);
10551055

@@ -1246,9 +1246,9 @@ export class Project {
12461246

12471247
// Step 3: Inform our linkers that they should have all the info needed
12481248

1249-
const installersCustomData = new Map();
1249+
const linkersCustomData = new Map();
12501250

1251-
for (const installer of installers.values()) {
1251+
for (const [linker, installer] of installers) {
12521252
const finalizeInstallData = await installer.finalizeInstall();
12531253

12541254
for (const installStatus of finalizeInstallData?.records ?? []) {
@@ -1259,11 +1259,11 @@ export class Project {
12591259
}
12601260

12611261
if (typeof finalizeInstallData?.customData !== `undefined`) {
1262-
installersCustomData.set(installer.getCustomDataKey(), finalizeInstallData.customData);
1262+
linkersCustomData.set(linker.getCustomDataKey(), finalizeInstallData.customData);
12631263
}
12641264
}
12651265

1266-
this.installersCustomData = installersCustomData;
1266+
this.linkersCustomData = linkersCustomData;
12671267

12681268
await miscUtils.allSettledSafe(pendingPromises);
12691269

@@ -1789,7 +1789,7 @@ export class Project {
17891789
this.installStateChecksum = newInstallStateChecksum;
17901790
}
17911791

1792-
async restoreInstallState({restoreInstallersCustomData = true, restoreResolutions = true, restoreBuildState = true}: RestoreInstallStateOpts = {}) {
1792+
async restoreInstallState({restoreLinkersCustomData = true, restoreResolutions = true, restoreBuildState = true}: RestoreInstallStateOpts = {}) {
17931793
const installStatePath = this.configuration.get(`installStatePath`);
17941794

17951795
let installState: InstallState;
@@ -1805,9 +1805,9 @@ export class Project {
18051805
return;
18061806
}
18071807

1808-
if (restoreInstallersCustomData)
1809-
if (typeof installState.installersCustomData !== `undefined`)
1810-
this.installersCustomData = installState.installersCustomData;
1808+
if (restoreLinkersCustomData)
1809+
if (typeof installState.linkersCustomData !== `undefined`)
1810+
this.linkersCustomData = installState.linkersCustomData;
18111811

18121812
if (restoreBuildState)
18131813
Object.assign(this, pick(installState, INSTALL_STATE_FIELDS.restoreBuildState));

0 commit comments

Comments
 (0)