@@ -55,6 +55,24 @@ function nodeModulePath(repoRoot, packageName) {
5555 return path . join ( repoRoot , "node_modules" , ...packageName . split ( "/" ) ) ;
5656}
5757
58+ // Follow Node's importer-relative lookup: hoisted installs can contain several versions,
59+ // and root-only traversal can misclassify a kept dependency as exclusive to an omitted plugin.
60+ function resolveNodeModulePackageDir ( importerDir , packageName ) {
61+ let currentDir = fs . realpathSync ( importerDir ) ;
62+
63+ while ( true ) {
64+ const packageDir = path . join ( currentDir , "node_modules" , ...packageName . split ( "/" ) ) ;
65+ if ( fs . existsSync ( path . join ( packageDir , "package.json" ) ) ) {
66+ return fs . realpathSync ( packageDir ) ;
67+ }
68+ const parentDir = path . dirname ( currentDir ) ;
69+ if ( parentDir === currentDir ) {
70+ return null ;
71+ }
72+ currentDir = parentDir ;
73+ }
74+ }
75+
5876function removeEmptyScopeDir ( repoRoot , packageName ) {
5977 if ( ! packageName . startsWith ( "@" ) ) {
6078 return ;
@@ -68,32 +86,41 @@ function removeEmptyScopeDir(repoRoot, packageName) {
6886 }
6987}
7088
71- function collectPackageRuntimeClosure ( repoRoot , seedPackageNames , options = { } ) {
72- const seen = new Set ( ) ;
73- const stack = [ ...seedPackageNames ] ;
89+ function collectPackageRuntimeClosure ( repoRoot , seeds , options = { } ) {
90+ const packageDirs = new Set ( ) ;
91+ const rootPackageNames = new Set ( ) ;
92+ const stack = [ ...seeds ] ;
7493
7594 while ( stack . length > 0 ) {
76- const packageName = stack . pop ( ) ;
77- if ( ! packageName || seen . has ( packageName ) ) {
95+ const entry = stack . pop ( ) ;
96+ const packageDir = resolveNodeModulePackageDir ( entry . importerDir , entry . packageName ) ;
97+ if ( ! packageDir ) {
7898 continue ;
7999 }
80- seen . add ( packageName ) ;
81100
82- const packageJson = readPackageJson (
83- path . join ( nodeModulePath ( repoRoot , packageName ) , "package.json" ) ,
84- ) ;
101+ const rootPackageDir = nodeModulePath ( repoRoot , entry . packageName ) ;
102+ if (
103+ fs . existsSync ( path . join ( rootPackageDir , "package.json" ) ) &&
104+ fs . realpathSync ( rootPackageDir ) === packageDir
105+ ) {
106+ rootPackageNames . add ( entry . packageName ) ;
107+ }
108+ if ( packageDirs . has ( packageDir ) ) {
109+ continue ;
110+ }
111+ packageDirs . add ( packageDir ) ;
112+
113+ const packageJson = readPackageJson ( path . join ( packageDir , "package.json" ) ) ;
85114 for ( const dependencyName of collectRuntimeDependencyNames ( packageJson , options ) ) {
86- if ( ! seen . has ( dependencyName ) ) {
87- stack . push ( dependencyName ) ;
88- }
115+ stack . push ( { importerDir : packageDir , packageName : dependencyName } ) ;
89116 }
90117 }
91118
92- return seen ;
119+ return { packageDirs , rootPackageNames } ;
93120}
94121
95122function collectWorkspacePackageRuntimeSeeds ( repoRoot , workspaceDir , excludedPluginIds ) {
96- const seeds = new Set ( ) ;
123+ const seeds = [ ] ;
97124 const workspaceRoot = path . join ( repoRoot , workspaceDir ) ;
98125 if ( ! fs . existsSync ( workspaceRoot ) ) {
99126 return seeds ;
@@ -103,12 +130,13 @@ function collectWorkspacePackageRuntimeSeeds(repoRoot, workspaceDir, excludedPlu
103130 if ( ! entry . isDirectory ( ) || excludedPluginIds . has ( entry . name ) ) {
104131 continue ;
105132 }
106- const packageJson = readPackageJson ( path . join ( workspaceRoot , entry . name , "package.json" ) ) ;
133+ const importerDir = path . join ( workspaceRoot , entry . name ) ;
134+ const packageJson = readPackageJson ( path . join ( importerDir , "package.json" ) ) ;
107135 if ( typeof packageJson ?. name === "string" ) {
108- seeds . add ( packageJson . name ) ;
136+ seeds . push ( { importerDir , packageName : packageJson . name } ) ;
109137 }
110- for ( const dependencyName of collectRuntimeDependencyNames ( packageJson ) ) {
111- seeds . add ( dependencyName ) ;
138+ for ( const packageName of collectRuntimeDependencyNames ( packageJson ) ) {
139+ seeds . push ( { importerDir , packageName } ) ;
112140 }
113141 }
114142 return seeds ;
@@ -117,35 +145,27 @@ function collectWorkspacePackageRuntimeSeeds(repoRoot, workspaceDir, excludedPlu
117145function pruneNodeModulesForOmittedPlugins ( repoRoot , bundledPluginDir , omittedPluginIds ) {
118146 const rootPackageJson = readPackageJson ( path . join ( repoRoot , "package.json" ) ) ;
119147 const omittedPackageNames = new Set ( ) ;
120- const omittedSeeds = new Set ( ) ;
148+ const omittedSeeds = [ ] ;
121149
122150 for ( const pluginId of omittedPluginIds ) {
123- const packageJson = readPackageJson (
124- path . join ( repoRoot , bundledPluginDir , pluginId , "package.json" ) ,
125- ) ;
151+ const importerDir = path . join ( repoRoot , bundledPluginDir , pluginId ) ;
152+ const packageJson = readPackageJson ( path . join ( importerDir , "package.json" ) ) ;
126153 if ( typeof packageJson ?. name === "string" ) {
127154 omittedPackageNames . add ( packageJson . name ) ;
128155 }
129- for ( const dependencyName of collectRuntimeDependencyNames ( packageJson ) ) {
130- omittedSeeds . add ( dependencyName ) ;
156+ for ( const packageName of collectRuntimeDependencyNames ( packageJson ) ) {
157+ omittedSeeds . push ( { importerDir , packageName } ) ;
131158 }
132159 }
133160
134- const keptSeeds = new Set ( collectRuntimeDependencyNames ( rootPackageJson ) ) ;
135- for ( const dependencyName of collectWorkspacePackageRuntimeSeeds (
136- repoRoot ,
137- "packages" ,
138- new Set ( ) ,
139- ) ) {
140- keptSeeds . add ( dependencyName ) ;
141- }
142- for ( const dependencyName of collectWorkspacePackageRuntimeSeeds (
143- repoRoot ,
144- bundledPluginDir ,
145- omittedPluginIds ,
146- ) ) {
147- keptSeeds . add ( dependencyName ) ;
148- }
161+ const keptSeeds = [ ...collectRuntimeDependencyNames ( rootPackageJson ) ] . map ( ( packageName ) => ( {
162+ importerDir : repoRoot ,
163+ packageName,
164+ } ) ) ;
165+ keptSeeds . push ( ...collectWorkspacePackageRuntimeSeeds ( repoRoot , "packages" , new Set ( ) ) ) ;
166+ keptSeeds . push (
167+ ...collectWorkspacePackageRuntimeSeeds ( repoRoot , bundledPluginDir , omittedPluginIds ) ,
168+ ) ;
149169
150170 const keptClosure = collectPackageRuntimeClosure ( repoRoot , keptSeeds ) ;
151171 // Hoisted workspace dev dependencies can satisfy optional peers of omitted
@@ -155,18 +175,18 @@ function pruneNodeModulesForOmittedPlugins(repoRoot, bundledPluginDir, omittedPl
155175 includeOptionalPeers : true ,
156176 } ) ;
157177 const removed = [ ] ;
158- const removalCandidates = new Set ( [ ...omittedPackageNames , ...omittedClosure ] ) ;
178+ const removalCandidates = new Set ( [ ...omittedPackageNames , ...omittedClosure . rootPackageNames ] ) ;
159179
160180 for ( const packageName of [ ...removalCandidates ] . toSorted ( ( left , right ) =>
161181 left . localeCompare ( right ) ,
162182 ) ) {
163- if ( keptClosure . has ( packageName ) ) {
164- continue ;
165- }
166183 const packageDir = nodeModulePath ( repoRoot , packageName ) ;
167184 if ( ! fs . existsSync ( packageDir ) ) {
168185 continue ;
169186 }
187+ if ( keptClosure . packageDirs . has ( fs . realpathSync ( packageDir ) ) ) {
188+ continue ;
189+ }
170190 removePathIfExists ( packageDir ) ;
171191 removeEmptyScopeDir ( repoRoot , packageName ) ;
172192 removed . push ( path . relative ( repoRoot , packageDir ) . replaceAll ( "\\" , "/" ) ) ;
0 commit comments