Skip to content

Commit

Permalink
refactor(core)!: replace workspace.dependencies with `worskpace.anc…
Browse files Browse the repository at this point in the history
…horedPackage` (#4898)

* refactor(core)!: replace `workspace.dependencies` with `getPackage`

* chore: changelog

* refactor: `getPackage` -> `get anchoredPackage`
  • Loading branch information
paul-soporan authored Oct 7, 2022
1 parent 659d00f commit 578dbe7
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 49 deletions.
34 changes: 34 additions & 0 deletions .yarn/versions/a53c2cda.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
releases:
"@yarnpkg/cli": major
"@yarnpkg/core": major
"@yarnpkg/plugin-essentials": patch
"@yarnpkg/plugin-npm-cli": patch
"@yarnpkg/plugin-pnp": patch

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@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-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- "@yarnpkg/builder"
- "@yarnpkg/doctor"
- "@yarnpkg/extensions"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ The following changes only affect people writing Yarn plugins:

- `FakeFS` classes are now required to implement `lutimes{Sync,Promise}`.

- `workspace.dependencies` has been removed. Use `workspace.anchoredPackage.dependencies` instead.

### Installs

- The `pnpm` linker avoids creating symlinks that lead to loops on the file system, by moving them higher up in the directory structure.
Expand Down
18 changes: 4 additions & 14 deletions packages/plugin-essentials/sources/commands/why.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,8 @@ function whyRecursive(project: Project, identHash: IdentHash, {configuration, pe
return depends;
};

for (const workspace of sortedWorkspaces) {
const pkg = project.storedPackages.get(workspace.anchoredLocator.locatorHash);
if (!pkg)
throw new Error(`Assertion failed: The package should have been registered`);

markAllDependents(pkg);
}
for (const workspace of sortedWorkspaces)
markAllDependents(workspace.anchoredPackage);

const printed: Set<LocatorHash> = new Set();

Expand Down Expand Up @@ -209,13 +204,8 @@ function whyRecursive(project: Project, identHash: IdentHash, {configuration, pe
}
};

for (const workspace of sortedWorkspaces) {
const pkg = project.storedPackages.get(workspace.anchoredLocator.locatorHash);
if (!pkg)
throw new Error(`Assertion failed: The package should have been registered`);

printAllDependents(pkg, rootChildren, null);
}
for (const workspace of sortedWorkspaces)
printAllDependents(workspace.anchoredPackage, rootChildren, null);

return root;
}
2 changes: 1 addition & 1 deletion packages/plugin-npm-cli/sources/npmAuditUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function getTransitiveDevDependencies(project: Project, workspace: Workspace, {a

// Map workspace dependencies to descriptor hashes, filtered by the top-level production and development dependencies
const workspaceDependencies = workspaces
.map(workspace => [...workspace.dependencies.values()])
.map(workspace => [...workspace.anchoredPackage.dependencies.values()])
.flat();

const productionRoots = workspaceDependencies
Expand Down
9 changes: 2 additions & 7 deletions packages/plugin-pnp/sources/commands/unplug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,8 @@ export default class UnplugCommand extends BaseCommand {
}
};

for (const workspace of roots) {
const pkg = project.storedPackages.get(workspace.anchoredLocator.locatorHash);
if (!pkg)
throw new Error(`Assertion failed: The package should have been registered`);

traverse(pkg, 0);
}
for (const workspace of roots)
traverse(workspace.anchoredPackage, 0);

return selection;
};
Expand Down
20 changes: 0 additions & 20 deletions packages/yarnpkg-core/sources/Project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,20 +531,6 @@ export class Project {
return workspace;
}

/**
* Import the dependencies of each resolved workspace into their own
* `Workspace` instance.
*/
private refreshWorkspaceDependencies() {
for (const workspace of this.workspaces) {
const pkg = this.storedPackages.get(workspace.anchoredLocator.locatorHash);
if (!pkg)
throw new Error(`Assertion failed: Expected workspace ${structUtils.prettyWorkspace(this.configuration, workspace)} (${formatUtils.pretty(this.configuration, ppath.join(workspace.cwd, Filename.manifest), formatUtils.Type.PATH)}) to have been resolved. Run "yarn install" to update the lockfile`);

workspace.dependencies = new Map(pkg.dependencies);
}
}

forgetResolution(descriptor: Descriptor): void;
forgetResolution(locator: Locator): void;
forgetResolution(dataStructure: Descriptor | Locator): void {
Expand Down Expand Up @@ -943,11 +929,6 @@ export class Project {
this.originalPackages = originalPackages;
this.optionalBuilds = optionalBuilds;
this.peerRequirements = peerRequirements;

// Now that the internal resolutions have been updated, we can refresh the
// dependencies of each resolved workspace's `Workspace` instance.

this.refreshWorkspaceDependencies();
}

async fetchEverything({cache, report, fetcher: userFetcher, mode}: InstallOptions) {
Expand Down Expand Up @@ -1808,7 +1789,6 @@ export class Project {
if (restoreResolutions) {
if (installState.lockFileChecksum === this.lockFileChecksum) {
Object.assign(this, pick(installState, INSTALL_STATE_FIELDS.restoreResolutions));
this.refreshWorkspaceDependencies();
} else {
await this.applyLightResolution();
}
Expand Down
13 changes: 9 additions & 4 deletions packages/yarnpkg-core/sources/Workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import globby from 'globby';
import {HardDependencies, Manifest} from './Manifest';
import {Project} from './Project';
import {WorkspaceResolver} from './WorkspaceResolver';
import * as formatUtils from './formatUtils';
import * as hashUtils from './hashUtils';
import * as semverUtils from './semverUtils';
import * as structUtils from './structUtils';
import {IdentHash} from './types';
import {Descriptor, Locator} from './types';

export class Workspace {
Expand All @@ -31,9 +31,6 @@ export class Workspace {

public readonly workspacesCwds: Set<PortablePath> = new Set();

// Generated at resolution; basically dependencies + devDependencies + child workspaces
public dependencies: Map<IdentHash, Descriptor> = new Map();

constructor(workspaceCwd: PortablePath, {project}: {project: Project}) {
this.project = project;
this.cwd = workspaceCwd;
Expand Down Expand Up @@ -81,6 +78,14 @@ export class Workspace {
}
}

get anchoredPackage() {
const pkg = this.project.storedPackages.get(this.anchoredLocator.locatorHash);
if (!pkg)
throw new Error(`Assertion failed: Expected workspace ${structUtils.prettyWorkspace(this.project.configuration, this)} (${formatUtils.pretty(this.project.configuration, ppath.join(this.cwd, Filename.manifest), formatUtils.Type.PATH)}) to have been resolved. Run "yarn install" to update the lockfile`);

return pkg;
}

accepts(range: string) {
const protocolIndex = range.indexOf(`:`);

Expand Down
6 changes: 3 additions & 3 deletions packages/yarnpkg-core/tests/Project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe(`Project`, () => {
report: new ThrowReport(),
});

const topLevelPkg = project.storedPackages.get(project.topLevelWorkspace.anchoredLocator.locatorHash)!;
const topLevelPkg = project.topLevelWorkspace.anchoredPackage;

const fooIdent = structUtils.makeIdent(null, `foo`);
const barIdent = structUtils.makeIdent(null, `bar`);
Expand Down Expand Up @@ -115,7 +115,7 @@ describe(`Project`, () => {

const xxx = project.getWorkspaceByIdent(structUtils.makeIdent(null, `xxx`));

const yyyDescriptor = xxx.dependencies.get(structUtils.makeIdent(null, `yyy`).identHash)!;
const yyyDescriptor = xxx.anchoredPackage.dependencies.get(structUtils.makeIdent(null, `yyy`).identHash)!;
const yyyResolution = project.storedResolutions.get(yyyDescriptor.descriptorHash)!;
const yyy = project.storedPackages.get(yyyResolution)!;

Expand All @@ -127,7 +127,7 @@ describe(`Project`, () => {
const expectedDescriptor = structUtils.makeDescriptor(ident, `npm:^1.0.0`);

// This one is a sanity check
expect(xxx.dependencies.get(ident.identHash)).toEqual(expectedDescriptor);
expect(xxx.anchoredPackage.dependencies.get(ident.identHash)).toEqual(expectedDescriptor);

// This one is the real check
expect(zzz.dependencies.get(ident.identHash)).toEqual(expectedDescriptor);
Expand Down

0 comments on commit 578dbe7

Please sign in to comment.