Skip to content

Commit d1e7cf7

Browse files
authored
Add additional debug logging when inferring the microfrontends config (#23)
* Add additional debug logging when inferring the microfrontends config * changeset
1 parent 6b7262a commit d1e7cf7

File tree

5 files changed

+100
-1
lines changed

5 files changed

+100
-1
lines changed

.changeset/shy-pens-wave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vercel/microfrontends': patch
3+
---
4+
5+
Improve debug logging when inferring the microfrontends.json configuration when the MFE_DEBUG env var is set.

packages/microfrontends/src/bin/port.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { logger } from './logger';
88
* Assumes cwd is the package directory for the current application.
99
*/
1010
export function displayPort(): void {
11+
// Don't log any debug information when computing the port to use.
12+
delete process.env.MFE_DEBUG;
1113
const portInfo = mfePort(cwd());
1214
header(portInfo);
1315
logger.info(portInfo.port);

packages/microfrontends/src/config/microfrontends/server/index.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { findPackageRoot } from '../utils/find-package-root';
1313
import { findConfig } from '../utils/find-config';
1414
import { MicrofrontendConfigIsomorphic } from '../../microfrontends-config/isomorphic';
1515
import { getApplicationContext } from '../utils/get-application-context';
16+
import { logger } from '../../../bin/logger';
1617
import { getOutputFilePath } from './utils/get-output-file-path';
1718
import { validateSchema } from './validation';
1819

@@ -129,7 +130,14 @@ class MicrofrontendsServer {
129130
filePath?: string;
130131
cookies?: { name: string; value: string }[];
131132
} = {}): MicrofrontendsServer {
133+
logger.debug('[MFE Config] Starting config inference', {
134+
appName,
135+
directory: directory || process.cwd(),
136+
filePath,
137+
});
138+
132139
if (filePath) {
140+
logger.debug('[MFE Config] Using explicit filePath:', filePath);
133141
return MicrofrontendsServer.fromFile({
134142
filePath,
135143
cookies,
@@ -138,20 +146,31 @@ class MicrofrontendsServer {
138146

139147
try {
140148
const packageRoot = findPackageRoot(directory);
149+
logger.debug('[MFE Config] Package root:', packageRoot);
150+
141151
const applicationContext = getApplicationContext({
142152
appName,
143153
packageRoot,
144154
});
155+
logger.debug('[MFE Config] Application context:', applicationContext);
145156

146157
const customConfigFilename =
147158
process.env.VC_MICROFRONTENDS_CONFIG_FILE_NAME;
148159

160+
if (customConfigFilename) {
161+
logger.debug(
162+
'[MFE Config] Custom config filename from VC_MICROFRONTENDS_CONFIG_FILE_NAME:',
163+
customConfigFilename,
164+
);
165+
}
166+
149167
// see if we have a config file at the package root
150168
const maybeConfig = findConfig({
151169
dir: packageRoot,
152170
customConfigFilename,
153171
});
154172
if (maybeConfig) {
173+
logger.debug('[MFE Config] Config found at package root:', maybeConfig);
155174
return MicrofrontendsServer.fromFile({
156175
filePath: maybeConfig,
157176
cookies,
@@ -161,48 +180,85 @@ class MicrofrontendsServer {
161180
// if we don't have a microfrontends configuration file, see if we have another package in the repo that references this one
162181
const repositoryRoot = findRepositoryRoot();
163182
const isMonorepo = isRepositoryMonorepo({ repositoryRoot });
183+
logger.debug(
184+
'[MFE Config] Repository root:',
185+
repositoryRoot,
186+
'Is monorepo:',
187+
isMonorepo,
188+
);
189+
164190
const configFromEnv = process.env.VC_MICROFRONTENDS_CONFIG;
165191
// the environment variable, if specified, takes precedence over other inference methods
166192
if (typeof configFromEnv === 'string') {
193+
logger.debug(
194+
'[MFE Config] Checking VC_MICROFRONTENDS_CONFIG:',
195+
configFromEnv,
196+
);
167197
const maybeConfigFromEnv = resolve(packageRoot, configFromEnv);
168198
if (maybeConfigFromEnv) {
199+
logger.debug(
200+
'[MFE Config] Config loaded from VC_MICROFRONTENDS_CONFIG:',
201+
maybeConfigFromEnv,
202+
);
169203
return MicrofrontendsServer.fromFile({
170204
filePath: maybeConfigFromEnv,
171205
cookies,
172206
});
173207
}
174208
} else {
175209
// when the VC_MICROFRONTENDS_CONFIG environment variable is not set, try to find the config in the .vercel directory first
210+
const vercelDir = join(packageRoot, '.vercel');
211+
logger.debug(
212+
'[MFE Config] Searching for config in .vercel directory:',
213+
vercelDir,
214+
);
176215
const maybeConfigFromVercel = findConfig({
177-
dir: join(packageRoot, '.vercel'),
216+
dir: vercelDir,
178217
customConfigFilename,
179218
});
180219
if (maybeConfigFromVercel) {
220+
logger.debug(
221+
'[MFE Config] Config found in .vercel directory:',
222+
maybeConfigFromVercel,
223+
);
181224
return MicrofrontendsServer.fromFile({
182225
filePath: maybeConfigFromVercel,
183226
cookies,
184227
});
185228
}
186229

187230
if (isMonorepo) {
231+
logger.debug(
232+
'[MFE Config] Inferring microfrontends location in monorepo for application:',
233+
applicationContext.name,
234+
);
188235
// find the default package
189236
const defaultPackage = inferMicrofrontendsLocation({
190237
repositoryRoot,
191238
applicationContext,
192239
customConfigFilename,
193240
});
241+
logger.debug(
242+
'[MFE Config] Inferred package location:',
243+
defaultPackage,
244+
);
194245

195246
// see if we have a config file at the package root
196247
const maybeConfigFromDefault = findConfig({
197248
dir: defaultPackage,
198249
customConfigFilename,
199250
});
200251
if (maybeConfigFromDefault) {
252+
logger.debug(
253+
'[MFE Config] Config found in inferred package:',
254+
maybeConfigFromDefault,
255+
);
201256
return MicrofrontendsServer.fromFile({
202257
filePath: maybeConfigFromDefault,
203258
cookies,
204259
});
205260
}
261+
logger.debug('[MFE Config] No config found in inferred package');
206262
}
207263
}
208264
// will be caught below
@@ -234,8 +290,13 @@ class MicrofrontendsServer {
234290
cookies?: { name: string; value: string }[];
235291
}): MicrofrontendsServer {
236292
try {
293+
logger.debug('[MFE Config] Reading config from file:', filePath);
237294
const configJson = fs.readFileSync(filePath, 'utf-8');
238295
const config = MicrofrontendsServer.validate(configJson);
296+
logger.debug(
297+
'[MFE Config] Config loaded with applications:',
298+
Object.keys(config.applications),
299+
);
239300

240301
return new MicrofrontendsServer({
241302
config,

packages/microfrontends/src/config/microfrontends/utils/get-application-context.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from 'node:fs';
22
import path from 'node:path';
33
import { MicrofrontendError } from '../../errors';
4+
import { logger } from '../../../bin/logger';
45

56
export interface ApplicationContext {
67
name: string;
@@ -16,10 +17,12 @@ export function getApplicationContext(opts?: {
1617
packageRoot?: string;
1718
}): ApplicationContext {
1819
if (opts?.appName) {
20+
logger.debug('[MFE Config] Application name from appName parameter:', opts.appName);
1921
return { name: opts.appName };
2022
}
2123

2224
if (process.env.VERCEL_PROJECT_NAME) {
25+
logger.debug('[MFE Config] Application name from VERCEL_PROJECT_NAME:', process.env.VERCEL_PROJECT_NAME);
2326
return {
2427
name: process.env.VERCEL_PROJECT_NAME,
2528
projectName: process.env.VERCEL_PROJECT_NAME,
@@ -30,6 +33,7 @@ export function getApplicationContext(opts?: {
3033
// might not even have a package.json. Use the environment variable, which
3134
// relies on NX's logic to find the name.
3235
if (process.env.NX_TASK_TARGET_PROJECT) {
36+
logger.debug('[MFE Config] Application name from NX_TASK_TARGET_PROJECT:', process.env.NX_TASK_TARGET_PROJECT);
3337
return {
3438
name: process.env.NX_TASK_TARGET_PROJECT,
3539
packageJsonName: process.env.NX_TASK_TARGET_PROJECT,
@@ -45,6 +49,7 @@ export function getApplicationContext(opts?: {
4549
projectName?: string;
4650
};
4751
if (projectJson.projectName) {
52+
logger.debug('[MFE Config] Application name from .vercel/project.json:', projectJson.projectName);
4853
return {
4954
name: projectJson.projectName,
5055
projectName: projectJson.projectName,
@@ -73,6 +78,7 @@ export function getApplicationContext(opts?: {
7378
);
7479
}
7580

81+
logger.debug('[MFE Config] Application name from package.json:', packageJson.name);
7682
return { name: packageJson.name, packageJsonName: packageJson.name };
7783
} catch (err) {
7884
throw MicrofrontendError.handle(err, {

packages/microfrontends/src/config/microfrontends/utils/infer-microfrontends-location.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { parse } from 'jsonc-parser';
44
import fg from 'fast-glob';
55
import type { Config } from '../../schema/types';
66
import { MicrofrontendError } from '../../errors';
7+
import { logger } from '../../../bin/logger';
78
import { getPossibleConfigurationFilenames } from './get-config-file-name';
89
import type { ApplicationContext } from './get-application-context';
910

@@ -28,6 +29,11 @@ function findPackageWithMicrofrontendsConfig({
2829
customConfigFilename,
2930
}: FindDefaultMicrofrontendPackageArgs): string | null {
3031
const applicationName = applicationContext.name;
32+
logger.debug(
33+
'[MFE Config] Searching repository for configs containing application:',
34+
applicationName,
35+
);
36+
3137
try {
3238
// eslint-disable-next-line import/no-named-as-default-member
3339
const microfrontendsJsonPaths = fg.globSync(
@@ -41,6 +47,12 @@ function findPackageWithMicrofrontendsConfig({
4147
},
4248
);
4349

50+
logger.debug(
51+
'[MFE Config] Found',
52+
microfrontendsJsonPaths.length,
53+
'config file(s) in repository',
54+
);
55+
4456
const matchingPaths: string[] = [];
4557
for (const microfrontendsJsonPath of microfrontendsJsonPaths) {
4658
try {
@@ -51,12 +63,20 @@ function findPackageWithMicrofrontendsConfig({
5163
const microfrontendsJson = parse(microfrontendsJsonContent) as Config;
5264

5365
if (microfrontendsJson.applications[applicationName]) {
66+
logger.debug(
67+
'[MFE Config] Found application in config:',
68+
microfrontendsJsonPath,
69+
);
5470
matchingPaths.push(microfrontendsJsonPath);
5571
} else {
5672
for (const [_, app] of Object.entries(
5773
microfrontendsJson.applications,
5874
)) {
5975
if (app.packageName === applicationName) {
76+
logger.debug(
77+
'[MFE Config] Found application via packageName in config:',
78+
microfrontendsJsonPath,
79+
);
6080
matchingPaths.push(microfrontendsJsonPath);
6181
}
6282
}
@@ -66,6 +86,11 @@ function findPackageWithMicrofrontendsConfig({
6686
}
6787
}
6888

89+
logger.debug(
90+
'[MFE Config] Total matching config files:',
91+
matchingPaths.length,
92+
);
93+
6994
if (matchingPaths.length > 1) {
7095
throw new MicrofrontendError(
7196
`Found multiple \`microfrontends.json\` files in the repository referencing the application "${applicationName}", but only one is allowed.\n${matchingPaths.join('\n • ')}`,

0 commit comments

Comments
 (0)