Lightning Web Components – Solving ‘Could Not Infer a Metadata Type’ Error in 2GP Package Version Creation

2gpcsplightning-web-componentsrest-api

Am starting to build Lightning Web Components that will need to call an external API. Looks like I need to add CSP Trusted Sites and Remote Site Settings. So I created folders in /force-app/main/default called cspTrustedSites and remoteSiteSettings. Within them, I followed the metadata documentation.

Here's MyTrustedSites.cspTrustedSite:

<?xml version="1.0" encoding="UTF-8"?>
<CspTrustedSite xmlns="https://soap.sforce.com/2006/04/metadata">
  <context>All</context>
  <description>For API calls, styles, fonts, media, and images</description>
  <developerName>MyTrustedSites</developerName>
  <endpointUrl>https://*.example.com</endpointUrl>
  <isActive>true</isActive>
  <isApplicableToConnectSrc>true</isApplicableToConnectSrc>
  <isApplicableToFontSrc>true</isApplicableToFontSrc>
  <isApplicableToFrameSrc>true</isApplicableToFrameSrc>
  <isApplicableToImgSrc>true</isApplicableToImgSrc>
  <isApplicableToMediaSrc>true</isApplicableToMediaSrc>
  <isApplicableToStyleSrc>true</isApplicableToStyleSrc>
  <masterLabel>My Trusted Sites</masterLabel>
  <namespacePrefix>sumthin</namespacePrefix>
</CspTrustedSite>

When I issue sfdx package:version:create -p 0Ho... --installation-key-bypass -b 2gp-internal --code-coverage, I get this error:

Error (1): Component conversion failed:
/var/folders/qd/REDACTED/T/0Ho…-REDACTED/md-files/cspTrustedSites/MyTrustedSites.cspTrustedSit:
Could not infer a metadata type

Note: the error message omits the final "e" in the file extension but it's there. Seems to be an sfdx bug.

If I move the cspTrustedSites and remoteSiteSettings folders out of my project temporarily, I can create the package version.

sfdx --version
sfdx-cli/7.187.1 darwin-arm64 node-v18.14.0

Any thoughts on what I'm doing wrong?

Best Answer

The full name of my metadata file was MyTrustedSites.cspTrustedSite. It needed to be MyTrustedSites.cspTrustedSite-meta.xml.

My mistake was using the file created by sf retrieve metadata without changing the name. Not sure why it doesn't add -meta.xml by default but...

Related Topic