Skip to content

Commit 189f440

Browse files
committed
chore(core): enhance logging and error handling in preset generator
1 parent b4f583a commit 189f440

File tree

2 files changed

+54
-14
lines changed

2 files changed

+54
-14
lines changed

packages/nx/src/config/schema-utils.ts

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,28 @@ export function getImplementationFactory<T>(
2121
const [implementationModulePath, implementationExportName] =
2222
implementation.split('#');
2323
return () => {
24-
console.log('resolving implementation');
24+
console.log(`Resolving implementation: ${implementation} from ${directory}`);
2525
const modulePath = resolveImplementation(
2626
implementationModulePath,
2727
directory,
2828
packageName,
2929
projects
3030
);
31+
console.log(`Resolved module path: ${modulePath}`);
3132
if (extname(modulePath) === '.ts') {
3233
registerPluginTSTranspiler();
3334
}
35+
try {
3436
const module = require(modulePath);
3537
return implementationExportName
3638
? module[implementationExportName]
3739
: module.default ?? module;
40+
} catch (e) {
41+
console.error(`Failed to require implementation at ${modulePath}:`, e);
42+
throw new Error(
43+
`Could not require implementation "${implementation}" from "${directory}". Ensure the implementation is correctly exported.`
44+
);
45+
}
3846
};
3947
}
4048

@@ -50,6 +58,9 @@ export function resolveImplementation(
5058
packageName: string,
5159
projects: Record<string, ProjectConfiguration>
5260
): string {
61+
62+
console.log(`Resolving implementation: ${implementationModulePath} from ${directory}`);
63+
5364
const validImplementations = ['', '.js', '.ts'].map(
5465
(x) => implementationModulePath + x
5566
);
@@ -66,6 +77,7 @@ export function resolveImplementation(
6677
projects
6778
);
6879
if (maybeImplementationFromSource) {
80+
console.log(`Resolved from source: ${maybeImplementationFromSource}`);
6981
return maybeImplementationFromSource;
7082
}
7183
}
@@ -74,17 +86,26 @@ export function resolveImplementation(
7486
for (const maybeImplementation of validImplementations) {
7587
const maybeImplementationPath = join(directory, maybeImplementation);
7688
if (existsSync(maybeImplementationPath)) {
89+
console.log(`Resolved from directory: ${maybeImplementationPath}`);
7790
return maybeImplementationPath;
7891
}
7992

8093
try {
81-
return require.resolve(maybeImplementation, {
94+
const resolved = require.resolve(maybeImplementation, {
8295
paths: [directory],
8396
});
84-
} catch {}
97+
console.log(`Resolved via require.resolve: ${resolved}`);
98+
return resolved;
99+
} catch(e) {
100+
console.error(`Failed to resolve "${maybeImplementation}" from "${directory}":`, e);
101+
// If it fails, we continue to the next valid implementation
102+
// This is useful for cases where the implementation might be in a different format
103+
// or if the file is not found in the expected location.
104+
continue;
105+
}
85106
}
86107

87-
throw new Error(
108+
throw new Error(
88109
`Could not resolve "${implementationModulePath}" from "${directory}".`
89110
);
90111
}
@@ -127,14 +148,19 @@ function tryResolveFromSource(
127148
packageName: string,
128149
projects: Record<string, ProjectConfiguration>
129150
): string | null {
151+
console.log(`Trying to resolve from source: ${path} in ${directory} for package: ${packageName}`);
152+
130153
packageToProjectMap ??=
131154
getWorkspacePackagesMetadata(projects).packageToProjectMap;
132155
const localProject = packageToProjectMap[packageName];
133156
if (!localProject) {
134-
// it doesn't match any of the package names from the local projects
157+
console.log(`No local project found for package: ${packageName}`);
158+
// it doesn't match any of the package names from the local projects
135159
return null;
136160
}
137161

162+
console.log(`Found local project for package: ${packageName} at ${localProject.root}`);
163+
138164
try {
139165
const fromExports = resolveExports(
140166
{
@@ -146,13 +172,16 @@ function tryResolveFromSource(
146172
);
147173
if (fromExports && fromExports.length) {
148174
for (const exportPath of fromExports) {
149-
if (existsSync(join(directory, exportPath))) {
150-
return join(directory, exportPath);
175+
const fullPath = join(directory, exportPath);
176+
if (existsSync(fullPath)) {
177+
console.log(`Resolved from exports: ${fullPath}`);
178+
return fullPath;
151179
}
152180
}
153181
}
154-
} catch {}
155-
182+
} catch (e) {
183+
console.log(`Failed to resolve from exports: ${e.message}`);
184+
}
156185
/**
157186
* Fall back to try to "guess" the source by checking the path in some common directories:
158187
* - the root of the project
@@ -169,6 +198,7 @@ function tryResolveFromSource(
169198

170199
for (const possiblePath of possiblePaths) {
171200
if (existsSync(possiblePath)) {
201+
console.log(`Resolved from fallback path: ${possiblePath}`);
172202
return possiblePath;
173203
}
174204
}

packages/workspace/src/generators/preset/preset.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,26 @@ import { Preset } from '../utils/presets';
44
import { join } from 'path';
55

66
export async function presetGenerator(tree: Tree, options: Schema) {
7-
const presetTask = await createPreset(tree, options);
8-
return async () => {
9-
installPackagesTask(tree);
10-
if (presetTask) await presetTask();
11-
};
7+
8+
console.log(`Running preset generator with preset: ${options.preset} and workspaces: ${options.workspaces}`);
9+
10+
try {
11+
const presetTask = await createPreset(tree, options);
12+
return async () => {
13+
installPackagesTask(tree);
14+
if (presetTask) await presetTask();
15+
};
16+
} catch (e) {
17+
console.log(`Preset generation error: ${e.stack}`);
18+
throw new Error(e);
19+
}
1220
}
1321

1422
export default presetGenerator;
1523

1624
async function createPreset(tree: Tree, options: Schema) {
25+
console.log(`Creating preset: ${options.preset}`);
26+
1727
console.log('reading nx.json');
1828
const nxJson = readNxJson(tree);
1929
const addPlugin =

0 commit comments

Comments
 (0)