@@ -26,6 +26,8 @@ import handleSignals from '../util/signal-handler.js';
26
26
import { boolify , boolifyWithDefault } from '../util/conversion.js' ;
27
27
import { ProcessTermError } from '../errors' ;
28
28
29
+ const chalk = require ( 'chalk' ) ;
30
+
29
31
process . stdout . prependListener ( 'error' , err => {
30
32
// swallow err only if downstream consumer process closed pipe early
31
33
if ( err . code === 'EPIPE' || err . code === 'ERR_STREAM_DESTROYED' ) {
@@ -263,6 +265,39 @@ export async function main({
263
265
reporter . initPeakMemoryCounter ( ) ;
264
266
265
267
const config = new Config ( reporter ) ;
268
+
269
+ const projectRoot = findProjectRoot ( commander . cwd ) ;
270
+ const cwd = command . shouldRunInCurrentCwd ? commander . cwd : projectRoot ;
271
+
272
+ if ( ! process . env . COREPACK_ROOT ) {
273
+ const rootManifest = await config . readRootManifest ( projectRoot ) ;
274
+ if ( typeof rootManifest . packageManager === `string` ) {
275
+ if ( ! rootManifest . packageManager . match ( / ^ y a r n @ [ 0 1 ] \. / ) ) {
276
+ reporter . error (
277
+ `This project's package.json defines ${ chalk . gray ( '"packageManager": "yarn@' ) } ${ chalk . yellow (
278
+ `${ rootManifest . packageManager . replace ( / ^ y a r n @ / , `` ) . replace ( / \+ .* / , `` ) } ` ,
279
+ ) } ${ chalk . gray ( `"` ) } . However the current global version of Yarn is ${ chalk . yellow ( version ) } .`,
280
+ ) ;
281
+
282
+ process . stderr . write ( `\n` ) ;
283
+ process . stderr . write (
284
+ `Presence of the ${ chalk . gray (
285
+ `"packageManager"` ,
286
+ ) } field indicates that the project is meant to be used with Corepack, a tool included by default with all official Node.js distributions starting from 16.9 and 14.19.\n`,
287
+ ) ;
288
+
289
+ process . stderr . write (
290
+ `Corepack must currently be enabled by running ${ chalk . magenta (
291
+ `corepack enable` ,
292
+ ) } in your terminal. For more information, check out ${ chalk . blueBright ( `https://yarnpkg.com/corepack` ) } .\n`,
293
+ ) ;
294
+
295
+ exit ( 1 ) ;
296
+ return ;
297
+ }
298
+ }
299
+ }
300
+
266
301
const outputWrapperEnabled = boolifyWithDefault ( process . env . YARN_WRAP_OUTPUT , true ) ;
267
302
const shouldWrapOutput =
268
303
outputWrapperEnabled &&
@@ -466,61 +501,6 @@ export async function main({
466
501
} ) ;
467
502
} ;
468
503
469
- function onUnexpectedError ( err : Error ) {
470
- function indent ( str : string ) : string {
471
- return '\n ' + str . trim ( ) . split ( '\n' ) . join ( '\n ' ) ;
472
- }
473
-
474
- const log = [ ] ;
475
- log . push ( `Arguments: ${ indent ( process . argv . join ( ' ' ) ) } ` ) ;
476
- log . push ( `PATH: ${ indent ( process . env . PATH || 'undefined' ) } ` ) ;
477
- log . push ( `Yarn version: ${ indent ( version ) } ` ) ;
478
- log . push ( `Node version: ${ indent ( process . versions . node ) } ` ) ;
479
- log . push ( `Platform: ${ indent ( process . platform + ' ' + process . arch ) } ` ) ;
480
-
481
- log . push ( `Trace: ${ indent ( err . stack ) } ` ) ;
482
-
483
- // add manifests
484
- for ( const registryName of registryNames ) {
485
- const possibleLoc = path . join ( config . cwd , registries [ registryName ] . filename ) ;
486
- const manifest = fs . existsSync ( possibleLoc ) ? fs . readFileSync ( possibleLoc , 'utf8' ) : 'No manifest' ;
487
- log . push ( `${ registryName } manifest: ${ indent ( manifest ) } ` ) ;
488
- }
489
-
490
- // lockfile
491
- const lockLoc = path . join (
492
- config . lockfileFolder || config . cwd , // lockfileFolder might not be set at this point
493
- constants . LOCKFILE_FILENAME ,
494
- ) ;
495
- const lockfile = fs . existsSync ( lockLoc ) ? fs . readFileSync ( lockLoc , 'utf8' ) : 'No lockfile' ;
496
- log . push ( `Lockfile: ${ indent ( lockfile ) } ` ) ;
497
-
498
- const errorReportLoc = writeErrorReport ( log ) ;
499
-
500
- reporter . error ( reporter . lang ( 'unexpectedError' , err . message ) ) ;
501
-
502
- if ( errorReportLoc ) {
503
- reporter . info ( reporter . lang ( 'bugReport' , errorReportLoc ) ) ;
504
- }
505
- }
506
-
507
- function writeErrorReport ( log ) : ?string {
508
- const errorReportLoc = config . enableMetaFolder
509
- ? path . join ( config . cwd , constants . META_FOLDER , 'yarn-error.log' )
510
- : path . join ( config . cwd , 'yarn-error.log' ) ;
511
-
512
- try {
513
- fs . writeFileSync ( errorReportLoc , log . join ( '\n\n' ) + '\n' ) ;
514
- } catch ( err ) {
515
- reporter . error ( reporter . lang ( 'fileWriteError' , errorReportLoc , err . message ) ) ;
516
- return undefined ;
517
- }
518
-
519
- return errorReportLoc ;
520
- }
521
-
522
- const cwd = command . shouldRunInCurrentCwd ? commander . cwd : findProjectRoot ( commander . cwd ) ;
523
-
524
504
const folderOptionKeys = [ 'linkFolder' , 'globalFolder' , 'preferredCacheFolder' , 'cacheFolder' , 'modulesFolder' ] ;
525
505
526
506
// Resolve all folder options relative to cwd
@@ -608,8 +588,6 @@ export async function main({
608
588
609
589
if ( err instanceof MessageError ) {
610
590
reporter . error ( err . message ) ;
611
- } else {
612
- onUnexpectedError ( err ) ;
613
591
}
614
592
615
593
if ( command . getDocsInfo ) {
0 commit comments