Skip to content

[WIP] - Handle optional dependencies alongside resolutions #7272

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

Closed
wants to merge 2 commits into from
Closed
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
36 changes: 27 additions & 9 deletions src/cli/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import type {InstallationMethod} from '../../util/yarn-version.js';
import type {Reporter} from '../../reporters/index.js';
import type {ReporterSelectOption} from '../../reporters/types.js';
import type {Manifest, DependencyRequestPatterns} from '../../types.js';
import type {Manifest, DependencyRequestPatterns, DependencyRequestPattern} from '../../types.js';
import type Config, {RootManifests} from '../../config.js';
import type {RegistryNames} from '../../registries/index.js';
import type {LockfileObject} from '../../lockfile';
Expand Down Expand Up @@ -202,6 +202,7 @@ export class Install {
this.integrityChecker = new InstallationIntegrityChecker(config);
this.linker = new PackageLinker(config, this.resolver);
this.scripts = new PackageInstallScripts(config, this.resolver, this.flags.force);
this.depsByPattern = new Map(); // Keep track of dependencies for access by key
}

flags: Flags;
Expand All @@ -217,6 +218,7 @@ export class Install {
rootPatternsToOrigin: {[pattern: string]: string};
integrityChecker: InstallationIntegrityChecker;
resolutionMap: ResolutionMap;
depsByPattern: Map<string, DependencyRequestPattern>;

/**
* Create a list of dependency requests from the current directories manifests.
Expand Down Expand Up @@ -283,13 +285,6 @@ export class Install {
Object.assign(this.resolutions, projectManifestJson.resolutions);
Object.assign(manifest, projectManifestJson);

this.resolutionMap.init(this.resolutions);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is moved to after pushDeps is so that we know what dependencies to not add

for (const packageName of Object.keys(this.resolutionMap.resolutionsByPackage)) {
for (const {pattern} of this.resolutionMap.resolutionsByPackage[packageName]) {
resolutionDeps = [...resolutionDeps, {registry, pattern, optional: false, hint: 'resolution'}];
}
}

const pushDeps = (
depType,
manifest: Object,
Expand All @@ -306,6 +301,9 @@ export class Install {
return;
}
const depMap = manifest[depType];
const workspaceName = manifest.name;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done to pass linter line length below

const workspaceLoc = manifest._loc;

for (const name in depMap) {
if (excludeNames.indexOf(name) >= 0) {
continue;
Expand All @@ -327,7 +325,13 @@ export class Install {

this.rootPatternsToOrigin[pattern] = depType;
patterns.push(pattern);
deps.push({pattern, registry, hint, optional, workspaceName: manifest.name, workspaceLoc: manifest._loc});

// Not ideal, but we need to track deps as key/values
// to avoid iterating over the entire collection when
// handling optional dependencies
const depObject = {pattern, registry, hint, optional, workspaceName, workspaceLoc};
this.depsByPattern.set(pattern, depObject);
deps.push(depObject);
}
};

Expand All @@ -337,6 +341,20 @@ export class Install {
pushDeps('optionalDependencies', projectManifestJson, {hint: 'optional', optional: true}, true);
}

this.resolutionMap.init(this.resolutions);
for (const packageName of Object.keys(this.resolutionMap.resolutionsByPackage)) {
for (const {pattern} of this.resolutionMap.resolutionsByPackage[packageName]) {
// Get the existing dep and check if it is optional
const existingDep = this.depsByPattern.get(pattern);
const depIsOptional = existingDep && existingDep.optional;

// Add the dependency to resolutionDeps only if it is *not* optional
if (!depIsOptional) {
resolutionDeps = [...resolutionDeps, {registry, pattern, optional: false, hint: 'resolution'}];
}
}
}

if (this.config.workspaceRootFolder) {
const workspaceLoc = cwdIsRoot ? loc : path.join(this.config.lockfileFolder, filename);
const workspacesRoot = path.dirname(workspaceLoc);
Expand Down