Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(webpack): create build log for remotes to help debug errors #22539

Merged
merged 1 commit into from Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,7 +1,10 @@
import { type Schema } from '../schema';
import { logger, type ExecutorContext } from '@nx/devkit';
import { fork } from 'child_process';
import { type ExecutorContext, logger } from '@nx/devkit';
import type { StaticRemotesConfig } from './parse-static-remotes-config';
import { projectGraphCacheDirectory } from 'nx/src/utils/cache-directory';
import { fork } from 'node:child_process';
import { join } from 'node:path';
import { createWriteStream } from 'node:fs';
Comment on lines +5 to +7
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will we run into issues with this style of import for users on older versions of Node?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's supported since Node 16, and we already use it in a few places.


export async function buildStaticRemotes(
staticRemotesConfig: StaticRemotesConfig,
Expand Down Expand Up @@ -44,10 +47,17 @@ export async function buildStaticRemotes(
stdio: ['ignore', 'pipe', 'pipe', 'ipc'],
}
);
// File to debug build failures e.g. 2024-01-01T00_00_0_0Z-build.log'
const remoteBuildLogFile = join(
projectGraphCacheDirectory,
`${new Date().toISOString().replace(/[:\.]/g, '_')}-build.log`
);
const stdoutStream = createWriteStream(remoteBuildLogFile);
staticProcess.stdout.on('data', (data) => {
const ANSII_CODE_REGEX =
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
const stdoutString = data.toString().replace(ANSII_CODE_REGEX, '');
stdoutStream.write(stdoutString);
if (stdoutString.includes('Successfully ran target build')) {
staticProcess.stdout.removeAllListeners('data');
logger.info(
Expand All @@ -58,8 +68,11 @@ export async function buildStaticRemotes(
});
staticProcess.stderr.on('data', (data) => logger.info(data.toString()));
staticProcess.on('exit', (code) => {
stdoutStream.end();
if (code !== 0) {
throw new Error(`Remotes failed to build. See above for errors.`);
throw new Error(
`Remote failed to start. A complete log can be found in: ${remoteBuildLogFile}`
);
}
});
process.on('SIGTERM', () => staticProcess.kill('SIGTERM'));
Expand Down
Expand Up @@ -18,9 +18,10 @@ import {
createAsyncIterable,
} from '@nx/devkit/src/utils/async-iterable';
import { waitForPortOpen } from '@nx/web/src/utils/wait-for-port-open';
import { fork } from 'child_process';
import { basename, dirname, join } from 'path';
import { cpSync } from 'fs';
import { projectGraphCacheDirectory } from 'nx/src/utils/cache-directory';
import { fork } from 'node:child_process';
import { basename, dirname, join } from 'node:path';
import { createWriteStream, cpSync } from 'node:fs';
Comment on lines +22 to +24
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above


type ModuleFederationDevServerOptions = WebDevServerOptions & {
devRemotes?: string[];
Expand Down Expand Up @@ -178,10 +179,19 @@ async function buildStaticRemotes(
stdio: ['ignore', 'pipe', 'pipe', 'ipc'],
}
);

// File to debug build failures e.g. 2024-01-01T00_00_0_0Z-build.log'
const remoteBuildLogFile = join(
projectGraphCacheDirectory,
`${new Date().toISOString().replace(/[:\.]/g, '_')}-build.log`
);
const stdoutStream = createWriteStream(remoteBuildLogFile);

staticProcess.stdout.on('data', (data) => {
const ANSII_CODE_REGEX =
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
const stdoutString = data.toString().replace(ANSII_CODE_REGEX, '');
stdoutStream.write(stdoutString);
if (stdoutString.includes('Successfully ran target build')) {
staticProcess.stdout.removeAllListeners('data');
logger.info(
Expand All @@ -192,8 +202,11 @@ async function buildStaticRemotes(
});
staticProcess.stderr.on('data', (data) => logger.info(data.toString()));
staticProcess.on('exit', (code) => {
stdoutStream.end();
if (code !== 0) {
throw new Error(`Remote failed to start. See above for errors.`);
throw new Error(
`Remote failed to start. A complete log can be found in: ${remoteBuildLogFile}`
);
}
});
process.on('SIGTERM', () => staticProcess.kill('SIGTERM'));
Expand Down