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

fix: missing metadata type error matches toolbelt #155

Merged
merged 2 commits into from
Jul 29, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 60 additions & 50 deletions src/componentSetBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,61 +41,71 @@ export class ComponentSetBuilder {
const csAggregator: ComponentLike[] = [];

const { sourcepath, manifest, metadata, packagenames, apiversion, sourceapiversion } = options;
try {
if (sourcepath) {
logger.debug(`Building ComponentSet from sourcepath: ${sourcepath.toString()}`);
sourcepath.forEach((filepath) => {
if (fs.fileExistsSync(filepath)) {
csAggregator.push(...ComponentSet.fromSource(path.resolve(filepath)));
} else {
throw new SfdxError(`The sourcepath "${filepath}" is not a valid source file path.`);
}
});
}

if (sourcepath) {
logger.debug(`Building ComponentSet from sourcepath: ${sourcepath.toString()}`);
sourcepath.forEach((filepath) => {
if (fs.fileExistsSync(filepath)) {
csAggregator.push(...ComponentSet.fromSource(path.resolve(filepath)));
} else {
throw new SfdxError(`The sourcepath "${filepath}" is not a valid source file path.`);
}
});
}

// Return empty ComponentSet and use packageNames in the library via `.retrieve` options
if (packagenames) {
logger.debug(`Building ComponentSet for packagenames: ${packagenames.toString()}`);
csAggregator.push(...new ComponentSet([]));
}

// Resolve manifest with source in package directories.
if (manifest) {
logger.debug(`Building ComponentSet from manifest: ${manifest.manifestPath}`);
const directoryPaths = options.manifest.directoryPaths;
logger.debug(`Searching in packageDir: ${directoryPaths.join(', ')} for matching metadata`);
const compSet = await ComponentSet.fromManifest({
manifestPath: manifest.manifestPath,
resolveSourcePaths: options.manifest.directoryPaths,
forceAddWildcards: true,
});
csAggregator.push(...compSet);
}
// Return empty ComponentSet and use packageNames in the library via `.retrieve` options
if (packagenames) {
logger.debug(`Building ComponentSet for packagenames: ${packagenames.toString()}`);
csAggregator.push(...new ComponentSet([]));
}

// Resolve metadata entries with source in package directories.
if (metadata) {
logger.debug(`Building ComponentSet from metadata: ${metadata.metadataEntries.toString()}`);
const registry = new RegistryAccess();
// Resolve manifest with source in package directories.
if (manifest) {
logger.debug(`Building ComponentSet from manifest: ${manifest.manifestPath}`);
const directoryPaths = options.manifest.directoryPaths;
logger.debug(`Searching in packageDir: ${directoryPaths.join(', ')} for matching metadata`);
const compSet = await ComponentSet.fromManifest({
manifestPath: manifest.manifestPath,
resolveSourcePaths: options.manifest.directoryPaths,
forceAddWildcards: true,
});
csAggregator.push(...compSet);
}

// Build a Set of metadata entries
const filter = new ComponentSet();
metadata.metadataEntries.forEach((entry) => {
const splitEntry = entry.split(':');
// try and get the type by name to ensure no typos or errors in type name
// matches toolbelt functionality
registry.getTypeByName(splitEntry[0]);
filter.add({
type: splitEntry[0],
fullName: splitEntry.length === 1 ? '*' : splitEntry[1],
// Resolve metadata entries with source in package directories.
if (metadata) {
logger.debug(`Building ComponentSet from metadata: ${metadata.metadataEntries.toString()}`);
const registry = new RegistryAccess();

// Build a Set of metadata entries
const filter = new ComponentSet();
metadata.metadataEntries.forEach((entry) => {
const splitEntry = entry.split(':');
// try and get the type by name to ensure no typos or errors in type name
// matches toolbelt functionality
registry.getTypeByName(splitEntry[0]);
filter.add({
type: splitEntry[0],
fullName: splitEntry.length === 1 ? '*' : splitEntry[1],
});
});
});

const directoryPaths = options.metadata.directoryPaths;
logger.debug(`Searching for matching metadata in directories: ${directoryPaths.join(', ')}`);
const fromSource = ComponentSet.fromSource({ fsPaths: directoryPaths, include: filter });
// If no matching metadata is found, default to the original component set
const finalized = fromSource.size > 0 ? fromSource : filter;
csAggregator.push(...finalized);
const directoryPaths = options.metadata.directoryPaths;
logger.debug(`Searching for matching metadata in directories: ${directoryPaths.join(', ')}`);
const fromSource = ComponentSet.fromSource({ fsPaths: directoryPaths, include: filter });
// If no matching metadata is found, default to the original component set
const finalized = fromSource.size > 0 ? fromSource : filter;
csAggregator.push(...finalized);
}
} catch (e) {
if ((e as Error).message.includes('Missing metadata type definition in registry for id')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we logging this via telemetry (it could be that the regular exception logging picks them up ok)

If not, it'd be great to have these as a type of exception, with the mdtype(s) in there, so we can use that data to help MD owners know customers are trying to deploy their feature AND help us prioritize support gaps.

// to remain generic to catch missing metadata types regardless of parameters, split on '
// example message : Missing metadata type definition in registry for id 'NonExistentType'
const issueType = (e as Error).message.split("'")[1];
throw new SfdxError(`The specified metadata type is unsupported: [${issueType}]`);
} else {
throw e;
}
}

const componentSet = new ComponentSet(csAggregator);
Expand Down
3 changes: 1 addition & 2 deletions test/commands/source/componentSetBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ describe('ComponentSetBuilder', () => {
assert.fail('the above should throw an error');
} catch (e) {
expect(e).to.not.be.null;
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
expect(e.message).to.include("Missing metadata type definition in registry for id 'notatype'");
expect((e as Error).message).to.include('The specified metadata type is unsupported: [notatype]');
}
});

Expand Down
2 changes: 1 addition & 1 deletion test/nuts/seeds/convert.seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ context('Convert NUTs [name: %REPO_NAME%] [exec: %EXECUTABLE%]', () => {

it('should throw an error if the metadata is not valid', async () => {
const convert = await testkit.convert({ args: '--metadata DOES_NOT_EXIST', exitCode: 1 });
const expectedError = testkit.isLocalExecutable() ? 'RegistryError' : 'UnsupportedType';
const expectedError = testkit.isLocalExecutable() ? 'SfdxError' : 'UnsupportedType';
testkit.expect.errorToHaveName(convert, expectedError);
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/nuts/seeds/deploy.metadata.seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ context('Deploy metadata NUTs [name: %REPO_NAME%] [exec: %EXECUTABLE%]', () => {

it('should throw an error if the metadata is not valid', async () => {
const deploy = await testkit.deploy({ args: '--metadata DOES_NOT_EXIST', exitCode: 1 });
const expectedError = testkit.isLocalExecutable() ? 'RegistryError' : 'UnsupportedType';
const expectedError = testkit.isLocalExecutable() ? 'SfdxError' : 'UnsupportedType';
testkit.expect.errorToHaveName(deploy, expectedError);
});

Expand Down
2 changes: 1 addition & 1 deletion test/nuts/seeds/retrieve.metadata.seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ context('Retrieve metadata NUTs [name: %REPO_NAME%] [exec: %EXECUTABLE%]', () =>

it('should throw an error if the metadata is not valid', async () => {
const retrieve = await testkit.retrieve({ args: '--metadata DOES_NOT_EXIST', exitCode: 1 });
const expectedError = testkit.isLocalExecutable() ? 'RegistryError' : 'UnsupportedType';
const expectedError = testkit.isLocalExecutable() ? 'SfdxError' : 'UnsupportedType';
testkit.expect.errorToHaveName(retrieve, expectedError);
});
});
Expand Down