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25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#!/usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "nextInfo", {
    enumerable: true,
    get: function() {
        return nextInfo;
    }
});
const _os = /*#__PURE__*/ _interop_require_default(require("os"));
const _child_process = /*#__PURE__*/ _interop_require_default(require("child_process"));
const _picocolors = require("../lib/picocolors");
const _constants = require("../shared/lib/constants");
const _config = /*#__PURE__*/ _interop_require_default(require("../server/config"));
const _getregistry = require("../lib/helpers/get-registry");
const _parseversioninfo = require("../server/dev/parse-version-info");
const _versionstaleness = require("../next-devtools/shared/version-staleness");
const _log = require("../build/output/log");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
function getPackageVersion(packageName) {
    try {
        return require(`${packageName}/package.json`).version;
    } catch  {
        return 'N/A';
    }
}
async function getNextConfig() {
    var _config_experimental;
    const config = await (0, _config.default)(_constants.PHASE_INFO, process.cwd());
    return {
        output: config.output ?? 'N/A',
        experimental: {
            useWasmBinary: (_config_experimental = config.experimental) == null ? void 0 : _config_experimental.useWasmBinary
        }
    };
}
/**
 * Returns the version of the specified binary, by supplying `--version` argument.
 * N/A if it fails to run the binary.
 */ function getBinaryVersion(binaryName) {
    try {
        return _child_process.default.execFileSync(binaryName, [
            '--version'
        ]).toString().trim();
    } catch  {
        try {
            return _child_process.default.execSync(`${binaryName} --version`).toString().trim();
        } catch  {
            return 'N/A';
        }
    }
}
/**
 * Collect basic next.js installation information and print it to stdout.
 */ async function printInfo() {
    const installedRelease = getPackageVersion('next');
    const nextConfig = await getNextConfig();
    let stalenessWithTitle = '';
    let title = '';
    let versionInfo;
    try {
        const registry = (0, _getregistry.getRegistry)();
        const res = await fetch(`${registry}-/package/next/dist-tags`);
        const tags = await res.json();
        versionInfo = (0, _parseversioninfo.parseVersionInfo)({
            installed: installedRelease,
            latest: tags.latest,
            canary: tags.canary
        });
        title = (0, _versionstaleness.getStaleness)(versionInfo).title;
        if (title) {
            stalenessWithTitle = ` // ${title}`;
        }
    } catch (e) {
        console.warn(`${(0, _picocolors.yellow)((0, _picocolors.bold)('warn'))}  - Failed to fetch latest canary version. (Reason: ${e.message}.)
      Detected "${installedRelease}". Visit https://github.com/vercel/next.js/releases.
      Make sure to try the latest canary version (eg.: \`npm install next@canary\`) to confirm the issue still exists before creating a new issue.
      \nLearn more: ${(0, _picocolors.cyan)('https://nextjs.org/docs/messages/opening-an-issue')}`);
    }
    const cpuCores = _os.default.cpus().length;
    let relevantPackages = `  next: ${installedRelease}${stalenessWithTitle}
  eslint-config-next: ${getPackageVersion('eslint-config-next')}
  react: ${getPackageVersion('react')}
  react-dom: ${getPackageVersion('react-dom')}
  typescript: ${getPackageVersion('typescript')}`;
    if (process.env.NEXT_RSPACK) {
        relevantPackages += `
  next-rspack: ${getPackageVersion('next-rspack')}`;
    }
    console.log(`
Operating System:
  Platform: ${_os.default.platform()}
  Arch: ${_os.default.arch()}
  Version: ${_os.default.version()}
  Available memory (MB): ${Math.ceil(_os.default.totalmem() / 1024 / 1024)}
  Available CPU cores: ${cpuCores > 0 ? cpuCores : 'N/A'}
Binaries:
  Node: ${process.versions.node}
  npm: ${getBinaryVersion('npm')}
  Yarn: ${getBinaryVersion('yarn')}
  pnpm: ${getBinaryVersion('pnpm')}
Relevant Packages:
${relevantPackages}
Next.js Config:
  output: ${nextConfig.output}`);
    if (versionInfo == null ? void 0 : versionInfo.staleness.startsWith('stale')) {
        (0, _log.warn)(`${title}
   Please try the latest canary version (\`npm install next@canary\`) to confirm the issue still exists before creating a new issue.
   Read more - https://nextjs.org/docs/messages/opening-an-issue`);
    }
}
/**
 * Using system-installed tools per each platform, trying to read shared dependencies of next-swc.
 * This is mainly for debugging DLOPEN failure.
 *
 * We don't / can't install these tools by ourselves, will skip the check if we can't find them.
 */ async function runSharedDependencyCheck(tools, skipMessage) {
    var _getSupportedArchTriples_currentPlatform;
    const currentPlatform = _os.default.platform();
    const spawn = require('next/dist/compiled/cross-spawn');
    const { getSupportedArchTriples } = require('../build/swc');
    const triples = ((_getSupportedArchTriples_currentPlatform = getSupportedArchTriples()[currentPlatform]) == null ? void 0 : _getSupportedArchTriples_currentPlatform[_os.default.arch()]) ?? [];
    // First, check if system have a tool installed. We can't install these by our own.
    const availableTools = [];
    for (const tool of tools){
        try {
            const check = spawn.sync(tool.bin, tool.checkArgs);
            if (check.status === 0) {
                availableTools.push(tool);
            }
        } catch  {
        // ignore if existence check fails
        }
    }
    if (availableTools.length === 0) {
        return {
            messages: skipMessage,
            result: 'skipped'
        };
    }
    const outputs = [];
    let result = 'fail';
    for (const triple of triples){
        const triplePkgName = `@next/swc-${triple.platformArchABI}`;
        let resolved;
        try {
            resolved = require.resolve(triplePkgName);
        } catch (e) {
            return {
                messages: 'Cannot find next-swc installation, skipping dependencies check',
                result: 'skipped'
            };
        }
        for (const tool of availableTools){
            const proc = spawn(tool.bin, [
                ...tool.args,
                resolved
            ]);
            outputs.push(`Running ${tool.bin} ------------- `);
            // Captures output, doesn't matter if it fails or not since we'll forward both to output.
            const procPromise = new Promise((resolve)=>{
                proc.stdout.on('data', function(data) {
                    outputs.push(data);
                });
                proc.stderr.on('data', function(data) {
                    outputs.push(data);
                });
                proc.on('close', (c)=>resolve(c));
            });
            let code = await procPromise;
            if (code === 0) {
                result = 'pass';
            }
        }
    }
    return {
        output: outputs.join('\n'),
        result
    };
}
/**
 * Collect additional diagnostics information.
 */ async function printVerboseInfo() {
    const fs = require('fs');
    const currentPlatform = _os.default.platform();
    if (currentPlatform !== 'win32' && currentPlatform !== 'linux' && currentPlatform !== 'darwin') {
        console.log('Unsupported platform, only win32, linux, darwin are supported.');
        return;
    }
    // List of tasks to run.
    const tasks = [
        {
            title: 'Host system information',
            scripts: {
                default: async ()=>{
                    // Node.js diagnostic report contains basic information, i.e OS version, CPU architecture, etc.
                    // Only collect few addtional details here.
                    const isWsl = require('next/dist/compiled/is-wsl');
                    const ciInfo = require('next/dist/compiled/ci-info');
                    const isDocker = require('next/dist/compiled/is-docker');
                    const output = `
  WSL: ${isWsl}
  Docker: ${isDocker()}
  CI: ${ciInfo.isCI ? ciInfo.name || 'unknown' : 'false'}
`;
                    return {
                        output,
                        result: 'pass'
                    };
                }
            }
        },
        {
            title: 'Next.js installation',
            scripts: {
                default: async ()=>{
                    const installedRelease = getPackageVersion('next');
                    const nextConfig = await getNextConfig();
                    const output = `
  Binaries:
    Node: ${process.versions.node}
    npm: ${getBinaryVersion('npm')}
    Yarn: ${getBinaryVersion('yarn')}
    pnpm: ${getBinaryVersion('pnpm')}
  Relevant Packages:
    next: ${installedRelease}
    eslint-config-next: ${getPackageVersion('eslint-config-next')}
    react: ${getPackageVersion('react')}
    react-dom: ${getPackageVersion('react-dom')}
    typescript: ${getPackageVersion('typescript')}
  Next.js Config:
    output: ${nextConfig.output}

`;
                    return {
                        output,
                        result: 'pass'
                    };
                }
            }
        },
        {
            title: 'Node.js diagnostic report',
            scripts: {
                default: async ()=>{
                    var _process_report;
                    const report = (_process_report = process.report) == null ? void 0 : _process_report.getReport();
                    if (!report) {
                        return {
                            messages: 'Node.js diagnostic report is not available.',
                            result: 'fail'
                        };
                    }
                    const { header, javascriptHeap, sharedObjects } = report;
                    // Delete some fields potentially containing sensitive information.
                    header == null ? true : delete header.cwd;
                    header == null ? true : delete header.commandLine;
                    header == null ? true : delete header.host;
                    header == null ? true : delete header.cpus;
                    header == null ? true : delete header.networkInterfaces;
                    const reportSummary = {
                        header,
                        javascriptHeap,
                        sharedObjects
                    };
                    return {
                        output: JSON.stringify(reportSummary, null, 2),
                        result: 'pass'
                    };
                }
            }
        },
        {
            title: 'next-swc installation',
            scripts: {
                default: async ()=>{
                    var _platformArchTriples_currentPlatform;
                    const output = [];
                    // First, try to load next-swc via loadBindings.
                    try {
                        var _nextConfig_experimental;
                        let nextConfig = await getNextConfig();
                        const { loadBindings } = require('../build/swc');
                        const bindings = await loadBindings((_nextConfig_experimental = nextConfig.experimental) == null ? void 0 : _nextConfig_experimental.useWasmBinary);
                        // Run arbitrary function to verify the bindings are loaded correctly.
                        const target = bindings.getTargetTriple();
                        // We think next-swc is installed correctly if getTargetTriple returns.
                        return {
                            output: `next-swc is installed correctly for ${target}`,
                            result: 'pass'
                        };
                    } catch (e) {
                        output.push(`loadBindings() failed: ${e.message}`);
                    }
                    const { platformArchTriples } = require('next/dist/compiled/@napi-rs/triples');
                    const triples = (_platformArchTriples_currentPlatform = platformArchTriples[currentPlatform]) == null ? void 0 : _platformArchTriples_currentPlatform[_os.default.arch()];
                    if (!triples || triples.length === 0) {
                        return {
                            messages: `No target triples found for ${currentPlatform} / ${_os.default.arch()}`,
                            result: 'fail'
                        };
                    }
                    // Trying to manually resolve corresponding target triples to see if bindings are physically located.
                    const path = require('path');
                    let fallbackBindingsDirectory;
                    try {
                        const nextPath = path.dirname(require.resolve('next/package.json'));
                        fallbackBindingsDirectory = path.join(nextPath, 'next-swc-fallback');
                    } catch (e) {
                    // Not able to locate next package from current running location, skipping fallback bindings check.
                    }
                    const tryResolve = (pkgName)=>{
                        try {
                            const resolved = require.resolve(pkgName);
                            const fileExists = fs.existsSync(resolved);
                            let loadError;
                            let loadSuccess;
                            try {
                                loadSuccess = !!require(resolved).getTargetTriple();
                            } catch (e) {
                                loadError = e.message;
                            }
                            output.push(`${pkgName} exists: ${fileExists} for the triple ${loadSuccess}`);
                            if (loadError) {
                                output.push(`${pkgName} load failed: ${loadError ?? 'unknown'}`);
                            }
                            if (loadSuccess) {
                                return true;
                            }
                        } catch (e) {
                            output.push(`${pkgName} resolve failed: ${e.message ?? 'unknown'}`);
                        }
                        return false;
                    };
                    for (const triple of triples){
                        const triplePkgName = `@next/swc-${triple.platformArchABI}`;
                        // Check installed optional dependencies. This is the normal way package being installed.
                        // For the targets have multiple triples (gnu / musl), if any of them loads successfully, we consider as installed.
                        if (tryResolve(triplePkgName)) {
                            break;
                        }
                        // Check if fallback binaries are installed.
                        if (!fallbackBindingsDirectory) {
                            continue;
                        }
                        tryResolve(path.join(fallbackBindingsDirectory, triplePkgName));
                    }
                    return {
                        output: output.join('\n'),
                        result: 'pass'
                    };
                }
            }
        },
        {
            // For the simplicity, we only check the correctly installed optional dependencies -
            // as this is mainly for checking DLOPEN failure. If user hit MODULE_NOT_FOUND,
            // expect above next-swc installation would give some hint instead.
            title: 'next-swc shared object dependencies',
            scripts: {
                linux: async ()=>{
                    const skipMessage = 'This diagnostics uses system-installed tools (ldd) to check next-swc dependencies, but it is not found. Skipping dependencies check.';
                    return await runSharedDependencyCheck([
                        {
                            bin: 'ldd',
                            checkArgs: [
                                '--help'
                            ],
                            args: [
                                '--verbose'
                            ]
                        }
                    ], skipMessage);
                },
                win32: async ()=>{
                    const skipMessage = `This diagnostics uses system-installed tools (dumpbin.exe) to check next-swc dependencies, but it was not found in the path. Skipping dependencies check.
          dumpbin (https://learn.microsoft.com/en-us/cpp/build/reference/dumpbin-reference) is a part of Microsoft VC toolset,
          can be installed with Windows SDK, Windows Build tools or Visual Studio.

          Please make sure you have one of them installed and dumpbin.exe is in the path.
          `;
                    return await runSharedDependencyCheck([
                        {
                            bin: 'dumpbin.exe',
                            checkArgs: [
                                '/summary'
                            ],
                            args: [
                                '/imports'
                            ]
                        }
                    ], skipMessage);
                },
                darwin: async ()=>{
                    const skipMessage = 'This diagnostics uses system-installed tools (otools, dyld_info) to check next-swc dependencies, but none of them are found. Skipping dependencies check.';
                    return await runSharedDependencyCheck([
                        {
                            bin: 'otool',
                            checkArgs: [
                                '--version'
                            ],
                            args: [
                                '-L'
                            ]
                        },
                        {
                            bin: 'dyld_info',
                            checkArgs: [],
                            args: []
                        }
                    ], skipMessage);
                }
            }
        }
    ];
    // Collected output after running all tasks.
    const report = [];
    console.log('\n');
    for (const task of tasks){
        if (task.targetPlatform && task.targetPlatform !== currentPlatform) {
            report.push({
                title: task.title,
                result: {
                    messages: undefined,
                    output: `[SKIPPED (${_os.default.platform()} / ${task.targetPlatform})] ${task.title}`,
                    result: 'skipped'
                }
            });
            continue;
        }
        const taskScript = task.scripts[currentPlatform] ?? task.scripts.default;
        let taskResult;
        try {
            taskResult = await taskScript();
        } catch (e) {
            taskResult = {
                messages: `Unexpected failure while running diagnostics: ${e.message}`,
                result: 'fail'
            };
        }
        console.log(`- ${task.title}: ${taskResult.result}`);
        if (taskResult.messages) {
            console.log(`  ${taskResult.messages}`);
        }
        report.push({
            title: task.title,
            result: taskResult
        });
    }
    console.log(`\n${(0, _picocolors.bold)('Generated diagnostics report')}`);
    console.log(`\nPlease copy below report and paste it into your issue.`);
    for (const { title, result } of report){
        console.log(`\n### ${title}`);
        if (result.messages) {
            console.log(result.messages);
        }
        if (result.output) {
            console.log(result.output);
        }
    }
}
/**
 * Runs few scripts to collect system information to help with debugging next.js installation issues.
 * There are 2 modes, by default it collects basic next.js installation with runtime information. If
 * `--verbose` mode is enabled it'll try to collect, verify more data for next-swc installation and others.
 */ const nextInfo = async (options)=>{
    if (options.verbose) {
        await printVerboseInfo();
    } else {
        await printInfo();
    }
};

//# sourceMappingURL=next-info.js.map