2GP Managed Package : ERROR running force:package:version:create: while Verifying Metadata Invalid type: namespace__CustomObject__c

2gpmanaged-packagepackagesalesforcedx-cli

I am trying to create a managed package version for 2GP using salesforce CLI, after running force:package:version:create command I am getting thousands of errors in the terminal
I am only able to see a portion of the errors in the terminal which say "Invalid type : namespace__CustomObject__c" for every custom object, setting or metadata, and consequently throws errors like "Variable does not exist: var2" or DML requires SObject or SObject list type: List<namespace__CustomObject__c>.

I have followed following approach to create the package –

  1. created DX project and authorised dev hub org, dev hub org is already linked with the namespace org which is also our source org
  2. created scratch org of "partner developer" edition
  3. pushed all metadata to the scratch org
  4. created package
  5. ran package:version:create command – after this I am getting all errors in Verifying Metadata stage

Here is how the config.json looks like

    {
    "packageDirectories": [
        {
            "path": "force-app",
            "default": true,
            "package": "packageName-MP-v1.0",
            "versionName": "ver 1.0",
            "versionNumber": "1.0.0.NEXT"
        }
    ],
    "name": "packageName",
    "namespace": "namespace",
    "sfdcLoginUrl": "https://login.salesforce.com",
    "sourceApiVersion": "55.0",
    "packageAliases": {
        "packageName-MP-v1.0": "0Ho.."
    }
}

and this is the project-scratch-def.json

{
  "orgName": "packageName Scratch Org",
  "edition": "Partner Developer",
  "features": ["EnableSetPasswordInApi"],
  "settings": {
    "lightningExperienceSettings": {
      "enableS1DesktopEnabled": true
    },
    "mobileSettings": {
      "enableS1EncryptedStoragePref2": false
    }
  }
}

Am I missing something in scratch org definition?

Any help is appreciated
Thank you!

Best Answer

These errors usually means that: (a) your package doesn't have a namespace (did you create an Unlocked Package by mistake?), (b) your source directory is missing some metadata (check force-app/main/default/objects), or (c) some of your metadata is invalid (e.g. a missing field or invalid profile in a Search Layout).

How you set up your Scratch Org has nothing to do with package version creation. Unfortunately, cascading errors like this can be challenging to figure out, but that's what you need to do.

You can capture the output using something like:

sfdx force:package:version:create -d packageName-MP-v1.0 -x -w 100 &> log.txt

From there, you'll have to comb through the log. I typically start by deleting all the obvious duplicates, in your case, things like "variable does not exist" and "DML requires sObject..." errors. You know those are cascading errors, so you can easily rule them out. In VS Code, you can do a replace against a regex (toggle the .* button), then you can do a search like ^.+dml requires sobject.+$

It may sound challenging, but most of the time, you can whittle down hundreds or thousands of errors with a few regex searches, and what's left should lead you to the original problem. It's usually not taken me more than just a few minutes to figure out which metadata was the problem. From there, you just need to figure out how to fix the metadata.

For the most part, your source code should not have any mention of the namespace, e.g. a custom field shouldn't be called namespace__Status__c, but instead just Status__c. The few exceptions are for things like Flows and some URLs.

Unfortunately, the best I can really give you is a vague, general-purpose solution, because it's impossible for us to tell exactly what went wrong without seeing log files. Since you've deployed to a Scratch Org, try resetting your tracking and synchronizing:

sfdx force:source:tracking:reset
sfdx force:source:pull -f

I strongly advise that you do this in a repository and that you check the results of the pull with a diff before trying to build another package version. You may need to clean up some files or delete redundant files (e.g. if you have namespace__Status__c and Status__c, you should get rid of namespace__Status__c).