Skip to content

Commit

Permalink
yarn2nix: preserve top-level package.json resolutions field in workspace
Browse files Browse the repository at this point in the history
The `package.json` produced when building node_modules for a workspace
ignores the `resolutions` from the project `package.json`. This results
in dependencies being resolved in a way that conflicts with the
`yarn.lock` file. To fix this, we need to preserve the `resolutions`.
  • Loading branch information
Richard Wallace authored and jsoo1 committed Jul 14, 2023
1 parent 8d06fdc commit 9d3eb87
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions pkgs/development/tools/yarn2nix-moretea/yarn2nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ in rec {
preBuild ? "",
postBuild ? "",
workspaceDependencies ? [], # List of yarn packages
packageResolutions ? {},
}:
let
extraNativeBuildInputs =
Expand All @@ -98,7 +99,7 @@ in rec {

workspaceJSON = pkgs.writeText
"${name}-workspace-package.json"
(builtins.toJSON { private = true; workspaces = ["deps/**"]; }); # scoped packages need second splat
(builtins.toJSON { private = true; workspaces = ["deps/**"]; resolutions = packageResolutions; }); # scoped packages need second splat

workspaceDependencyLinks = lib.concatMapStringsSep "\n"
(dep: ''
Expand Down Expand Up @@ -176,6 +177,8 @@ in rec {

packageGlobs = if lib.isList package.workspaces then package.workspaces else package.workspaces.packages;

packageResolutions = package.resolutions or {};

globElemToRegex = lib.replaceStrings ["*"] [".*"];

# PathGlob -> [PathGlobElem]
Expand Down Expand Up @@ -223,7 +226,7 @@ in rec {
inherit name;
value = mkYarnPackage (
builtins.removeAttrs attrs ["packageOverrides"]
// { inherit src packageJSON yarnLock workspaceDependencies; }
// { inherit src packageJSON yarnLock packageResolutions workspaceDependencies; }
// lib.attrByPath [name] {} packageOverrides
);
})
Expand All @@ -245,6 +248,7 @@ in rec {
extraBuildInputs ? [],
publishBinsFor ? null,
workspaceDependencies ? [], # List of yarnPackages
packageResolutions ? {},
...
}@attrs:
let
Expand All @@ -264,7 +268,7 @@ in rec {
preBuild = yarnPreBuild;
postBuild = yarnPostBuild;
workspaceDependencies = workspaceDependenciesTransitive;
inherit packageJSON pname version yarnLock offlineCache yarnFlags pkgConfig;
inherit packageJSON pname version yarnLock offlineCache yarnFlags pkgConfig packageResolutions;
};

publishBinsFor_ = unlessNull publishBinsFor [pname];
Expand Down Expand Up @@ -298,7 +302,7 @@ in rec {
'')
workspaceDependenciesTransitive;

in stdenv.mkDerivation (builtins.removeAttrs attrs ["yarnNix" "pkgConfig" "workspaceDependencies"] // {
in stdenv.mkDerivation (builtins.removeAttrs attrs ["yarnNix" "pkgConfig" "workspaceDependencies" "packageResolutions"] // {
inherit src pname;

name = baseName;
Expand Down

0 comments on commit 9d3eb87

Please sign in to comment.