@@ -21,7 +21,9 @@ import {
21
21
sdkContext
22
22
} from './helpers' ;
23
23
24
- const verboseEnabled : boolean = typeof process !== 'undefined' && process . env . RUSH_SDK_DEBUG === '1' ;
24
+ const verboseEnabled : boolean =
25
+ typeof process !== 'undefined' &&
26
+ ( process . env . RUSH_SDK_DEBUG === '1' || process . env . _RUSH_SDK_DEBUG === '1' ) ;
25
27
const terminal : Terminal = new Terminal (
26
28
new ConsoleTerminalProvider ( {
27
29
verboseEnabled
@@ -31,6 +33,7 @@ const terminal: Terminal = new Terminal(
31
33
declare const global : typeof globalThis & {
32
34
___rush___rushLibModule ?: RushLibModuleType ;
33
35
___rush___rushLibModuleFromEnvironment ?: RushLibModuleType ;
36
+ ___rush___rushLibModuleFromRushGlobalFolder ?: RushLibModuleType ;
34
37
___rush___rushLibModuleFromInstallAndRunRush ?: RushLibModuleType ;
35
38
} ;
36
39
@@ -42,6 +45,7 @@ if (sdkContext.rushLibModule === undefined) {
42
45
sdkContext . rushLibModule =
43
46
global . ___rush___rushLibModule ||
44
47
global . ___rush___rushLibModuleFromEnvironment ||
48
+ global . ___rush___rushLibModuleFromRushGlobalFolder ||
45
49
global . ___rush___rushLibModuleFromInstallAndRunRush ;
46
50
}
47
51
@@ -111,7 +115,8 @@ if (sdkContext.rushLibModule === undefined) {
111
115
}
112
116
113
117
// SCENARIO 4: A standalone tool or script depends on "rush-sdk", and is meant to be used inside a monorepo folder.
114
- // In this case, we can use install-run-rush.js to obtain the appropriate rush-lib version for the monorepo.
118
+ // In this case, we can first load the rush-lib version in rush global folder. If the expected version is not installed,
119
+ // using install-run-rush.js to obtain the appropriate rush-lib version for the monorepo.
115
120
if ( sdkContext . rushLibModule === undefined ) {
116
121
try {
117
122
const rushJsonPath : string | undefined = tryFindRushJsonLocation ( process . cwd ( ) ) ;
@@ -126,51 +131,78 @@ if (sdkContext.rushLibModule === undefined) {
126
131
const rushJson : JsonObject = JsonFile . load ( rushJsonPath ) ;
127
132
const { rushVersion } = rushJson ;
128
133
129
- const installRunNodeModuleFolder : string = path . join (
130
- monorepoRoot ,
131
- `common/temp/install-run/@microsoft+rush@${ rushVersion } `
132
- ) ;
133
-
134
134
try {
135
- // First, try to load the version of "rush-lib" that was installed by install-run-rush.js
136
- terminal . writeVerboseLine ( `Trying to load ${ RUSH_LIB_NAME } installed by install-run-rush` ) ;
137
- sdkContext . rushLibModule = requireRushLibUnderFolderPath ( installRunNodeModuleFolder ) ;
138
- } catch ( e1 ) {
139
- let installAndRunRushStderrContent : string = '' ;
140
- try {
141
- const installAndRunRushJSPath : string = path . join ( monorepoRoot , 'common/scripts/install-run-rush.js' ) ;
135
+ const { RushGlobalFolder } = require ( '@microsoft/rush-lib/lib-esnext/api/RushGlobalFolder' ) ;
136
+ terminal . writeLine ( RushGlobalFolder ) ;
137
+ terminal . writeVerboseLine ( `Try to load ${ RUSH_LIB_NAME } from rush global folder` ) ;
138
+ const rushGlobalFolder : typeof RushGlobalFolder = new RushGlobalFolder ( ) ;
139
+ // The path needs to keep align with the logic inside RushVersionSelector
140
+ const expectedGlobalRushInstalledFolder : string = path . join (
141
+ rushGlobalFolder . nodeSpecificPath ,
142
+ `rush-${ rushVersion } `
143
+ ) ;
144
+ terminal . writeVerboseLine (
145
+ `The expected global rush installed folder is "${ expectedGlobalRushInstalledFolder } "`
146
+ ) ;
147
+ sdkContext . rushLibModule = requireRushLibUnderFolderPath ( expectedGlobalRushInstalledFolder ) ;
148
+ } catch ( e ) {
149
+ terminal . writeVerboseLine ( `Failed to load ${ RUSH_LIB_NAME } from rush global folder: ${ e . message } ` ) ;
150
+ }
142
151
143
- terminal . writeLine ( 'The Rush engine has not been installed yet. Invoking install-run-rush.js...' ) ;
152
+ if ( sdkContext . rushLibModule !== undefined ) {
153
+ // to track which scenario is active and how it got initialized.
154
+ global . ___rush___rushLibModuleFromRushGlobalFolder = sdkContext . rushLibModule ;
155
+ terminal . writeVerboseLine ( `Loaded ${ RUSH_LIB_NAME } installed from rush global folder` ) ;
156
+ } else {
157
+ const installRunNodeModuleFolder : string = path . join (
158
+ monorepoRoot ,
159
+ `common/temp/install-run/@microsoft+rush@${ rushVersion } `
160
+ ) ;
144
161
145
- const installAndRunRushProcess : SpawnSyncReturns < string > = Executable . spawnSync (
146
- 'node' ,
147
- [ installAndRunRushJSPath , '--help' ] ,
148
- {
149
- stdio : 'pipe'
162
+ try {
163
+ // First, try to load the version of "rush-lib" that was installed by install-run-rush.js
164
+ terminal . writeVerboseLine ( `Trying to load ${ RUSH_LIB_NAME } installed by install-run-rush` ) ;
165
+ sdkContext . rushLibModule = requireRushLibUnderFolderPath ( installRunNodeModuleFolder ) ;
166
+ } catch ( e1 ) {
167
+ let installAndRunRushStderrContent : string = '' ;
168
+ try {
169
+ const installAndRunRushJSPath : string = path . join (
170
+ monorepoRoot ,
171
+ 'common/scripts/install-run-rush.js'
172
+ ) ;
173
+
174
+ terminal . writeLine ( 'The Rush engine has not been installed yet. Invoking install-run-rush.js...' ) ;
175
+
176
+ const installAndRunRushProcess : SpawnSyncReturns < string > = Executable . spawnSync (
177
+ 'node' ,
178
+ [ installAndRunRushJSPath , '--help' ] ,
179
+ {
180
+ stdio : 'pipe'
181
+ }
182
+ ) ;
183
+
184
+ installAndRunRushStderrContent = installAndRunRushProcess . stderr ;
185
+ if ( installAndRunRushProcess . status !== 0 ) {
186
+ throw new Error ( `The ${ RUSH_LIB_NAME } package failed to install` ) ;
150
187
}
151
- ) ;
152
188
153
- installAndRunRushStderrContent = installAndRunRushProcess . stderr ;
154
- if ( installAndRunRushProcess . status !== 0 ) {
155
- throw new Error ( `The ${ RUSH_LIB_NAME } package failed to install` ) ;
189
+ // Retry to load "rush-lib" after install-run-rush run
190
+ terminal . writeVerboseLine (
191
+ `Trying to load ${ RUSH_LIB_NAME } installed by install-run-rush a second time`
192
+ ) ;
193
+ sdkContext . rushLibModule = requireRushLibUnderFolderPath ( installRunNodeModuleFolder ) ;
194
+ } catch ( e2 ) {
195
+ // eslint-disable-next-line no-console
196
+ console . error ( `${ installAndRunRushStderrContent } ` ) ;
197
+ throw new Error ( `The ${ RUSH_LIB_NAME } package failed to load` ) ;
156
198
}
157
-
158
- // Retry to load "rush-lib" after install-run-rush run
159
- terminal . writeVerboseLine (
160
- `Trying to load ${ RUSH_LIB_NAME } installed by install-run-rush a second time`
161
- ) ;
162
- sdkContext . rushLibModule = requireRushLibUnderFolderPath ( installRunNodeModuleFolder ) ;
163
- } catch ( e2 ) {
164
- // eslint-disable-next-line no-console
165
- console . error ( `${ installAndRunRushStderrContent } ` ) ;
166
- throw new Error ( `The ${ RUSH_LIB_NAME } package failed to load` ) ;
167
199
}
168
- }
169
200
170
- if ( sdkContext . rushLibModule !== undefined ) {
171
- // to track which scenario is active and how it got initialized.
172
- global . ___rush___rushLibModuleFromInstallAndRunRush = sdkContext . rushLibModule ;
173
- terminal . writeVerboseLine ( `Loaded ${ RUSH_LIB_NAME } installed by install-run-rush` ) ;
201
+ if ( sdkContext . rushLibModule !== undefined ) {
202
+ // to track which scenario is active and how it got initialized.
203
+ global . ___rush___rushLibModuleFromInstallAndRunRush = sdkContext . rushLibModule ;
204
+ terminal . writeVerboseLine ( `Loaded ${ RUSH_LIB_NAME } installed by install-run-rush` ) ;
205
+ }
174
206
}
175
207
} catch ( e ) {
176
208
// no-catch
0 commit comments